这个题和维修数列一比就简单了许多,只要把所谓的光标处理好就OK 了,就是字符串处理的时候比较麻烦,需要多调试一下。这个题不用回收空间了,不过插入的时候还是要递归建树再插入,这样可以防止树的深度过大导致不平衡使得splay效率变差。

editor
  1 #include<iostream>
  2 #include<cstdio>
  3 #include<algorithm>
  4 #include<cmath>
  5 #include<cstring>
  6 #define maxn 2400000
  7 #define inf 2147483646
  8 using namespace std;
  9 int c[maxn][2];
 10 int fa[maxn],size[maxn],rev[maxn];
 11 char s[maxn],ch[maxn];
 12 int n,m,num,rot;
 13 
 14 inline void update(int x)
 15 {
 16         if (!x) return ;
 17         size[x]=size[c[x][0]]+size[c[x][1]]+1;
 18 }
 19 
 20 inline void reverse(int x)
 21 {
 22         if (!x) return ;
 23         swap(c[x][0],c[x][1]);
 24         rev[x]^=1;
 25 }
 26 
 27 inline void down(int x)
 28 {
 29         if (!x) return ;
 30         if (rev[x]) 
 31         {
 32                 reverse(c[x][0]);
 33                 reverse(c[x][1]);
 34                 rev[x]=0;
 35         }
 36 }
 37 
 38 inline void relax(int x,int rot)
 39 {
 40         if (x!=rot) relax(fa[x],rot);
 41         down(x);
 42 }
 43 
 44 inline void rotate(int x,int &rot)
 45 {
 46         int y=fa[x],z=fa[y];
 47         int p=(c[y][1]==x),q=p^1;
 48         if (y==rot) rot=x;
 49         else if (c[z][0]==y) c[z][0]=x; else c[z][1]=x;
 50         fa[x]=z; fa[y]=x; fa[c[x][q]]=y;
 51         c[y][p]=c[x][q]; c[x][q]=y;
 52         update(y);
 53 }
 54 
 55 inline void splay(int x,int &rot)
 56 {
 57         relax(x,rot);
 58         while (x!=rot)
 59         {
 60                 int y=fa[x],z=fa[y];
 61                 if (y!=rot)
 62                         if ((c[z][0]==y)xor(c[y][0]==x)) rotate(x,rot);else rotate(y,rot);
 63                 rotate(x,rot);
 64         }
 65         update(x);
 66 }
 67 
 68 inline int find(int t,int k)
 69 {
 70         down(t);
 71         if (k==size[c[t][0]]+1) return t;
 72         if (k<size[c[t][0]]+1) return find(c[t][0],k);
 73         if (k>size[c[t][0]]+1) return find(c[t][1],k-size[c[t][0]]-1);
 74 }
 75 
 76 inline int build(int l,int r)
 77 {
 78         int mid=(l+r)>>1,left=0,right=0;
 79         if (l<mid)      left=build(l,mid-1);
 80         int t=++num;
 81         s[t]=ch[mid];
 82         if (r>mid)      right=build(mid+1,r);
 83         if (left)       fa[left]=t,c[t][0]=left; 
 84         if (right) fa[right]=t,c[t][1]=right;
 85         update(t);
 86         return t;
 87 }
 88 
 89 int main()
 90 {
 91         //freopen("editor2.in","r",stdin);
 92         //freopen("editor.out","w",stdout);
 93         scanf("%d",&n);
 94         char sign[10];
 95         num=2; 
 96         s[1]=0; c[1][1]=2;  size[1]=2;
 97         s[2]=0; fa[2]=1; size[2]=1;
 98         rot=1;
 99         int x,m,l,r,now=0;
100         for (int i=1;i<=n;i++)
101         {
102                 scanf("%s",&sign);
103                 //cout<<sign<<endl;
104                 //for (int j=1;j<=num;j++) cout<<s[find(rot,j)];
105                 //cout<<endl;
106                 if (sign[0]=='M') 
107                 {
108                         scanf("%d",&x);
109                         now=x;
110                 }
111                 if (sign[0]=='I')
112                 {
113                         scanf("%d",&m);
114                         l=find(rot,now+1); r=find(rot,now+2);
115                         splay(r,rot); splay(l,c[rot][0]);
116                         for (int j=0;j<=m-1;j++) while ((ch[j]=getchar())==10);
117                                 //scanf("%c",&ch[j]);
118                         //      cout<<ch<<endl;
119                         int t=build(0,m-1);
120                         c[l][1]=t;
121                         fa[t]=l;
122                         update(l);
123                 }
124                 if (sign[0]=='D')
125                 {
126                         scanf("%d",&x);
127                         l=find(rot,now+1); r=find(rot,now+x+2);
128                         splay(r,rot); splay(l,c[rot][0]);
129                         fa[c[l][1]]=0;
130                         c[l][1]=0;
131                         update(l);
132                 }
133                 if (sign[0]=='R')
134                 {
135                         scanf("%d",&x);
136                         l=find(rot,now+1); r=find(rot,now+x+2);
137                         splay(r,rot); splay(l,c[rot][0]);
138                         reverse(c[l][1]);
139                 }
140                 if (sign[0]=='G')
141                 {
142                         printf("%c\n",s[find(rot,now+2)]);
143                 }
144                 if (sign[0]=='P') now-=1;
145                 if (sign[0]=='N') now+=1;
146         }
147         return 0;
148 }
149                         

 

相关文章:

  • 2021-09-26
  • 2022-02-13
  • 2022-12-23
  • 2021-06-29
  • 2021-12-27
  • 2021-11-16
  • 2021-12-20
  • 2021-11-26
猜你喜欢
  • 2022-02-28
  • 2022-01-06
  • 2022-12-23
  • 2021-09-22
  • 2021-05-11
  • 2021-11-12
  • 2021-08-31
相关资源
相似解决方案