Topic links
Balance Tree series The second question
Xuan God's split is still so good to write%%%
Still need to pay attention to the virtual node.
1#include <algorithm>2#include <iostream>3#include <cstdlib>4#include <cstring>5#include <cstdio>6#include <string>7#include <cmath>8#include <ctime>9#include <queue>Ten#include <stack> One#include <map> A#include <Set> - #defineRre (i,r,l) for (int i= (r); i>= (l); i--) - #defineRe (I,L,R) for (int i= (l); i<= (R); i++) the #defineClear (A, B) memset (A,b,sizeof (a)) - #defineInOut (x) printf ("%d", (x)) - #defineDouin (x) scanf ("%lf", &x) - #defineStrin (x) scanf ("%s", (x)) + #defineLLin (x) scanf ("%lld", &x) - #defineOP operator + #defineCSC Main Atypedef unsignedLong LongULL; attypedefConst intCInt; -typedefLong LongLL; - using namespacestd; - voidInin (int&ret) - { -ret=0;intf=0;CharCh=GetChar (); in while(ch<'0'|| Ch>'9'){if(ch=='-') f=1; ch=GetChar ();} - while(ch>='0'&&ch<='9') ret*=Ten, ret+=ch-'0', ch=GetChar (); toret=f?-Ret:ret; + } - intch[100010][2],w[100010],s[100010],rev[100010],fa[100010],ed,root; the voidMaintain (intk) {s[k]=s[ch[k][0]]+s[ch[k][1]]+1;} * intNewNodeintVintf) $ {Panax Notoginsenged++; -w[ed]=v,fa[ed]=F; thech[ed][0]=ch[ed][1]=0; +s[ed]=1; rev[ed]=0; A returnEd; the } + voidDownintk) - { $ if(Rev[k]) $ { -Swap (ch[k][0],ch[k][1]); -rev[ch[k][0]]^=1, rev[ch[k][1]]^=1; therev[k]=0; - }Wuyi } the voidRotateintx) - { Wu inty=fa[x],z=Fa[y]; - if(z) ch[z][ch[z][1]==y]=x; About intl=ch[y][1]==x,r=l^1; $fa[y]=x,fa[x]=Z; - if(Ch[x][r]) fa[ch[x][r]]=y; -ch[y][l]=ch[x][r],ch[x][r]=y; - maintain (y); A } + intsta[100010],top; the voidSplay (intKintf) - { $top=0; the for(intI=k;i!=f;i=fa[i]) sta[++top]=i; the while(top) Down (sta[top--]); the while(fa[k]!=f) the { - inty=fa[k],z=Fa[y]; in if(z!=f) the if((ch[z][1]==y) ^ (ch[y][1]==k)) Rotate (k); the ElseRotate (y);Else ; About rotate (k); the } the maintain (k); the if(!f) root=K; + } - inttot,a[100010]; the intKthintk)Bayi { the intx=Root; the while(x) - { - Down (x); the intpp=s[ch[x][0]]+1; the if(pp==k)returnx; the if(pp<k) k-=pp,x=ch[x][1]; the Elsex=ch[x][0]; -}return 0; the } the intBuildintFintLintR) the {94 if(L>r)return 0; the intMid= (l+r) >>1; the intk=NewNode (a[mid],f); thech[k][0]=build (k,l,mid-1);98ch[k][1]=build (k,mid+1, R); AboutMaintain (k);returnK; - }101 intSplitintLintR)102 {103 intX=kth (L), y=kth (r+2);104Splay (x,0); splay (y,x); the returnch[y][0];106 }107 intn,m;108 voidPrintintk)109 { the if(!k)return ;111 Down (k); thePrint (ch[k][0]);113printf"%d", W[k]); thePrint (ch[k][1]); the } the intCSC ()117 {118 inin (n), Inin (m);119Re (I,1, n+2) a[i]=i-1; -Root=build (0,1, n+2);121Re (I,1, M)122 {123 intL,r;124 Inin (L), Inin (R); the intHh=split (l,r);126rev[hh]^=1;127 } - intHh=split (1, n);129 print (HH); the return 0;131}
bzoj3223 tyvj 1729 Literary Balance Tree