Personal feeling a little bit pit
Add tags are permanently
The tag used for set is passed
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#defineLson o<<1,l,m#defineRson (o<<1) +1,m+1,r#defineLS o<<1#defineRS (o<<1) +1using namespacestd;Const intn=1e4,inf=1e9;intminv[n<<2],maxv[n<<2],sumv[n<<2];intaddv[n<<2],setv[n<<2];intql,qr,p,v;// PointintQuerymin (intOintLintR) { intm=l+ (r-l) >>1), ans=INF; if(QL<=L&&R<=QR)returnMinv[o]; if(ql<=m) ans=min (ans,querymin (Lson)); if(m+1<=QR) ans=min (ans,querymin (Rson)); returnans;}voidChangeintOintLintR) { intm=l+ (r-l) >>1); if(l==r) Minv[o]=v; Else{ if(p<=m) Change (Lson); ElseChange (Rson); Minv[o]=min (Minv[ls],minv[rs]); }}//Addint_min,_max,_sum;voidMaintain (intOintLintR) {Sumv[o]=minv[o]=maxv[o]=0;//!! if(l<R) {Sumv[o]=sumv[ls]+Sumv[rs]; Minv[o]=min (Minv[ls],minv[rs]); Maxv[o]=Max (Maxv[ls],maxv[rs]); } Minv[o]+=Addv[o]; Maxv[o]+=Addv[o]; Sumv[o]+=addv[o]* (r-l+1);}voidAddRange (intOintLintR) { if(ql<=l&&r<=qr) Addv[o]+=v; Else{ intM= (r+l) >>1; if(ql<=m) addRange (Lson); if(m+1<=qr) addRange (Rson); } maintain (o,l,r);}voidQueryrange (intOintLintRintadd) { if(ql<=l&&r<=qr) {_sum+=sumv[o]+add* (r-l+1); _min=min (_min,minv[o]+add); _max=max (_max,maxv[o]+add); }Else{ intM= (r+l) >>1; if(ql<=m) Queryrange (lson,add+Addv[o]); if(m+1<=QR) Queryrange (rson,add+Addv[o]); }}//SetvoidMtintOintLintR) { if(setv[o]>=0) {Sumv[o]=setv[o]* (r-l+1); Minv[o]=maxv[o]=Setv[o]; }Else if(l<R) {Sumv[o]=sumv[ls]+Sumv[rs]; Minv[o]=min (Minv[ls],minv[rs]); Maxv[o]=Max (Maxv[ls],maxv[rs]); }}voidPushdown (into) { if(setv[o]>=0) {Setv[ls]=setv[rs]=Setv[o]; Setv[o]=-1; }}voidSetRange (intOintLintR) { if(QL<=L&&R<=QR) setv[o]=v; Else{pushdown (o); intM= (r+l) >>1; if(ql<=m) SetRange (Lson);ElseMT (Lson); if(m+1<=QR) SetRange (Rson);ElseMT (Rson); } MT (O,l,r);}voidQueryrange (intOintLintR) { if(setv[o]>=0) {_sum+=setv[o]* (r-l+1); _min=min (_min,setv[o]); _max=Max (_max,setv[o]); }Else if(ql<=l&&r<=qr) {_sum+=Sumv[o]; _min=min (_min,minv[o]); _max=Max (_max,maxv[o]); }Else{ intM= (r+l) >>1; if(ql<=m) queryrange (Lson); if(m+1<=qr) queryrange (Rson); }}intn,t;intMainintargcConst Char*argv[]) {//freopen ("In.txt", "R", stdin);//freopen ("2.txt", "w", stdout);Cin>>n>>T; for(intI=1; i<=n;i++) {cin>>v; QL=qr=i; AddRange (1,1, N); }//for (int i=1;i<=n<<1;i++) printf ("%d%d%d%d\n", I,sumv[i],minv[i],maxv[i]); while(t--){ intFlag; CIN>>Flag; if(flag==1) {cin>>ql>>QR; _sum=0; _min=inf;_max=-INF; Queryrange (1,1N0); printf ("%d\n", _sum); }Else{cin>>ql>>qr>>v; AddRange (1,1, N); _sum=0; _min=inf;_max=-INF; Queryrange (1,1N0); printf ("%d\n", _sum); } } return 0;}
Segment Tree (petition version)