5 Operations 0 1 then XOR or it seems that the 2 updates have been confusing every time.
Overwrite with overlay can be different or
And I don't know why.
#include <stdio.h>#include<string.h>#include<algorithm>using namespacestd;#defineMAXN 100010structnode{intl,r,ls,rs,ms;//left consecutive 1 right upintLz,rz,mz; 0intXor,cov; XOR/Lazyintsum; and}X[MAXN<<3];intZ[MAXN];voidMakexor (inta)//0 1 swap {swap (X[A].LS,X[A].LZ); Swap (X[A].RS,X[A].RZ); Swap (X[A].MS,X[A].MZ); X[a].sum=x[a].r-x[a].l+1-x[a].sum;}voidPUSH_UP (inta) { intlen=x[a].r-x[a].l+1; X[a].ls=x[a<<1].ls; X[a].rs=x[a<<1|1].rs; x[a].ms=max (x[a<<1].ms,x[a<<1|1].ms); if(x[a].ls== (len+1) >>1)//This is the normal interval merging x[a].ls+=x[a<<1|1].ls; if(x[a].rs==len>>1) x[a].rs+=x[a<<1].rs; intMid= (X[A].R+X[A].L) >>1;//if (z[mid]==z[mid+1]&&z[mid]==1)//At the beginning here has been wrong actually back updated no need to consider thisX[a].ms=max (x[a].ms,x[a<<1].rs+x[a<<1|1].ls); X[a].lz=x[a<<1].lz; X[a].rz=x[a<<1|1].rz; X[a].mz=max (x[a<<1].mz,x[a<<1|1].MZ); if(x[a].lz== (len+1) >>1) X[a].lz+=x[a<<1|1].lz; if(x[a].rz==len>>1) X[a].rz+=x[a<<1].rz; X[a].mz=max (x[a].mz,x[a<<1].rz+x[a<<1|1].lz); X[a].sum=x[a<<1].sum+x[a<<1|1].sum;}voidPush_down (inta) { if(x[a].cov!=-1) { intL=X[A].L; intR=X[A].R; intlen=r-l+1; X[a<<1].cov=x[a<<1|1].cov=X[a].cov; X[a<<1]. xor=x[a<<1|1]. Xor=0; X[a<<1].ls=x[a<<1].rs=x[a<<1].ms=x[a].cov? (len+1) >>1:0; X[a<<1].lz=x[a<<1].rz=x[a<<1].mz=x[a].cov?0:(len+1) >>1; X[a<<1].sum=x[a].cov? (len+1) >>1:0; X[a<<1|1].ls=x[a<<1|1].rs=x[a<<1|1].ms=x[a].cov?len>>1:0; X[a<<1|1].lz=x[a<<1|1].rz=x[a<<1|1].mz=x[a].cov?0:len>>1; X[a<<1|1].sum=x[a].cov?len>>1:0; X[a].cov=-1; } if(X[a]. XOR) {X[a]. XOR=0; X[a<<1]. xor^=1; X[a<<1|1]. xor^=1; Makexor (A<<1); Makexor (A<<1|1); }}voidBuild (intLintRinta) {X[A].L=l; X[A].R=0; X[a]. XOR=0; X[a].cov=-1; if(l==r) {x[a].ls=x[a].rs=x[a].ms= (z[l]==1); X[a].lz=x[a].rz=x[a].mz= (z[l]==0); X[a].sum=Z[l]; X[a].cov=Z[l]; return ; } intMid= (l+r) >>1; Build (L,mid,a<<1); Build (Mid+1,r,a<<1|1); Push_up (a);}voidUpdateintLintRintA1,intB1,intWinta) {Push_down (a); if(a1<=l&&r<=B1) { if(w<2) { intlen=r-l+1; X[a].cov=W; X[a].ls=x[a].rs=x[a].ms=w?len:0; X[a].lz=x[a].rz=x[a].mz=w?0: Len; X[a].sum=w?len:0; X[a]. XOR=0; } Else{X[a]. XOR=1; Makexor (a); } return ; } intMid= (l+r) >>1; if(a1<=mid) Update (L,mid,a1,b1,w,a<<1); if(b1>mid) Update (mid+1,r,a1,b1,w,a<<1|1); Push_up (a);}intQuery_sum (intLintRintLintRintRT) { if(l<=l&&r<=R) {returnx[rt].sum; } push_down (RT); intM= (l+r) >>1; intans=0; if(l<=m) ans+=query_sum (l,r,l,m,rt<<1); if(m<R) ans+=query_sum (l,r,m+1,r,rt<<1|1); returnans;}intQuery_len (intLintRintLintRintRT) {push_down (RT); if(l<=l&&r<=R) {returnx[rt].ms; } intM= (l+r) >>1; intans=0; if(l<=m) ans=max (Ans,query_len (l,r,l,m,rt<<1)); if(m<R) ans=max (Ans,query_len (l,r,m+1,r,rt<<1|1)); returnMax (Ans,min (m-l+1,x[rt<<1].rs) +min (r-m,x[rt<<1|1].ls));}intMain () {intT; scanf ("%d",&t); while(t--) { intn,m; scanf ("%d%d",&n,&m); for(intI=1; i<=n;i++) scanf ("%d",&Z[i]); Build (1N1); while(m--) { intop,a,b; scanf ("%d%d%d",&op,&a,&b); A++; b++; if(op<=2) Update (1, N,a,b,op,1); Else if(op==3) printf ("%d\n", Query_sum (A, B,1N1)); Elseprintf ("%d\n", Query_len (A, B,1N1)); } } return 0;}
Segment Tree HDU 3397 (True)