Test instructions
Long Long data[250001];
void A (int st, int nd) {for (int i = st; i \le nd; i++) data[i] = Data[i] + (i-st + 1);}
void B (int st, int nd) {for (int i = st; i \le nd; i++) data[i] = Data[i] + (nd-i + 1);}
void C (int st, int nd, int x) {for (int i = st; i \le nd; i++) data[i] = x;}
Long Long S (int st, int nd) {long long res = 0; for (int i = st; i \le nd; i++) res + = Data[i]; return res;}
Four functions, corresponding to four operations A, B, C, S gives the m operation, the result of the S operation is returned.
Analysis:
Obviously the interval of the line tree is updated, but the choice of lazy tag to think about, c this operation good processing, we observe a, B discovery is added a arithmetic progression, arithmetic progression plus arithmetic progression or arithmetic progression, that we choose arithmetic progression first and tolerance mark.
#include <map>#include<Set>#include<list>#include<cmath>#include<queue>#include<stack>#include<cstdio>#include<vector>#include<string>#include<cctype>#include<complex>#include<cassert>#include<utility>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using namespaceStd;typedef pair<int,int>Pii;typedefLong Longll;#defineLson l,m,rt<<1#definePi ACOs (-1.0)#defineRson m+1,r,rt<<1|1#defineAll 1,n,1#defineRead Freopen ("In.txt", "R", stdin)#defineN 250001Constll infll =0x3f3f3f3f3f3f3f3fll;Const intinf=0x7ffffff;Const intMoD =1000000007;structnode{ll A1,d,sum,setv; intL,r;} T[n<<2];voidPushup (intRT) {T[rt].sum=t[rt<<1].sum+t[rt<<1|1].sum;}voidPushdown (intRT) { if(t[rt].setv!=INF) {T[rt<<1].setv=t[rt<<1|1].setv=T[rt].setv; T[rt<<1].a1=t[rt<<1].d=t[rt<<1|1].a1=t[rt<<1|1].d=0; T[rt<<1].sum=1ll* (t[rt<<1].r-t[rt<<1].l+1)*T[rt].setv; T[rt<<1|1].sum=1ll* (t[rt<<1|1].r-t[rt<<1|1].l+1)*T[rt].setv; T[rt].setv=INF; } if(t[rt].a1| |t[rt].d) {ll len1=t[rt<<1].r-t[rt<<1].l+1; ll Len2=t[rt<<1|1].r-t[rt<<1|1].l+1; ll TMP1=t[rt].a1; ll TMP2=tmp1+len1*T[RT].D; T[rt<<1].a1+=TMP1; T[rt<<1|1].a1+=TMP2; T[rt<<1].d+=T[RT].D; T[rt<<1|1].d+=T[RT].D; T[rt<<1].sum+=tmp1*len1+len1* (len1-1)/2*T[RT].D;//Arithmetic progression summationt[rt<<1|1].sum+=tmp2*len2+len2* (len2-1)/2*T[RT].D; T[RT].A1=t[rt].d=0; }}voidBuildintLintRintRT) {T[RT].L=l; T[RT].R=R; T[rt].sum=t[rt].a1=t[rt].d=0; T[rt].setv=INF; if(L==R)return; intM= (l+r) >>1; Build (Lson); Build (Rson); Pushup (RT);}voidUpdate_add (intLintRintRt,ll D) { if(l<=t[rt].l&&r>=T[RT].R) {LL A1= (d==1)? (t[rt].l-l+1):(r-t[rt].l+1);//determine the first itemll len=t[rt].r-t[rt].l+1; T[RT].A1+=A1; T[RT].D+=D; T[rt].sum+=a1*len+len* (len-1)/2*D; return; } pushdown (RT); intM= (T[RT].L+T[RT].R) >>1; if(l<=m) Update_add (l,r,rt<<1, D); if(r>m) Update_add (l,r,rt<<1|1, D); Pushup (RT);}voidUpdate_set (intLintRintrt,ll x) { if(l<=t[rt].l&&r>=T[RT].R) {T[rt].setv=x; T[RT].A1=t[rt].d=0; T[rt].sum=1ll* (t[rt].r-t[rt].l+1)*x; return; } pushdown (RT); intM= (T[RT].L+T[RT].R) >>1; if(l<=m) Update_set (l,r,rt<<1, x); if(r>m) Update_set (l,r,rt<<1|1, x); Pushup (RT);} ll query (intLintRintRT) { if(l<=t[rt].l&&r>=T[RT].R) { returnt[rt].sum; } pushdown (RT); ll Num=0; intM= (T[RT].L+T[RT].R) >>1; if(l<=m) Num+=query (l,r,rt<<1); if(r>m) Num+=query (l,r,rt<<1|1); returnnum;}intMain () {intN,TLL,TRR; ll X; Charop[3]; Build (1N1); scanf ("%d",&N); while(n--) {scanf ("%s%d%d",op,&tll,&TRR); if(op[0]=='A') {Update_add (TLL,TRR,1,1); } Else if(op[0]=='B') Update_add (TLL,TRR,1,-1); Else if(op[0]=='C') {scanf ("%lld",&x); Update_set (TLL,TRR,1, x); } Else if(op[0]=='S') {printf ("%lld\n", Query (TLL,TRR,1)); } }return 0;}
UVA 12436-rip Van Winkle ' s Code (segment tree interval update)