1 #include<cstdio>
2 #include<iostream>
3 using namespace std;
4
5 const int MAXN=80005;
6 struct node
7 {
8 int d,size;
9 node *parent,*ch[2];
10 }pool[MAXN],*root,*rf,*tmp;
11 int cnt,pos[MAXN];
12 int size(node *p)
13 {
14 if(p) return p->size;
15 return 0;
16 }
17 void update(node *p)
18 {
19 p->size=1+size(p->ch[0])+size(p->ch[1]);
20 }
21 void rotate(node *p,int type)
22 {
23 node *parent=p->parent,*son=p->ch[!type],*gp=p->parent->parent;
24 parent->ch[type]=son;
25 if(son) son->parent=parent;
26 p->ch[!type]=parent;
27 parent->parent=p;
28 p->parent=gp;
29 gp->ch[parent==gp->ch[1]]=p;
30 update(parent);
31 update(p);
32 if(parent==root) root=p;
33 }
34 void splay(node *p,node *target)
35 {
36 while(p->parent!=target)
37 {
38 if(p->parent->parent==target)
39 rotate(p,p==p->parent->ch[1]);
40 else
41 {
42 node *parent=p->parent,*gp=p->parent->parent;
43 int f=parent==gp->ch[0];
44 if(p==parent->ch[f]) rotate(p,f),rotate(p,!f);
45 else rotate(parent,!f),rotate(p,!f);
46 }
47 }
48 }
49 node *find(node *p,int k)
50 {
51 if(size(p->ch[0])>=k) return find(p->ch[0],k);
52 else if(size(p->ch[0])==k-1) return p;
53 else return find(p->ch[1],k-1-size(p->ch[0]));
54 }
55 void insert(int x,node *newnode)
56 {
57 node *p=find(root,x);
58 splay(p,rf);
59 p=find(root,x+1);
60 splay(p,root);
61 p->ch[0]=newnode;
62 newnode->parent=p;
63 update(p);
64 update(p->parent);
65 }
66 void del(int x)
67 {
68 node *p=find(root,x-1);
69 splay(p,rf);
70 p=find(root,x+1);
71 splay(p,root);
72 p->ch[0]=NULL;
73 update(p);
74 update(p->parent);
75 }
76 int Get_Rank(node *p)
77 {
78 int ret=size(p->ch[0]);
79 while(p!=root)
80 {
81 if(p==p->parent->ch[1]) ret+=1+size(p->parent->ch[0]);
82 p=p->parent;
83 }
84 return ret+1;
85 }
86
87 int main()
88 {
89 int n,m,i,x,y,k,a;
90 char ch[10];
91 scanf("%d%d",&n,&m);
92
93 rf=&pool[++cnt];
94 root=&pool[++cnt];
95 root->d=0;root->size=1;
96 root->parent=rf;rf->ch[0]=root;
97 for(i=1;i<=n;i++)
98 {
99 scanf("%d",&a);
100 pos[a]=++cnt;
101 tmp=&pool[pos[a]];
102 tmp->d=a;tmp->size=1;
103 tmp->parent=root;root->ch[1]=tmp;
104 splay(tmp,rf);
105 }
106 tmp=&pool[++cnt];
107 tmp->d=0;tmp->size=1;
108 tmp->parent=root;root->ch[1]=tmp;
109 splay(tmp,rf);
110
111 while(m --> 0)
112 {
113 scanf("%s",ch);
114 if(ch[0]==‘T‘)
115 {
116 scanf("%d",&x);
117 tmp=&pool[pos[x]];
118 del(Get_Rank(tmp));
119 insert(1,tmp);
120 }
121 else if(ch[0]==‘B‘)
122 {
123 scanf("%d",&x);
124 tmp=&pool[pos[x]];
125 del(Get_Rank(tmp));
126 insert(n,tmp);
127 }
128 else if(ch[0]==‘I‘)
129 {
130 scanf("%d%d",&x,&y);
131 tmp=&pool[pos[x]];
132 k=Get_Rank(tmp);
133 del(k);
134 insert(k-1+y,tmp);
135 }
136 else if(ch[0]==‘A‘)
137 {
138 scanf("%d",&x);
139 tmp=&pool[pos[x]];
140 printf("%d\n",Get_Rank(tmp)-2);
141 }
142 else
143 {
144 scanf("%d",&x);x++;
145 printf("%d\n",find(root,x)->d);
146 }
147 }
148
149 return 0;
150 }