1 #include<bits/stdc++.h>
2 using namespace std;
3 template <class _T> inline void read(_T &_x) {
4 int _t; bool flag = false;
5 while ((_t = getchar()) != ‘-‘ && (_t < ‘0‘ || _t > ‘9‘)) ;
6 if (_t == ‘-‘) _t = getchar(), flag = true; _x = _t - ‘0‘;
7 while ((_t = getchar()) >= ‘0‘ && _t <= ‘9‘) _x = _x * 10 + _t - ‘0‘;
8 if (flag) _x = -_x;
9 }
10 const int maxn = 70010;
11 struct Tnode {
12 int v, l, r;
13 };
14 namespace T {
15 Tnode t[maxn * 400];
16 int st[maxn * 400], top, tot;
17 inline int newnode() {
18 if (!top) st[top++] = ++tot;
19 int cur = st[--top];
20 t[cur].v = t[cur].l = t[cur].r = 0;
21 return cur;
22 }
23 void remove(int rt) {
24 if (!rt) return ;
25 remove(t[rt].l), remove(t[rt].r);
26 st[top++] = rt;
27 }
28 int merge(int a, int b) {
29 if (!a && !b) return 0;
30 int cur = newnode();
31 t[cur].v = t[a].v + t[b].v;
32 t[cur].l = merge(t[a].l, t[b].l);
33 t[cur].r = merge(t[a].r, t[b].r);
34 return cur;
35 }
36 void insert(int &rt, int l, int r, int pos, int val) {
37 if (!rt) rt = newnode(); t[rt].v += val;
38 if (l == r) return ;
39 int mid = (l + r) >> 1;
40 if (pos <= mid) insert(t[rt].l, l, mid, pos, val);
41 else insert(t[rt].r, mid + 1, r, pos, val);
42 if (!t[rt].v) remove(rt), rt = 0;
43 }
44 }
45 namespace S {
46 const double alpha = 0.7;
47 Tnode t[maxn * 2];
48 int st[maxn * 2], top, tot;
49 int root[maxn * 2], sz[maxn * 2]; //root for segtree
50 int Root, v[maxn * 2], cnt, badroot; //Root of Scapegoat
51 inline int newnode() {
52 if (!top) st[top++] = ++tot;
53 int cur = st[--top];
54 t[cur].v = t[cur].l = t[cur].r = 0;
55 return cur;
56 }
57 void remove(int rt) {
58 if (!rt) return ;
59 remove(t[rt].l), remove(t[rt].r);
60 T::remove(root[rt]);
61 st[top++] = rt;
62 }
63 inline bool isbad(int rt) {
64 return sz[rt] * alpha < max(sz[t[rt].l], sz[t[rt].r]);
65 }
66 void print(int rt) {
67 if (!rt) return ;
68 print(t[rt].l);
69 cout << t[rt].v << ‘ ‘ ;
70 print(t[rt].r);
71 }
72 void Print() {
73 cout << "Seq: " ; print(Root);
74 cout << endl;
75 }
76 void insert(int &rt, int pos, int val) { // insert val after pos
77 if (!rt) rt = newnode(), sz[rt] = 0, t[rt].v = val;
78 ++sz[rt];
79 T::insert(root[rt], 0, maxn, val, 1);
80 if (sz[rt] != 1) {
81 if (pos <= sz[t[rt].l]) insert(t[rt].l, pos, val);
82 else insert(t[rt].r, pos - sz[t[rt].l] - 1, val);
83 if (isbad(rt)) badroot = rt;
84 }
85 }
86 void dfs_node(int rt) {
87 if (!rt) return ;
88 dfs_node(t[rt].l);
89 v[++cnt] = t[rt].v;
90 dfs_node(t[rt].r);
91 }
92 void build(int &rt, int l, int r) {
93 rt = newnode();
94 int mid = (l + r) >> 1;
95 t[rt].v = v[mid], sz[rt] = r - l + 1;
96 if (l < mid) build(t[rt].l, l, mid - 1);
97 if (mid < r) build(t[rt].r, mid + 1, r);
98 root[rt] = T::merge(root[t[rt].l], root[t[rt].r]);
99 T::insert(root[rt], 0, maxn, t[rt].v, 1);
100 }
101 void rebuild(int &rt) {
102 cnt = 0, dfs_node(rt);
103 remove(rt);
104 build(rt, 1, cnt);
105 }
106 void init(int n) {
107 for (int i = 1; i <= n; ++i) read(v[i]);
108 build(Root, 1, n);
109 }
110 void Insert(int pos, int val) { // insert before pos & rebuild
111 //printf("Insert %d before position %d\n", val, pos);
112 badroot = 0;
113 insert(Root, pos - 1, val);
114 if (badroot) rebuild(badroot);
115 }
116 int change(int &rt, int pos, int val) {
117 int ret = -1;
118 if (pos == sz[t[rt].l] + 1) {
119 ret = t[rt].v, t[rt].v = val;
120 goto Modify_part;
121 }
122 if (pos <= sz[t[rt].l]) ret = change(t[rt].l, pos, val);
123 else ret = change(t[rt].r, pos - sz[t[rt].l] - 1, val);
124 Modify_part:T::insert(root[rt], 0, maxn, val, 1),
125 T::insert(root[rt], 0, maxn, ret, -1);
126 return ret;
127 }
128 void Change(int pos, int val) {
129 //printf("Change position %d‘s value to %d\n", pos, val);
130 change(Root, pos, val);
131 }
132 struct Pnode {
133 int v, rt;
134 Pnode() {}
135 Pnode(int a, int b):v(a), rt(b) {}
136 }p[maxn * 2];
137 int pcnt;
138 void dfs_part(int rt, int pos) {
139 if (pos == 0) return ;
140 if (pos == sz[t[rt].l]) {
141 p[++pcnt] = Pnode(1, root[t[rt].l]);
142 } else if (pos > sz[t[rt].l]) {
143 p[++pcnt] = Pnode(1, root[rt]);
144 p[++pcnt] = Pnode(-1, root[t[rt].r]);
145 dfs_part(t[rt].r, pos - sz[t[rt].l] - 1);
146 } else {
147 dfs_part(t[rt].l, pos);
148 }
149 }
150 int Query(int l, int r, int k) {
151 //printf("Query %d%s smallest in range [%d, %d]\n", k, k%10 == 1 ? "st" : (k%10 == 2 ? "nd" : (k%10 == 3 ? "rd" : "th")), l, r);
152 pcnt = 0;
153 dfs_part(Root, l - 1);
154 for (int i = 1; i <= pcnt; ++i) p[i].v = -p[i].v;
155 dfs_part(Root, r);
156 int L = 0, R = maxn, mid, totv;
157 while (L < R) {
158 mid = (L + R) >> 1, totv = 0;
159 for (int i = 1; i <= pcnt; ++i) totv += p[i].v * T::t[T::t[p[i].rt].l].v;
160 //cout << mid << ‘ ‘ << tot << endl;
161 if (totv >= k) {
162 for (int i = 1; i <= pcnt; ++i) p[i].rt = T::t[p[i].rt].l;
163 R = mid;
164 } else {
165 for (int i = 1; i <= pcnt; ++i) p[i].rt = T::t[p[i].rt].r;
166 k -= totv, L = mid + 1;
167 }
168 }
169 return L;
170 }
171 }
172 using S::Print;
173 int n, q;
174 int main() {
175 //freopen("3065.in", "r", stdin);
176 //freopen("3065.out", "w", stdout);
177 read(n);
178 S::init(n);
179 //Print();
180 read(q);
181 char op[20];
182 int lastans = 0;
183 for (int i = 1, a, b, c; i <= q; ++i) {
184 scanf("%s", op);
185 switch (op[0]) {
186 case ‘Q‘:
187 read(a), read(b), read(c);
188 a ^= lastans, b ^= lastans, c ^= lastans;
189 printf("%d\n", lastans = S::Query(a, b, c));
190 break;
191 case ‘M‘:
192 read(a), read(b);
193 a ^= lastans, b ^= lastans;
194 S::Change(a, b);
195 break;
196 case ‘I‘:
197 read(a), read(b);
198 a ^= lastans, b ^= lastans;
199 S::Insert(a, b);
200 break;
201 }
202 //Print();
203 }
204 return 0;
205 }