BZOJ - 3223 Tyvj 1729 文艺平衡树 (splay/无旋treap)
splay:
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int N=1e5+10,inf=0x3f3f3f3f; 5 int ch[N][2],val[N],siz[N],rev[N],tot,n,m,a[N],rt; 6 int newnode(int x) {int u=++tot; val[u]=x,siz[u]=1,ch[u][0]=ch[u][1]=rev[u]=0; return u;} 7 void pu(int u) {siz[u]=siz[ch[u][0]]+siz[ch[u][1]]+1;} 8 void pd(int u) {if(rev[u])rev[u]=0,swap(ch[u][0],ch[u][1]),rev[ch[u][0]]^=1,rev[ch[u][1]]^=1;} 9 void rot(int& u,int f) { 10 int v=ch[u][f]; 11 ch[u][f]=ch[v][f^1],ch[v][f^1]=u; 12 pu(u),pu(v),u=v; 13 } 14 void splay(int& u,int k) { 15 pd(u); 16 if(siz[ch[u][0]]+1!=k) { 17 int f=k>siz[ch[u][0]]+1; 18 if(f)k-=siz[ch[u][0]]+1; 19 int& v=ch[u][f]; 20 pd(v); 21 if(siz[ch[v][0]]+1!=k) { 22 int ff=k>siz[ch[v][0]]+1; 23 if(ff)k-=siz[ch[v][0]]+1; 24 splay(ch[v][ff],k),f==ff?rot(u,f):rot(v,ff); 25 } 26 rot(u,f); 27 } 28 } 29 void sp(int& u,int k,int& v) {splay(u,k),v=ch[u][1],ch[u][1]=0,pu(u);} 30 void mg(int& u,int v) {splay(u,siz[u]),ch[u][1]=v,pu(u);} 31 void rv(int& u,int l,int r) { 32 int lv,rv; 33 sp(u,r,rv),sp(u,l-1,lv); 34 rev[lv]^=1; 35 mg(u,lv),mg(u,rv); 36 } 37 #define mid ((l+r)>>1) 38 void build(int& u,int l=0,int r=n) { 39 if(l>r) {u=0; return;} 40 u=newnode(a[mid]); 41 build(ch[u][0],l,mid-1),build(ch[u][1],mid+1,r),pu(u); 42 } 43 int f; 44 void dfs(int u) { 45 if(!u)return; 46 pd(u); 47 dfs(ch[u][0]); 48 if(val[u]!=0) { 49 f?f=0:printf(" "); 50 printf("%d",val[u]); 51 } 52 dfs(ch[u][1]); 53 } 54 int main() { 55 scanf("%d%d",&n,&m); 56 for(int i=1; i<=n; ++i)a[i]=i; 57 a[0]=0,build(rt); 58 while(m--) { 59 int l,r; 60 scanf("%d%d",&l,&r); 61 rv(rt,l+1,r+1); 62 } 63 f=1,dfs(rt); 64 return 0; 65 }
无旋treap(FHQ-treap):
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int N=1e5+10,inf=0x3f3f3f3f; 5 int ch[N][2],val[N],siz[N],rd[N],rev[N],tot,n,m,a[N],rt; 6 #define l(u) ch[u][0] 7 #define r(u) ch[u][1] 8 int newnode(int x) {int u=++tot; val[u]=x,siz[u]=1,rd[u]=rand(),l(u)=r(u)=rev[u]=0; return u;} 9 void flip(int u) {rev[u]^=1,swap(l(u),r(u));} 10 void pu(int u) {siz[u]=siz[l(u)]+siz[r(u)]+1;} 11 void pd(int u) {if(rev[u])rev[u]=0,flip(l(u)),flip(r(u));} 12 void sp(int& u,int k,int& v) { 13 if(!u) {v=0; return;} 14 pd(u); 15 if(k>=siz[l(u)]+1)sp(r(u),k-(siz[l(u)]+1),v),pu(u); 16 else v=u,u=l(u),sp(u,k,l(v)),pu(v); 17 } 18 void mg(int& u,int v) { 19 if(!u||!v) {u=u|v; return;} 20 if(rd[u]>rd[v])pd(u),mg(r(u),v); 21 else pd(v),mg(u,l(v)),l(v)=u,u=v; 22 pu(u); 23 } 24 void rv(int& u,int l,int r) { 25 int lv,rv; 26 sp(u,r,rv),sp(u,l-1,lv); 27 flip(lv); 28 mg(u,lv),mg(u,rv); 29 } 30 int f; 31 void dfs(int u) { 32 if(!u)return; 33 pd(u); 34 dfs(l(u)); 35 if(val[u]!=0) { 36 f?f=0:printf(" "); 37 printf("%d",val[u]); 38 } 39 dfs(r(u)); 40 } 41 int main() { 42 srand(time(0)); 43 scanf("%d%d",&n,&m); 44 rt=newnode(0); 45 for(int i=1; i<=n; ++i)mg(rt,newnode(i)); 46 while(m--) { 47 int l,r; 48 scanf("%d%d",&l,&r); 49 rv(rt,l+1,r+1); 50 } 51 f=1,dfs(rt); 52 return 0; 53 }