1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
| #include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <map>
using namespace std;
typedef long long LL;
const int INF = 1009999999;
int getint() {
int r = 0, k = 1; char c = getchar();
for (; '0' > c || c > '9'; c = getchar()) if (c == '-') k = -1;
for (; '0' <= c && c <= '9'; c = getchar()) r = r * 10 - '0' + c;
return r * k;
}
int n, m;
const int maxn = 10005;
const int size = 20105;
int tot = 0, siz[size * 250], lc[size * 250], rc[size * 250], root[maxn], A[maxn];
inline int lowbit(int x) { return x & (-x); }
void del(int &x, int y, int l, int r, int pos) {
x = ++tot;
siz[x] = siz[y] - 1; lc[x] = lc[y]; rc[x] = rc[y];
if (l == r) return;
int mid = (l + r) >> 1;
if (pos <= mid) del(lc[x], lc[y], l, mid, pos);
else del(rc[x], rc[y], mid + 1, r, pos);
}
void ins(int &x, int y, int l, int r, int pos) {
x = ++tot;
siz[x] = siz[y] + 1; lc[x] = lc[y]; rc[x] = rc[y];
if (l == r) return;
int mid = (l + r) >> 1;
if (pos <= mid) ins(lc[x], lc[y], l, mid, pos);
else ins(rc[x], rc[y], mid + 1, r, pos);
}
void DELETE(int pos, int val) {
for (int i = pos; i <= n; i += lowbit(i)) del(root[i], root[i], 1, size, val);
}
void INSERT(int pos, int val) {
for (int i = pos; i <= n; i += lowbit(i)) ins(root[i], root[i], 1, size, val);
}
int x[35], xcnt;
int y[35], ycnt;
int query(int l, int r, int k) {
if (l == r) return l;
int ls = 0, mid = (l + r) >> 1;
for (int i = 0; i < xcnt; ++i) ls += siz[lc[x[i]]];
for (int i = 0; i < ycnt; ++i) ls -= siz[lc[y[i]]];
if (k <= ls) {
for (int i = 0; i < xcnt; ++i) x[i] = lc[x[i]];
for (int i = 0; i < ycnt; ++i) y[i] = lc[y[i]];
query(l, mid, k);
} else {
for (int i = 0; i < xcnt; ++i) x[i] = rc[x[i]];
for (int i = 0; i < ycnt; ++i) y[i] = rc[y[i]];
query(mid + 1, r, k - ls);
}
}
int QUERY(int l, int r, int k) {
xcnt = ycnt = 0;
for (int i = r; i; i -= lowbit(i)) x[xcnt++] = root[i];
for (int j = l - 1; j; j -= lowbit(j)) y[ycnt++] = root[j];
return query(1, size, k);
}
int data[maxn][4], value[size];
struct Treap {
int fa[size], c[size][2], rd[size], val[size], root, total, clock, id[size];
void init() {
root = total = clock = 0;
}
void rotate(int x) {
int y = fa[x], z = fa[y], a = (c[y][1] == x), b = a ^ 1;
if (z) c[z][c[z][1]==y]=x;
fa[x]=z;fa[y]=x;fa[c[x][b]]=y;
c[y][a]=c[x][b];c[x][b]=y;
fa[0]=0;
}
void insert(int &now, int key, int father) {
if (!now) {
now = ++total;
fa[total] = father;
val[total] = key;
rd[total] = rand() << 15 ^ rand();
c[total][0] = c[total][1] = 0;
while (fa[total] && rd[total] > rd[fa[total]]) rotate(total);
if (!fa[total]) root = total;
return;
}
if (val[now] == key) return;
if (key < val[now]) insert(c[now][0], key, now);
else insert(c[now][1], key, now);
}
void DFS(int x) {
if (!x) return;
DFS(c[x][0]);
id[x] = ++clock;
value[clock] = val[x];
DFS(c[x][1]);
}
int find(int x, int key) {
if (val[x] == key) return id[x];
if (key < val[x]) return find(c[x][0], key);
return find(c[x][1], key);
}
int operator [](int x) {
return find(root, x);
}
} M;
int main() {
M.init();
srand(20000926);
n = getint(); m = getint();
for (int i = 1; i <= n; ++i) {
A[i] = getint();
M.insert(M.root, A[i], 0);
}
char str[10];
int a, b, c;
for (int i = 1; i <= m; ++i) {
scanf("%s", str);
if (str[0] == 'Q') {
a = getint(); b = getint(); c = getint();
data[i][0] = 1; data[i][1] = a; data[i][2] = b; data[i][3] = c;
} else {
a = getint(); b = getint();
data[i][0] = 0; data[i][1] = a; data[i][2] = b;
M.insert(M.root, b, 0);
}
}
int T = 0;
M.DFS(M.root);
for (int i = 1; i <= n; ++i) { INSERT(i, M[A[i]]); }
for (int i = 1; i <= m; ++i) {
if (data[i][0]) {
a = data[i][1]; b = data[i][2]; c = data[i][3];
printf("%d\n", value[QUERY(a, b, c)]);
} else {
a = data[i][1]; b = data[i][2];
DELETE(a, M[A[a]]);
INSERT(a, M[b]);
A[a] = b;
}
}
return 0;
}
|