Segment tree.
Really is a line tree.
With the exception of operations, the line tree is easy to handle, and the problem is how to handle operations 1 and 2. (This is not expected).
We use a Delta maintenance Operation 1, +k if there is no rotation, or-K.
Each time I and J are read into the process with trans, the success in the O (1) time resolved the operation 1 and 2.
Details are important.
#include <cstdio>#include<algorithm>#include<cstring>using namespacestd;Const intMAXN =2000000+Ten;structNode {intlc,rc,cnt; };structSegtree {#defineLC (x) ((x) <<1)#defineRC (x) (((x) <<1) |intL[MAXN],R[MAXN],TAG[MAXN]; Node A[MAXN]; intrev,delta,n,tmp; voidTransint&x,int&y) {if(rev) {//Reverse RotationX= (n-x+2-delta+n)%N; Y= (n-y+2-delta+n)%N; Swap (x, y); } Else{//Positive Spinx= (x-delta+n)%N; Y= (y-delta+n)%N; } x=x?x:n; Y=y?y:n; } voidPushintx) {if(!tag[x])return; A[LC (x)].lc=A[RC (x)].lc=Tag[x]; A[LC (x)].rc=A[RC (x)].rc=Tag[x]; TAG[LC (x)]=TAG[RC (x)]=Tag[x]; A[LC (x)].cnt=A[RC (x)].cnt=1; TAG[X]=0; } Node Update (node A,node b) {node res; RES.LC=a.lc; Res.rc=b.rc; Res.cnt=a.cnt+b.cnt-(a.rc==b.lc); returnRes; } voidUpdateintx) {a[x]=Update (A[LC (x)],A[RC (x)]); } voidBuildintXintLintR) {l[x]=l; r[x]=s; if(l==R) {scanf ("%d",&tmp); A[X].LC=a[x].rc=tmp; A[x].cnt=1; return; } intMid= (l+r) >>1; Build (LC (x), l,mid); Build (RC (x), Mid+1, R); Update (x); } voidColorintXintLintRintv) {if(L>r[x] | | R<L[X])return; if(L<=l[x] && r[x]<=R) {tag[x]=v; A[X].LC=a[x].rc=v; A[x].cnt=1; return; } push (x); Color (LC (x), l,r,v); Color (RC (x), l,r,v); Update (x); } Node Query (intXintLintR) {if(L<=l[x] && r[x]<=r)returnA[x]; intMid= (L[x]+r[x]) >>1; push (x); if(R<=mid)returnQuery (LC (x), l,r); Else if(L>mid)returnQuery (RC (x), l,r); Else returnUpdate (query (LC (x), l,r), query (RC (x), l,r)); } voidPawsintIintj) {trans (i,j); Node cur;intCI,CJ; if(i<=j) {cur=query (1, i,j); CI=CUR.LC; cj=cur.rc; } Else{cur=query (1, J,i); CJ=CUR.LC; Ci=cur.rc; } Color (1, I,I,CJ); Color (1, J,J,CI); } voidrotate () {intK; scanf ("%d",&k); if(rev) Delta= (Delta-k+n)%n;//Reverse Rotation ElseDelta= (delta+k)%n;//Positive Spin } voidPaintintIintJintv) {trans (i,j); if(i<=j) Color (1, i,j,v); Else{color (1, i,n,v); Color (1,1, j,v); } } voidC1 () {Node cur; Cur=query (1,1, N); printf ("%d\n", Max (cur.cnt-(cur.lc==cur.rc),1)); } voidC2 (intIintj) {trans (i,j); Node cur; if(I<=J) Cur=query (1, i,j); ElseCur=update (Query (1, i,n), query (1,1, J)); printf ("%d\n", cur.cnt); } voidinit () {intC; scanf ("%d%d",&n,&c); Rev=delta=0; Build (1,1, N); }}seg;Charop[ -];intk,i,j,x,q;intMain () {seg.init (); scanf ("%d",&q); while(q--) {scanf ("%s", op); if(op[0]=='R') seg.rotate (); Else if(op[0]=='F') seg.rev^=1; Else if(op[0]=='S') {scanf ("%d%d",&i,&j); Seg.paws (I,J); } Else if(op[0]=='P') {scanf ("%d%d%d",&i,&j,&x); Seg.paint (I,J,X); } Else if(op[0]=='C'&& op[1]=='S') {scanf ("%d%d",&i,&j); SEG.C2 (I,J); } Elseseg.c1 (); } return 0;}
1493: [NOI2007] Necklace Factory