版本比较
勰码金牌学院 S2 课程 3. 单调栈
正文
增加 0 行删除 186 行
17 行未改动18192021222324252627282930313233343536373839404142434445464748495051525354555629 行未改动868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312441 行未改动16616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320435 行未改动24024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727819 行未改动29829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533630 行未改动36736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440525 行未改动17 行未改动但是发现这个做法会 T,瓶颈在于压 $h_{top}$ 的时候太慢了,所以我们考虑用结构体,里面维护 $h$ 和 $sum$(几个 $h$),这样压栈时就是 $O(1)$ 了。需要注意此时弹栈时 $top$ 的贡献变成了 $sum$ 不是 $1$。```cpp/*_/ _/ _/_/_/_/_/_/ _/ _/ _/_/_/ _/ _/ _/_/ _/ _/ _/_/ _/ _/ _/ _/ _/ _/ _/_/_/_/_/_/_/ _/ _/ _/ _/ _/ _/_/ _/ _/ _/ _/_/ _/_/_/_/_/_/_/ _/ _/ _/ _/ _/ _/_/ _/ _/_/_/_/_/_/ _/ _/ _/ _/*/#include<iostream>#include<cstdio>#include<stack>#include<algorithm>#define re register#define endl '\n'#define ll long long#define ull unsigned long long#define db double#define fi const int&#define fl const ll&#define ful const ull&#define fc const char&#define fs const string&#define debug() puts("Running Successfully")using namespace std ;namespace IO {template<class T>inline void read(T &x){char c=getchar(),f=false;x=0;while(c<48||c>57) {f|=(c==45),c=getchar();}while(c>=48&&c<=57){x=(x<<3)+(x<<1)+(c^48),c=getchar();}if(f){x=~x+1;}}template<class T,class ...Arg>inline void read(T &x,Arg &...arg){read(x),read(arg...);}template<class T>inline void write(T x){if(x<0){ putchar(45),x=~x+1;}if(x>9){write(x/10);}putchar(x%10|48);}template<class T>inline void write(T x,fc c){write(x),putchar(c);}}using namespace IO ;const int N = 5e5 + 2 ;int n,h[N] ;ll ans ;struct node {29 行未改动假设现在是处理着第 $j$ 列,枚举每个 $(i,j)$,$f_{i,j}$ 作为最小值(作为高),单调栈求出向左向右最多能延申多长(需要保证 $f_{i,j}$ 最小),找最大值即可。```cpp/*_/ _/ _/_/_/_/_/_/ _/ _/ _/_/_/ _/ _/ _/_/ _/ _/ _/_/ _/ _/ _/ _/ _/ _/ _/_/_/_/_/_/_/ _/ _/ _/ _/ _/ _/_/ _/ _/ _/ _/_/ _/_/_/_/_/_/_/ _/ _/ _/ _/ _/ _/_/ _/ _/_/_/_/_/_/ _/ _/ _/ _/*/#include<iostream>#include<cstdio>#include<stack>#include<algorithm>#define re register#define endl '\n'#define ll long long#define ull unsigned long long#define db double#define fi const int&#define fl const ll&#define ful const ull&#define fc const char&#define fs const string&#define debug() puts("Running Successfully")using namespace std ;namespace IO {template<class T>inline void read(T &x){char c=getchar(),f=false;x=0;while(c<48||c>57) {f|=(c==45),c=getchar();}while(c>=48&&c<=57){x=(x<<3)+(x<<1)+(c^48),c=getchar();}if(f){x=~x+1;}}template<class T,class ...Arg>inline void read(T &x,Arg &...arg){read(x),read(arg...);}template<class T>inline void write(T x){if(x<0){ putchar(45),x=~x+1;}if(x>9){write(x/10);}putchar(x%10|48);}template<class T>inline void write(T x,fc c){write(x),putchar(c);}}using namespace IO ;const int N = 2002 ;int n,m ;int a[N][N],f[N][N] ;int l[N],r[N],res,ans ;41 行未改动预处理前缀和。```cpp/*_/ _/ _/_/_/_/_/_/ _/ _/ _/_/_/ _/ _/ _/_/ _/ _/ _/_/ _/ _/ _/ _/ _/ _/ _/_/_/_/_/_/_/ _/ _/ _/ _/ _/ _/_/ _/ _/ _/ _/_/ _/_/_/_/_/_/_/ _/ _/ _/ _/ _/ _/_/ _/ _/_/_/_/_/_/ _/ _/ _/ _/*/#include<iostream>#include<cstdio>#include<stack>#include<algorithm>#define re register#define endl '\n'#define ll long long#define ull unsigned long long#define db double#define fi const int&#define fl const ll&#define ful const ull&#define fc const char&#define fs const string&#define debug() puts("Running Successfully")using namespace std ;namespace IO {template<class T>inline void read(T &x){char c=getchar(),f=false;x=0;while(c<48||c>57) {f|=(c==45),c=getchar();}while(c>=48&&c<=57){x=(x<<3)+(x<<1)+(c^48),c=getchar();}if(f){x=~x+1;}}template<class T,class ...Arg>inline void read(T &x,Arg &...arg){read(x),read(arg...);}template<class T>inline void write(T x){if(x<0){ putchar(45),x=~x+1;}if(x>9){write(x/10);}putchar(x%10|48);}template<class T>inline void write(T x,fc c){write(x),putchar(c);}}using namespace IO ;const int N = 1e6 + 2 ;int n,a[N] ;int l[N],r[N] ;ll sum[N],ans,L,R ;35 行未改动答案就是 $\max sum_i$。```cpp/*_/ _/ _/_/_/_/_/_/ _/ _/ _/_/_/ _/ _/ _/_/ _/ _/ _/_/ _/ _/ _/ _/ _/ _/ _/_/_/_/_/_/_/ _/ _/ _/ _/ _/ _/_/ _/ _/ _/ _/_/ _/_/_/_/_/_/_/ _/ _/ _/ _/ _/ _/_/ _/ _/_/_/_/_/_/ _/ _/ _/ _/*/#include<iostream>#include<cstdio>#include<stack>#include<algorithm>#define re register#define endl '\n'#define ll long long#define ull unsigned long long#define db double#define fi const int&#define fl const ll&#define ful const ull&#define fc const char&#define fs const string&#define debug() puts("Running Successfully")using namespace std ;namespace IO {template<class T>inline void read(T &x){char c=getchar(),f=false;x=0;while(c<48||c>57) {f|=(c==45),c=getchar();}while(c>=48&&c<=57){x=(x<<3)+(x<<1)+(c^48),c=getchar();}if(f){x=~x+1;}}template<class T,class ...Arg>inline void read(T &x,Arg &...arg){read(x),read(arg...);}template<class T>inline void write(T x){if(x<0){ putchar(45),x=~x+1;}if(x>9){write(x/10);}putchar(x%10|48);}template<class T>inline void write(T x,fc c){write(x),putchar(c);}}using namespace IO ;const int N = 1e6 + 2 ;int n,h[N],v[N] ;ll sum[N],ans ;stack<int> st ;19 行未改动把 [MarsOJ P2017](https://marsoj.cn/p/P2017) 的和换成 $r_i-l_i+1$ 即可。```cpp/*_/ _/ _/_/_/_/_/_/ _/ _/ _/_/_/ _/ _/ _/_/ _/ _/ _/_/ _/ _/ _/ _/ _/ _/ _/_/_/_/_/_/_/ _/ _/ _/ _/ _/ _/_/ _/ _/ _/ _/_/ _/_/_/_/_/_/_/ _/ _/ _/ _/ _/ _/_/ _/ _/_/_/_/_/_/ _/ _/ _/ _/*/#include<iostream>#include<cstdio>#include<stack>#include<algorithm>#define re register#define endl '\n'#define ll long long#define ull unsigned long long#define db double#define fi const int&#define fl const ll&#define ful const ull&#define fc const char&#define fs const string&#define debug() puts("Running Successfully")using namespace std ;namespace IO {template<class T>inline void read(T &x){char c=getchar(),f=false;x=0;while(c<48||c>57) {f|=(c==45),c=getchar();}while(c>=48&&c<=57){x=(x<<3)+(x<<1)+(c^48),c=getchar();}if(f){x=~x+1;}}template<class T,class ...Arg>inline void read(T &x,Arg &...arg){read(x),read(arg...);}template<class T>inline void write(T x){if(x<0){ putchar(45),x=~x+1;}if(x>9){write(x/10);}putchar(x%10|48);}template<class T>inline void write(T x,fc c){write(x),putchar(c);}}using namespace IO ;const int N = 1e5 + 2 ;int n,h[N] ;int l[N],r[N] ;ll ans ;30 行未改动就是 [MarsOJ P2017](https://marsoj.cn/p/P2017)。```cpp/*_/ _/ _/_/_/_/_/_/ _/ _/ _/_/_/ _/ _/ _/_/ _/ _/ _/_/ _/ _/ _/ _/ _/ _/ _/_/_/_/_/_/_/ _/ _/ _/ _/ _/ _/_/ _/ _/ _/ _/_/ _/_/_/_/_/_/_/ _/ _/ _/ _/ _/ _/_/ _/ _/_/_/_/_/_/ _/ _/ _/ _/*/#include<iostream>#include<cstdio>#include<stack>#include<algorithm>#define re register#define endl '\n'#define ll long long#define ull unsigned long long#define db double#define fi const int&#define fl const ll&#define ful const ull&#define fc const char&#define fs const string&#define debug() puts("Running Successfully")using namespace std ;namespace IO {template<class T>inline void read(T &x){char c=getchar(),f=false;x=0;while(c<48||c>57) {f|=(c==45),c=getchar();}while(c>=48&&c<=57){x=(x<<3)+(x<<1)+(c^48),c=getchar();}if(f){x=~x+1;}}template<class T,class ...Arg>inline void read(T &x,Arg &...arg){read(x),read(arg...);}template<class T>inline void write(T x){if(x<0){ putchar(45),x=~x+1;}if(x>9){write(x/10);}putchar(x%10|48);}template<class T>inline void write(T x,fc c){write(x),putchar(c);}}using namespace IO ;const int N = 1e5 + 2 ;int n,h[N] ;int l[N],r[N],id ;ll ans,s[N] ;25 行未改动17 行未改动181920212223242529 行未改动555657585960616241 行未改动10410510610710810911011135 行未改动14714814915015115215315419 行未改动17417517617717817918018130 行未改动21221321421521621721821925 行未改动17 行未改动但是发现这个做法会 T,瓶颈在于压 $h_{top}$ 的时候太慢了,所以我们考虑用结构体,里面维护 $h$ 和 $sum$(几个 $h$),这样压栈时就是 $O(1)$ 了。需要注意此时弹栈时 $top$ 的贡献变成了 $sum$ 不是 $1$。```cppconst int N = 5e5 + 2 ;int n,h[N] ;ll ans ;struct node {29 行未改动假设现在是处理着第 $j$ 列,枚举每个 $(i,j)$,$f_{i,j}$ 作为最小值(作为高),单调栈求出向左向右最多能延申多长(需要保证 $f_{i,j}$ 最小),找最大值即可。```cppconst int N = 2002 ;int n,m ;int a[N][N],f[N][N] ;int l[N],r[N],res,ans ;41 行未改动预处理前缀和。```cppconst int N = 1e6 + 2 ;int n,a[N] ;int l[N],r[N] ;ll sum[N],ans,L,R ;35 行未改动答案就是 $\max sum_i$。```cppconst int N = 1e6 + 2 ;int n,h[N],v[N] ;ll sum[N],ans ;stack<int> st ;19 行未改动把 [MarsOJ P2017](https://marsoj.cn/p/P2017) 的和换成 $r_i-l_i+1$ 即可。```cppconst int N = 1e5 + 2 ;int n,h[N] ;int l[N],r[N] ;ll ans ;30 行未改动就是 [MarsOJ P2017](https://marsoj.cn/p/P2017)。```cppconst int N = 1e5 + 2 ;int n,h[N] ;int l[N],r[N],id ;ll ans,s[N] ;25 行未改动