版本比较
板子(自用)
正文
增加 192 行删除 0 行
1,091 行未改动全部展开10921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061,640 行未改动全部展开284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429351,091 行未改动全部展开 } return 0;}```# 线段树(指针)```cpp#include<bits/stdc++.h>#define pn putchar('\n')#define ps putchar(' ')#define rec(x) scanf(" %c",&(x))#define ls(i) (i>>1)#define rs(i) (i>>1|1)#define In(x) freopen(x".in","r",stdin)#define Out(x) freopen(x".ans","w",stdout)#define File(x) (In(x),Out(x))#define fi first#define se second#define endl '\n'#define debug(x) cout << #x << ": " << x << endl#define pub push_back#define pob pop_back#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?++x:--x))
using namespace std;typedef long long ll;
template <typename T> void re(T &t) { t=0; char ch=getchar(); int f=1; while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); } do { (t=((t<<3)+(t<<1)))+=(ch^48); ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;}
inline void wr(ll x){ if(x<0) putchar('-'),x=-x; if(x>9) wr(x/10); putchar(x%10+'0');}
ll a[100005],n,m;
struct seg{ int s,e,m; ll val=0,lazy=0; seg *l,*r; seg (int _s,int _e){ s=_s,e=_e,m=s+e>>1; if (s!=e){ l=new seg(s,m); r=new seg(m+1,e); val=l->val+r->val; } else val=a[s]; } void propo(){ if (lazy){ val+=lazy*(e-s+1); if (s!=e){ l->lazy+=lazy; r->lazy+=lazy; } lazy=0; } } void update(int i,int j,ll k){ if (s==i && e==j) lazy+=k,propo(); else{ if (j<=m) l->update(i,j,k); else if (m<i) r->update(i,j,k); else l->update(i,m,k),r->update(m+1,j,k); l->propo(),r->propo(); val=l->val+r->val; } } ll query(int i,int j){ propo(); if (s==i && e==j) return val; else if (j<=m) return l->query(i,j); else if (m<i) return r->query(i,j); else return l->query(i,m)+r->query(m+1,j); }} *rt;
int main(){ re(n),re(m); rep(i,1,n+1) re(a[i]); rt=new seg(1,n); rep(i,0,m){ int o,x,y; ll k; re(o),re(x),re(y); if(o-1){ wr(rt->query(x,y)),pn; } else{ re(k); rt->update(x,y,k); } } return 0;}
// rp++ Chtholly bless me!// If happiness has a color, it must be the blue dyed by the last red.// What do you do at the end tf the world? Are you busy? Will you save us?// May you grant me good fortune and brighter days.
```
# 线性筛```#include<bits/stdc++.h>#define N 1000051,640 行未改动全部展开 return 0;}
```# $O(n)$ RMQ```cpp#include<bits/stdc++.h>/*#include<ext/pb_ds/assoc_container.hpp>#include<ext/pb_ds/tree_policy.hpp>#include<ext/pb_ds/hash_policy.hpp>#include<ext/pb_ds/trie_policy.hpp>#include<ext/pb_ds/priority_queue.hpp>#include<ext/rope>using namespace __gnu_cxx;using namespace __gnu_pbds;*/#define pn putchar('\n')#define ps putchar(' ')using namespace std;typedef long long ll;
template <typename T> void re(T &t) { t=0; char ch=getchar(); int f=1; while (ch<'0'||ch>'9') { if (ch=='-') f=-1; ch=getchar(); } do { (t=((t<<3)+(t<<1)))+=(ch^48); ch=getchar(); } while ('0'<=ch&&ch<='9'); t*=f;}
inline void wr(auto x){ if(x<0) putchar('-'),x=-x; if(x>9) wr(x/10); putchar(x%10+'0');}
int k;inline int po(int x){ return (x+k-1)/k;}
int n,m;int lg[100005],a[100005];int s[10005][25];int pm[100005],nm[100005];
int main(){ re(n),re(m); lg[1]=0; for(int i=2;i<=n;i++) lg[i]=lg[i>>1]+1; k=lg[n]; for(int i=1;i<=n;i++){ re(a[i]); s[po(i)][0]=max(s[po(i)][0],a[i]); } int p=po(n); for(int j=1;j<=lg[p];j++){ for(int i=1;i<=p-(1<<j)+1;i++){ s[i][j]=max(s[i][j-1],s[i+(1<<j-1)][j-1]); } } for(int i=1;i<=n;i++){ if(i%k!=1) pm[i]=max(pm[i-1],a[i]); else pm[i]=a[i]; } for(int i=n;i>=1;i--){ if(i%k) nm[i]=max(nm[i+1],a[i]); else nm[i]=a[i]; } for(int i=1;i<=m;i++){ int l,r; re(l),re(r); int kk=lg[po(r)-po(l)-1]; if(po(l)==po(r)){ int ma=0; for(int i=l;i<=r;i++){ ma=max(a[i],ma); } wr(ma),pn; } else if(po(r)-po(l)==1) wr(max(pm[r],nm[l])),pn; else wr(max(max(s[po(l)+1][kk],s[po(r)-(1<<kk)][kk]),max(pm[r],nm[l]))),pn; } return 0;}
// rp++ Chtholly bless me!// If happiness has a color, it must be the blue dyed by the last red.// What do you do at the end of the world? Are you busy? Will you save us?// May you grant me good fortune and brighter days.```