The code problem is pain.
My idea is to double expand after the line tree maintenance, double expansion after the operation of print and swap for both sides to do once, adjusted for half a day
The first two operations can not move the line tree and the coordinates of his inquiry into the correct coordinates can be
Code:
#include <bits/stdc++.h>using namespacestd;#defineMAXN 500005intn,c,cl[maxn*2];intQ,tf,tv;Charqe[4];inlineintTran (intx) {returnTf? ((n (X+TV))%n+n)%n: (X+TV)%N;}namespacesegmenttree{Const intL=0, r=1; structnode{intson[2],be,nd,cs,Set; }X[MAXN*4]; intsz=1; InlinevoidUpdata (intnum) {x[num].be=x[x[num].son[l]].be; X[num].nd=x[x[num].son[r]].nd; X[num].cs=x[x[num].son[l]].cs+X[x[num].son[r]].cs; if(x[x[num].son[l]].nd==x[x[num].son[r]].be) x[num].cs--; } InlinevoidSet (intNumintTC) {x[num].be=x[num].nd=x[num].Set=tc;x[num].cs=1; } InlinevoidMaintain (intnum) {Set (X[num].son[l],x[num].Set); Set (X[num].son[r],x[num].Set); X[num].Set=0; } node Merge (node A,node b) {node ret; ret.be=a.be;ret.nd=b.nd; Ret.cs=a.cs+B.cs; if(a.nd==b.be) ret.cs--; returnret; } void_init (intLintRintnum) { if(l==r) {x[num].be=x[num].nd=Cl[l]; X[num].cs=1;return; } intMid=l+r>>1; X[NUM].SON[L]=++sz;x[num].son[r]=++sz; _init (L,mid,x[num].son[l]); _init (Mid+1, R,x[num].son[r]); Updata (num); } voidPrint (intLintRintnlintNrintNumintTC) { if(L==NL&&R==NR) {Set (NUM,TC);return;} intMid=l+r>>1; if(X[num].Set) maintain (num); if(Nl>mid) Print (mid+1, R,NL,NR,X[NUM].SON[R],TC); Else if(nr<=mid) Print (L,MID,NL,NR,X[NUM].SON[L],TC); Else{Print (L,MID,NL,MID,X[NUM].SON[L],TC); Print (Mid+1, r,mid+1, NR,X[NUM].SON[R],TC); } updata (num); } node Tqurey (intLintRintnlintNrintnum) { if(L==NL&&R==NR)returnX[num]; intMid=l+r>>1; node ret; if(Nl>mid) Ret=tqurey (mid+1, R,nl,nr,x[num].son[r]); Else if(Nr<=mid) ret=Tqurey (L,mid,nl,nr,x[num].son[l]); ElseRet=merge (Tqurey (L,mid,nl,mid,x[num].son[l]), Tqurey (mid+1, r,mid+1, Nr,x[num].son[r])); if(X[num].Set) {Ret.be=ret.nd=x[num].Set; ret.cs=1;} returnret; } intTColor (intPintLintRintnum) { if(L==R)returnx[num].be; if(X[num].Set)returnX[num].Set; intMid=l+r>>1; if(P>mid)returnTColor (p,mid+1, R,x[num].son[r]); Else returnTColor (P,l,mid,x[num].son[l]); } InlineintQurey (intLintRBOOLCR) { if(l>r) Swap (L,R); Node ans=tqurey (0,2*n-1, L,r,1); if(cr&&ans.be==ans.nd&&ans.cs>1) ans.cs--; returnAns.cs; } InlineintColor (intp) {returnTColor (P,0,2*n-1,1); }}#defineST SegmenttreeintMain () {scanf ("%d%d",&n,&c); for(intI=0; i<n;i++) scanf ("%d",&Cl[i]); for(intI=0; i<n;i++) cl[n+i]=Cl[i]; St::_init (0, (n<<1)-1,1); scanf ("%d",&q); for(intA,b,c,i=1; i<=q;i++) {scanf ("%s", QE); if(qe[0]=='R') {scanf ("%d", &a); tv= ((tv-a)%n+n)%N;} Else if(qe[0]=='F') {tf^=1; tv= ((-TV)%n+n)%N;} Else if(qe[0]=='C') { if(qe[1]=='S') {scanf ("%d%d", &a,&b); a--;b--; A=tran (a); B=tran (b);if(Tf) swap (A, b); if(b<a) b+=N; printf ("%d\n", St::qurey (A, B,0)); } Elseprintf"%d\n", St::qurey (0, N-1,1)); } Else if(qe[0]=='P') {scanf ("%d%d%d", &a,&b,&c); a--;b--; A=tran (a); B=tran (b);if(Tf) swap (A, b); if(b<a) b+=N; ST::P rint (0,2*n-1, A, B,1, c); if(b+n<2*n) ST::P rint (0,2*n-1, A+n,b+n,1, c); Else{ST::P rint (0,2*n-1, A+n,2*n-1,1, c); ST::P rint (0,2*n-1,0, (b+n)%n,1, c); } } Else if(qe[0]=='S') {scanf ("%d%d", &a,&b); a--;b--; A=tran (a); b=Tran (b); intT1=st::color (a), t2=St::color (b); ST::P rint (0,2*n-1, A,a,1, T2);if(a+n<2*n) ST::P rint (0,2*n-1, A+n,a+n,1, T2); ST::P rint (0,2*n-1, B,b,1, T1);if(b+n<2*n) ST::P rint (0,2*n-1, B+n,b+n,1, T1); } /*for (int i=0;i<2*n;i++) {printf ("%d", St::color (Tran (i))); } cout<<endl; */ } return 0;}#undefSt/*7 3 3 3 3 1 1100FR 2P 1 7 1*/
Bzoj1493--noi2007 Necklace Factory