bzoj1861 [Zjoi2006]Book 书架

题目链接

调了好久,要崩溃了。

splay入门题

注意虚拟结点

并不知道为什么加了空间回收就要T三组???

  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>
 10 #include<stack>
 11 #include<map>
 12 #include<set>
 13 #define rre(i,r,l) for(int i=(r);i>=(l);i--)
 14 #define re(i,l,r) for(int i=(l);i<=(r);i++)
 15 #define Clear(a,b) memset(a,b,sizeof(a))
 16 #define inout(x) printf("%d",(x))
 17 #define douin(x) scanf("%lf",&x)
 18 #define strin(x) scanf("%s",(x))
 19 #define LLin(x) scanf("%lld",&x)
 20 #define op operator
 21 #define CSC main
 22 typedef unsigned long long ULL;
 23 typedef const int cint;
 24 typedef long long LL;
 25 using namespace std;
 26 void inin(int &ret)
 27 {
 28     ret=0;int f=0;char ch=getchar();
 29     while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();}
 30     while(ch>='0'&&ch<='9')ret*=10,ret+=ch-'0',ch=getchar();
 31     ret=f?-ret:ret;
 32 }
 33 int ch[160016][2],w[160016],s[160016],ed,fa[160016],wei[160016],root;
 34 void maintain(int k){if(k)s[k]=s[ch[k][0]]+s[ch[k][1]]+1;}
 35 void rotate(int x)
 36 {
 37     int y=fa[x],z=fa[y];
 38     if(z)ch[z][ch[z][1]==y]=x;
 39     int l=ch[y][1]==x,r=l^1;
 40     fa[y]=x,fa[x]=z;
 41     if(ch[x][r])fa[ch[x][r]]=y;
 42     ch[y][l]=ch[x][r],ch[x][r]=y;
 43     maintain(y);
 44 }
 45 int newnode(int v,int f)
 46 {
 47     int ret=++ed;
 48     s[ret]=1,w[ret]=v;fa[ret]=f;
 49     ch[ret][0]=ch[ret][1]=0;
 50     wei[v]=ret;
 51     return ret;
 52 }
 53 int com(int k,int x)
 54 {
 55     int pp=s[ch[k][0]]+1;
 56     if(x==pp)return -1;
 57     return x<pp?0:1;
 58 }
 59 void splay(int x,int f)
 60 {
 61     if(!x)return ; 
 62     while(fa[x]!=f)
 63     {
 64         int y=fa[x],z=fa[y];
 65         if(z!=f)
 66             if((ch[z][1]==y)^(ch[y][1]==x))rotate(x);
 67             else rotate(y);else ;
 68         rotate(x);
 69     }
 70     maintain(x);
 71     if(!f)root=x;
 72 }
 73 int kth(int k)
 74 {
 75     int x=root;
 76     while(x)
 77     {
 78         int pp=s[ch[x][0]]+1;
 79         if(pp==k)return x;
 80         if(pp<k)k-=pp,x=ch[x][1];
 81         else x=ch[x][0];
 82     }return 0;
 83 }
 84 int a[80080];
 85 int build(int f,int l,int r)
 86 {
 87     if(l>r)return 0;
 88     int mid=(l+r)>>1;
 89     int k=newnode(a[mid],f);
 90     ch[k][0]=build(k,l,mid-1);
 91     ch[k][1]=build(k,mid+1,r);
 92     maintain(k);return k;
 93 }
 94 int split(int l,int r)
 95 {
 96     int x=kth(l-1),y=kth(r+1);
 97     splay(x,0),splay(y,x);
 98     return ch[y][0];
 99 }
100 int find(int k,int x)
101 {
102     if(!k)return 0;
103     int pp=s[ch[k][0]];
104     if(x>pp+1)return find(ch[k][1],x-pp-1);
105     if(x<=pp)return find(ch[k][0],x);
106     else return k;
107 }
108 void Top(int x)
109 {
110     int L=find(root,2);
111     splay(L,0);
112     ch[ch[root][0]][1]=wei[x];fa[wei[x]]=ch[root][0];
113     ch[wei[x]][0]=ch[wei[x]][1]=0;
114     maintain(wei[x]),maintain(ch[root][0]),maintain(root);
115 }
116 void B(int x)
117 {
118     int L=find(root,s[root]-1);
119     splay(L,0);
120     ch[ch[root][1]][0]=wei[x];fa[wei[x]]=ch[root][1];
121     ch[wei[x]][0]=ch[wei[x]][1]=0;
122     maintain(wei[x]);maintain(ch[root][1]);maintain(root);
123 }
124 int maxi(int x)
125 {
126     if(!x)return 0;
127     while(ch[x][1])x=ch[x][1];
128     return x;
129 }
130 void del(int x)
131 {
132     splay(wei[x],0);splay(maxi(ch[root][0]),0);
133     fa[ch[wei[x]][1]]=root;ch[root][1]=ch[wei[x]][1];
134     maintain(root);
135 }
136 int n,m;
137 void print(int x)
138 {
139     if(!x)return ;
140     print(ch[x][0]);
141     printf("%d ",w[x]);
142     print(ch[x][1]);
143 }
144 int CSC()
145 {
146     freopen("in.in","r",stdin);
147     freopen("out.out","w",stdout);
148     inin(n),inin(m);
149     re(i,1,n)inin(a[i]);
150     root=build(0,0,n+1);
151     char opt[22];
152     re(i,1,m)
153     {
154         strin(opt);int x;inin(x);
155         if(opt[0]=='T')
156         {
157             del(x);
158             Top(x);
159         }
160         if(opt[0]=='B')
161         {
162             del(x);
163             B(x);
164         }
165         if(opt[0]=='I')
166         {
167             int t;inin(t);
168             if(!t)continue;
169             int k=wei[x];
170             splay(k,0);
171             int rank=s[ch[k][0]]+t;
172             del(x);
173             int L=find(root,rank),R=find(root,rank+1);
174             splay(L,0);splay(R,root);
175             ch[ch[root][1]][0]=wei[x];fa[wei[x]]=ch[root][1];
176             ch[wei[x]][0]=ch[wei[x]][1]=0;
177             maintain(wei[x]);maintain(ch[root][1]);maintain(root);
178         }
179         if(opt[0]=='A')
180         {
181             int k=wei[x];
182             splay(k,0);
183             int kk=s[ch[k][0]]-1;
184             printf("%d\n",kk);
185         }
186         if(opt[0]=='Q')
187         {
188             int k=find(root,x+1);
189             splay(k,0);
190             printf("%d\n",w[root]);
191         }
192 //        print(root);cout<<"\n";
193     }
194     return 0;
195 }

 

posted @ 2016-01-25 08:54  HugeGun  阅读(124)  评论(0编辑  收藏  举报