SPLAY 翻转,删除段,添加段操作
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 const int maxn = 300005; 6 int fa[maxn],son[2][maxn],val[maxn],siz[maxn],lz[maxn]; 7 int root,tot; 8 void init(){ 9 memset(fa,0,sizeof(fa)); 10 memset(son,0,sizeof(son)); 11 memset(val,0,sizeof(val)); 12 memset(siz,0,sizeof(siz)); 13 memset(lz,0,sizeof(lz)); 14 root = tot = 0; 15 } 16 void Ins(int x){ 17 if(root==0){ 18 root = ++tot; 19 siz[root] = 1; 20 return; 21 } 22 fa[root] = ++tot; 23 son[0][tot] = root; 24 siz[tot] = siz[root]+1; 25 root = tot; 26 } 27 void pushdown(int x){ 28 int l = son[0][x],r = son[1][x]; 29 if(lz[x]){ 30 swap(son[0][x],son[1][x]); 31 lz[l]^=1;lz[r]^=1; 32 lz[x] = 0; 33 } 34 } 35 void pushup(int x){ 36 siz[x] = siz[son[0][x]]+siz[son[1][x]]+1; 37 } 38 void rota(int w,int x){ 39 int y = fa[x]; 40 son[!w][y] = son[w][x]; 41 if(son[w][x])fa[son[w][x]] = y; 42 fa[x] = fa[y]; 43 if(fa[y])son[y==son[1][fa[y]]][fa[y]] = x; 44 fa[y] = x; 45 son[w][x] = y; 46 pushup(y);pushup(x); 47 } 48 void splay(int x,int y){ 49 while(fa[x]!=y){ 50 if(fa[fa[x]]==y)rota(x==son[0][fa[x]],x); 51 else { 52 int w = fa[x]==son[0][fa[fa[x]]]; 53 if(x==son[w][fa[x]]){ 54 rota(!w,x);rota(w,x); 55 } 56 else { 57 rota(w,fa[x]);rota(w,x); 58 } 59 } 60 } 61 if(y==0)root = x;///HERE ,x!!!! !y 62 } 63 int getk(int k){ 64 int x = root; 65 while(1){ 66 pushdown(x); 67 if(k==siz[son[0][x]]+1)return x; 68 if(k<=siz[son[0][x]])x = son[0][x]; 69 else { 70 k-=(siz[son[0][x]]+1); 71 x = son[1][x]; 72 } 73 } 74 } 75 int Del(int l,int r){ 76 int x = getk(l-1),y = getk(r+1); 77 splay(x,0);splay(y,x); 78 int t = son[0][y]; 79 son[0][y] = 0; 80 return t; 81 } 82 void rev(int l,int r){ 83 int x = getk(l-1),y = getk(r+1); 84 splay(x,0);splay(y,x); 85 lz[son[0][y]]^=1; 86 } 87 int main() 88 { 89 int n,m; 90 while(~scanf("%d%d",&n,&m)){ 91 if(n+m<0)break; 92 init(); 93 for(int i = 2;i<=n+1;++i)val[i] = i-1; 94 for(int i = 1;i<=n+2;i++)Ins(i); 95 while(m--){ 96 char s[10];int a,b,c; 97 scanf("%s%d%d",s,&a,&b); 98 if(s[0]==‘C‘){ 99 scanf("%d",&c); 100 int t = Del(a+1,b+1); 101 int x = getk(c+1),y = getk(c+2); 102 splay(x,0);splay(y,x); 103 son[0][y] = t; 104 fa[t] = y; 105 } 106 else rev(a+1,b+1); 107 } 108 for(int i = 1;i<n;++i)printf("%d ",val[getk(i+1)]); 109 printf("%d\n",val[getk(n+1)]); 110 } 111 return 0; 112 }
时间: 2024-10-11 10:25:54