Problem Description LXHGWW got a sequence contains n characters which is all ' 0 ' or ' 1 ' s.
We have the five operations here:
Change operations:
0 A B change all characters to ' 0 ' in [A, b]
1 A B change all characters to ' 1 ' in [A, b]
2 A B change all ' 0 ' to ' 1 ' and change all ' 1 ' to ' 0 ' s in [A, b]
Output Operations:
3 A B output the number of ' 1 ' s in [A, b]
4 A B output the length of the longest continuous ' 1 ' string in [A, b] typical water problem, for a 01 sequence of interval coverage and inversion, maintenance of 1, the number of consecutive 1, the number of prefixes 1, suffix 1 Number, and 0 consecutive, prefix 0, suffix 0, used to calculate the 1 reversal. But still wrote the wrong place, in the program is marked out. The code is as follows:
#include <iostream>#include<cstdio>#include<cstring>#defineLC Po*2#defineRC Po*2+1#defineLson L,M,LC#defineRson M+1,R,RCusing namespacestd;Const intmaxn=1e5+5;intsum[maxn*4],lsum[2][maxn*4],rsum[2][maxn*4],msum[2][maxn*4];intcol[maxn*4],xor[maxn*4];inlinevoidSwapint&a,int&b) { inttemp=A; A=b; b=temp;}voidPushup (intPointLen) {Sum[po]=sum[lc]+SUM[RC]; for(intI=0;i<2;++i) {Msum[i][po]=Max (MSUM[I][LC],MSUM[I][RC]); MSUM[I][PO]=max (msum[i][po],lsum[i][rc]+RSUM[I][LC]); LSUM[I][PO]=LSUM[I][LC]; if(lsum[i][lc]== (len-(len/2)) ) Lsum[i][po]+=LSUM[I][RC]; RSUM[I][PO]=RSUM[I][RC]; if(rsum[i][rc]== (len/2)) Rsum[i][po]+=RSUM[I][LC]; }}voidPushdown (intPointLen) { if(col[po]!=-1) {COL[LC]=col[rc]=Col[po]; XOR[LC]=xor[rc]=0; SUM[LC]=col[po]* (len-(len/2)); SUM[RC]=col[po]* (len/2); for(intI=0;i<2;++i) {MSUM[I][LC]=lsum[i][lc]=rsum[i][lc]= (i SUM[LC]: len-(len/2)-SUM[LC]); MSUM[I][RC]=rsum[i][rc]=lsum[i][rc]= (i SUM[RC]: (len/2)-SUM[RC]); } Col[po]=-1; } if(Xor[po]) {XOR[LC]=!XOR[LC]; XOR[RC]=!XOR[RC]; SUM[LC]=len-(len/2)-SUM[LC]; SUM[RC]= (len/2)-SUM[RC]; Swap (msum[0][lc],msum[1][LC]); Swap (msum[0][rc],msum[1][RC]); Swap (lsum[0][lc],lsum[1][LC]); Swap (rsum[0][lc],rsum[1][LC]); Swap (lsum[0][rc],lsum[1][RC]); Swap (rsum[0][rc],rsum[1][RC]); XOR[PO]=0; }}voidBuild_tree (intLintRintPO) {Col[po]=-1; XOR[PO]=0; if(l==R) {inttemp; scanf ("%d",&temp); SUM[PO]=temp; lsum[0][po]=rsum[0][po]=msum[0][po]=1-temp; lsum[1][po]=rsum[1][po]=msum[1][po]=temp; return; } intM= (L+R)/2; Build_tree (Lson); Build_tree (Rson); Pushup (Po,r-l+1);}voidUpdate_col (intUlinturintUtintLintRintPO) { if(ul<=l&&ur>=R) {Col[po]=ut; XOR[PO]=0; SUM[PO]=ut* (r-l+1); lsum[1][po]=rsum[1][po]=msum[1][po]=Sum[po]; lsum[0][po]=rsum[0][po]=msum[0][po]=r-l+1-Sum[po]; return; } pushdown (Po,r-l+1); intM= (L+R)/2; if(ul<=M) Update_col (Ul,ur,ut,lson); if(ur>M) Update_col (Ul,ur,ut,rson); Pushup (Po,r-l+1);}voidUpdate_xor (intUlinturintLintRintPO) { if(ul<=l&&ur>=R) {Xor[po]=!Xor[po]; SUM[PO]=r-l+1-Sum[po]; Swap (msum[0][po],msum[1][po]); Swap (lsum[0][po],lsum[1][po]); Swap (rsum[0][po],rsum[1][po]); return; } pushdown (Po,r-l+1); intM= (L+R)/2; if(ul<=M) Update_xor (Ul,ur,lson); if(ur>M) Update_xor (Ul,ur,rson); Pushup (Po,r-l+1);}intQuery_sum (intQlintQrintLintRintPO) { if(ql<=l&&qr>=R)returnSum[po]; Pushdown (Po,r-l+1); intM= (L+R)/2; if(qr<=M)returnquery_sum (Ql,qr,lson); if(ql>M)returnquery_sum (Ql,qr,rson); returnQuery_sum (Ql,qr,lson) +query_sum (Ql,qr,rson);}intQuery_max (intQlintQrintLintRintPO) { if(ql<=l&&qr>=R)returnmsum[1][po]; Pushdown (Po,r-l+1); intM= (L+R)/2; intans=0; if(qr<=M)returnQuery_max (Ql,qr,lson); if(ql>M)returnQuery_max (Ql,qr,rson); Ans=Max (Query_max (Ql,qr,lson), Query_max (Ql,qr,rson)); Ans=max (Ans,min (rsum[1][lc],m-ql+1) +min (lsum[1][RC],QR-M));//!!! returnans;}intMain () {intT; intn,m; intA,b,c; CIN>>T; while(t--) {scanf ("%d%d",&n,&M); Build_tree (0, N-1,1); while(m--) {scanf (" %d%d%d",&a,&b,&c); Switch(a) { Case 0: Update_col (B,c,0,0, N-1,1); Break; Case 1: Update_col (B,c,1,0, N-1,1); Break; Case 2: Update_xor (B,c,0, N-1,1); Break; Case 3: printf ("%d\n", Query_sum (B,c,0, N-1,1)); Break; Case 4: printf ("%d\n", Query_max (B,c,0, N-1,1)); Break; } } } return 0;}
View Code
Simple HDU 3397 Sequence operation, segment tree + interval merge.