Hey, heh. Splay template questions.
#include <iostream>#include<cstdio>#include<cstring>#defineMAXV 100500using namespacestd;inttree[maxv][3],size[maxv],fath[maxv],rev[maxv],n,m,l,r,root;voidPushup (intNow ) { intls=tree[now][1],rs=tree[now][2]; Size[now]=size[ls]+size[rs]+1;}voidBuildintLeftintRightintfather) { if(Left>right)return; intnow=Left ; if(left==Right ) {Size[now]=1; fath[now]=father; if(Now<father) tree[father][1]=Now ; Elsetree[father][2]=Now ; return; } intMid= (left+right) >>1; now=mid; Fath[now]=father; if(Now<father) tree[father][1]=Now ; Elsetree[father][2]=Now ; Build (Left,mid-1, mid); Build (Mid+1, Right,mid); Pushup (now);}voidPushdown (intNow ) { if(rev[now]!=0) {Swap (tree[now][1],tree[now][2]); rev[tree[now][1]]^=1; rev[tree[now][2]]^=1; Rev[now]=0; }}intFindintRankintNow ) {pushdown (now); intls=tree[now][1],rs=tree[now][2]; if(Size[ls]>=rank)returnfind (RANK,LS); Else if(rank>size[ls]+1)returnFind (rank-size[ls]-1, RS); Else returnNow ;}voidRotateintXint&k) { inty=fath[x],z=Fath[y],l,r; if(tree[y][1]==X) l=1;ElseL=2; R=3-l; if(y==k) k=x; Else { if(tree[z][1]==y) tree[z][1]=x; Elsetree[z][2]=x; } Fath[x]=z;fath[y]=x;fath[tree[x][r]]=y; TREE[Y][L]=tree[x][r];tree[x][r]=y; Pushup (y);p ushup (x);}voidSplay (intXint&k) { while(x!=k) {inty=fath[x],z=Fath[y]; if(y!=k) {if((tree[y][1]==x) ^ (tree[z][1]==y)) rotate (x,k); Elserotate (y,k); } rotate (x,k); }}voidRever (intLeftintRight ) { intX=find (Left,root), Y=find (right+2, Root); Splay (X,root); Splay (y,tree[x][2]); intr=tree[y][1];rev[r]^=1;}intMain () {scanf ("%d%d",&n,&m); Root= (n+3) >>1; Build (1, n+2,0); for(intI=1; i<=m;i++) {scanf ("%d%d",&l,&R); Rever (L,R); } for(intI=2; i<=n+1; i++) printf ("%d", find (I,root)-1); return 0;}
Bzoj 3223 Literary Balance Tree