1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <algorithm>
5 #include <queue>
6 #include <set>
7 #include <vector>
8 #include <string.h>
9 #define siji(i,x,y) for(int i=(x);i<=(y);++i)
10 #define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
11 #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
12 #define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
13 #define inf 0x1f1f1f1f
14 #define ivorysi
15 #define mo 97797977
16 #define hash 974711
17 #define base 47
18 #define MAXN 100005
19 #define fi first
20 #define se second
21 #define pii pair<int,int>
22 using namespace std;
23 typedef long long ll;
24 struct node {
25 int size,son[2],fa;
26 void clear() {
27 size=son[0]=son[1]=fa=0;
28 }
29 }tree[MAXN];
30 int root;
31 //维护size
32 void update(int x,int c) {
33 int k=tree[x].size;
34 int o=tree[x].fa;
35 tree[x].size=tree[o].size;
36 tree[o].size=tree[o].size-k+tree[tree[x].son[c]].size;
37 }
38 void rotate(int x,int c) {
39 update(x,c);
40 int o=tree[x].fa;
41 if(tree[o].fa!=0) {
42 int t= o==tree[tree[o].fa].son[0] ? 0 : 1;
43 tree[tree[o].fa].son[t]=x;
44 }
45 tree[o].son[c^1]=tree[x].son[c];
46 if(tree[o].son[c^1])tree[tree[o].son[c^1]].fa=o;
47 tree[x].son[c]=o;
48 tree[x].fa=tree[o].fa;
49 tree[o].fa=x;
50
51 }
52 void splay(int x,int y) {
53 while(tree[x].fa!=y) {
54 int k1= x==tree[tree[x].fa].son[0] ? 1 : 0;
55 int k2= tree[x].fa == tree[tree[tree[x].fa].fa].son[0] ? 1 : 0;
56 if(tree[tree[x].fa].fa!=y && k2==k1) {
57 rotate(tree[x].fa,k1);
58 rotate(x,k1);
59 }
60 else {
61 rotate(x,k1);
62 }
63 }
64 if(y==0) root=x;
65 else {
66
67 }
68 }
69 int find(int x,int c) {
70 splay(x,0);
71 int t=tree[x].son[c];
72 while(tree[t].son[c^1]!=0) {
73 t=tree[t].son[c^1];
74 }
75 return t;
76 }
77 void delete_x(int prev,int next) {
78 if(prev!=0 && next!=0) {
79 splay(prev,0);
80 splay(next,prev);
81 tree[tree[root].son[1]].son[0]=0;
82 --tree[tree[root].son[1]].size;
83 }
84 else if(prev!=0) {
85 splay(prev,0);
86 tree[root].son[1]=0;
87 }
88 else {
89 splay(next,0);
90 tree[root].son[0]=0;
91 }
92 --tree[root].size;
93 }
94 //0是top 1是bottom
95 void in_hurry(int x,int c) {
96 int pr=find(x,0),ne=find(x,1);
97 delete_x(pr,ne);
98 int t=root;
99 ++tree[t].size;
100 tree[x].son[c^1]=tree[t].son[c];
101 tree[tree[t].son[c]].fa=x;
102 tree[x].size=tree[tree[t].son[c]].size+1;
103 tree[x].fa=t;
104 tree[t].son[c]=x;
105 }
106 int ask(int x) {
107 splay(x,0);
108 return tree[tree[x].son[0]].size;
109 }
110 void insert(int x,int c) {
111 if(c==0) return;
112 if(ask(x)==0 && c==-1) return;
113 int pr=find(x,0),ne=find(x,1);
114 delete_x(pr,ne);
115 int t= c==-1 ? pr : ne;
116 splay(t,0);
117 c= c==-1 ? 0 : 1;
118 ++tree[t].size;
119 tree[x].clear();
120 tree[x].son[c]=tree[t].son[c];
121 tree[tree[t].son[c]].fa=x;
122 tree[x].size=tree[tree[t].son[c]].size+1;
123 tree[t].son[c]=x;
124 tree[x].fa=t;
125 splay(x,0);
126 }
127
128 void query(int x,int s) {
129 int temp=tree[tree[x].son[0]].size+1;
130 if(s==temp) {splay(x,0);return;}
131 int t=s<temp ? 0 : 1;
132 s = s>=temp ? s-temp : s;
133 query(tree[x].son[t],s);
134 }
135 int n,m;
136 void init() {
137 scanf("%d%d",&n,&m);
138 int a=0,b;
139 siji(i,1,n) {
140 scanf("%d",&b);
141 tree[b].fa=a;
142 if(a!=0) {
143 tree[a].son[1]=b;
144 }
145 else {root=b;}
146 tree[b].size=n-i+1;
147 a=b;
148 }
149
150 }
151 void solve() {
152 init();
153 char ord[15];
154 int s,t;
155 siji(i,1,m) {
156 scanf("%s",ord);
157 if(ord[0]==‘T‘ || ord[0]==‘B‘ ) {
158 t=ord[0]==‘T‘ ? 0 : 1;
159 scanf("%d",&s);
160 in_hurry(s,t);
161 }
162 else if(ord[0]==‘I‘) {
163 scanf("%d%d",&s,&t);
164 insert(s,t);
165 }
166 else if(ord[0]==‘A‘){
167 scanf("%d",&s);
168 printf("%d\n",ask(s));
169 }
170 else if(ord[0]==‘Q‘) {
171 scanf("%d",&s);
172 query(root,s);
173 printf("%d\n",root);
174 }
175 }
176 }
177 int main(int argc, char const *argv[])
178 {
179 #ifdef ivorysi
180 freopen("f1.in","r",stdin);
181 #endif
182 solve();
183 }