Skip to content

Commit

Permalink
Merge pull request #8 from Neurotical/master
Browse files Browse the repository at this point in the history
add 单调栈 网络流 2-sat 差分约束
  • Loading branch information
Neurotical authored Sep 7, 2024
2 parents d471b62 + 134b1bc commit 1bfd999
Show file tree
Hide file tree
Showing 3 changed files with 736 additions and 1 deletion.
37 changes: 37 additions & 0 deletions 2-数据结构.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,14 @@ int main()
return 0;
}
```
## 线段树上二分
eg. 两种操作,1. 修改ai为d 2. 查询l,r中第一次出现大于等于d位置,否则返回-1
维护区间最大值,
对一个区间判断最大值是否大于等于d
存在则先找左区间,再找右区间
## 区间最值线段树
Expand Down Expand Up @@ -391,8 +398,38 @@ int query(int l, int r, int cl = 1, int cr = n, int p = 1)
}
}
```
## 单调栈
满足栈中元素单调递增或递减的栈

可用于o(n)寻找每个数之后第一个大于他的数的位置(用单调递减栈)

可解决求 $max_{1\leq l\leq n,l\leq r \leq n}((r-l+1)*min_{l\leq i \leq r}a[i])$

即区间长度乘区间最值结果的最值

``` cpp
int n,m;//洛谷P5788
stack<int>st;
int a[3000005],ans[3000005];
void solve(){
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++){
while(st.size()&&a[i]>a[st.top()]){//维护递减栈
ans[st.top()] = i;//更新答案
st.pop();
}
st.push(i);
}
for(int i=1;i<=n;i++) cout<<ans[i]<<" ";
cout<<endl;
}
```



## 单调队列
p[head]表示序号 p[head]!=head
```cpp
int p[N];
int head=1,tail=0;
Expand Down
Loading

0 comments on commit 1bfd999

Please sign in to comment.