1 #include<bits/stdc++.h>
2 using namespace std;
3 int read(){
4 int x=0,f=1;char ch=getchar();
5 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
6 while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
7 return x*f;
8 }
9 #define N 80005
10 int n,m,a[N],pos[N],root,sz[N],ch[N][2],fa[N];
11 void pushup(int x){
12 sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;
13 }
14 void build(int l,int r,int f){
15 int mid=l+r>>1;
16 sz[mid]=1;fa[mid]=f;
17 if(mid<f)ch[f][0]=mid;else ch[f][1]=mid;
18 if(l==r)return;
19 if(l<mid)build(l,mid-1,mid);
20 if(r>mid)build(mid+1,r,mid);
21 pushup(mid);
22 }
23 void rotate(int x,int &beto){
24 int y=fa[x],t=ch[y][1]==x;
25 if(y!=beto)ch[fa[y]][ch[fa[y]][1]==y]=x;else beto=x;
26 fa[x]=fa[y];fa[y]=x;
27 fa[ch[x][!t]]=y;
28 ch[y][t]=ch[x][!t];
29 ch[x][!t]=y;
30 pushup(y);pushup(x);
31 }
32 void splay(int x,int &beto){
33 while(x!=beto){
34 int y=fa[x];
35 if(y!=beto){
36 if((ch[fa[y]][1]==y)==(ch[y][1]==x))rotate(y,beto);
37 else rotate(x,beto);
38 }rotate(x,beto);
39 }
40 }
41 int find_k(int x,int k){
42 if(sz[ch[x][0]]==k-1)return x;
43 if(sz[ch[x][0]]<k-1)return find_k(ch[x][1],k-sz[ch[x][0]]-1);
44 return find_k(ch[x][0],k);
45 }
46 void del(int x){
47 int l=find_k(root,x-1),r=find_k(root,x+1);
48 splay(l,root);splay(r,ch[l][1]);
49 x=ch[r][0];ch[r][0]=0;
50 pushup(r);pushup(l);
51 }
52 int main(){
53 n=read();m=read();n+=2;
54 for(int i=2;i<n;i++)a[i]=read(),pos[a[i]]=i;
55 build(1,n,0);root=(1+n)>>1;
56 char s[10];int x;
57 for(int i=1;i<=m;i++){
58 scanf("%s",s);x=read();
59 if(s[0]==‘T‘){
60 int p=pos[x],rk;
61 splay(p,root);rk=sz[ch[p][0]]+1;
62 del(rk);
63 int l=find_k(root,1),r=find_k(root,2);
64 splay(l,root);splay(r,ch[l][1]);
65 ch[r][0]=p;fa[p]=r;
66 pushup(r);pushup(l);
67 }
68 else if(s[0]==‘B‘){
69 int p=pos[x],rk;
70 splay(p,root);rk=sz[ch[p][0]]+1;
71 del(rk);
72 int l=find_k(root,n-2),r=find_k(root,n-1);
73 splay(l,root);splay(r,ch[l][1]);
74 ch[r][0]=p;fa[p]=r;
75 pushup(r);pushup(l);
76 }
77 else if(s[0]==‘I‘){
78 int t=read();
79 int p=pos[x],rk;
80 splay(p,root);rk=sz[ch[p][0]]+1;
81 del(rk);
82 int l=find_k(root,rk+t-1),r=find_k(root,rk+t);
83 splay(l,root);splay(r,ch[l][1]);
84 ch[r][0]=p;fa[p]=r;
85 pushup(r);pushup(l);
86 }
87 else if(s[0]==‘A‘){
88 int p=pos[x],rk;
89 // cout<<"p "<<p<<endl;
90 splay(p,root);rk=sz[ch[p][0]]+1;
91 // cout<<"rk "<<rk<<endl;
92 printf("%d\n",rk-2);
93 }
94 else if(s[0]==‘Q‘){
95 int p=find_k(root,x+1);
96 printf("%d\n",a[p]);
97 }
98 }
99 return 0;
100 }