//by Hzwer
1#include <iostream>2#include <cstdio>3 4 using namespacestd;5 6InlineintRead ()7 {8 intx=0, f=1;CharCh=GetChar ();9 while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}Ten while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} One returnx*F; A } - - intN,m,sz,rt; the intfa[100005],c[100005][2],id[100005]; - intsize[100005]; - BOOLrev[100005]; - + voidPushup (intk) - { + intl=c[k][0],r=c[k][1]; Asize[k]=size[l]+size[r]+1; at } - - voidPushdown (intk) - { - intl=c[k][0],r=c[k][1]; - in if(Rev[k]) - { toSwap (c[k][0],c[k][1]); +rev[l]^=1; rev[r]^=1; -rev[k]=0; the } * } $ Panax Notoginseng voidRotateintXint&k) - { the inty=fa[x],z=Fa[y],l,r; + A if(c[y][0]==X) l=0;ElseL=1; r=l^1; the if(y==k) k=x; + Else{if(c[z][0]==y) c[z][0]=x;Elsec[z][1]=x;} - $fa[x]=z;fa[y]=x;fa[c[x][r]]=y; $c[y][l]=c[x][r];c[x][r]=y; - pushup (y);p ushup (x); - } the - voidSplay (intXint&k)Wuyi { the while(x!=k) - { Wu inty=fa[x],z=Fa[y]; - if(y!=k) About { $ if((c[y][0]==x) ^ (c[z][0]==y)) Rotate (x,k); - Elserotate (y,k); - } - A rotate (x,k); + } the } - $ intFindintKintrank) the { the Pushdown (k); the intl=c[k][0],r=c[k][1]; the - if(size[l]+1==rank)returnK; in Else if(Size[l]>=rank)returnfind (L,rank); the Else returnFind (r,rank-size[l]-1); the } About the voidRever (intLintR) the { the intX=find (rt,l), Y=find (rt,r+2); +Splay (X,RT); Splay (y,c[x][1]); - intz=c[y][0]; therev[z]^=1;Bayi } the the voidBuildintLintRintf) - { - if(L>r)return; the intnow=id[l],last=Id[f]; the the if(l==R) the { -fa[now]=last;size[now]=1; the if(l<f) c[last][0]=Now ; the Elsec[last][1]=Now ; the return;94 } the the intMid= (l+r) >>1; now=Id[mid]; the 98Build (l,mid-1, mid); Build (mid+1, r,mid); Aboutfa[now]=Last;pushup (mid); - 101 if(mid<f) c[last][0]=Now ;102 Elsec[last][1]=Now ;103 }104 the intMain ()106 {107N=read (); m=read ();108 109 for(intI=1; i<=n+2; i++) theid[i]=++sz;111 theBuild1, n+2,0); Rt= (n+3) >>1;113 the for(intI=1; i<=m;i++) the { the intL=read (), r=read ();117 Rever (l,r);118 }119 - for(intI=2; i<=n+1; i++)121printf"%d", find (Rt,i)-1);122 123 return 0;124}
[bzoj3223] Literary balance tree [splay]