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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
| #include "ybwhead/ios.h"
const int maxn = 5e6 + 10;
struct fhq_treap
{
struct node
{
int ch[2], val;
unsigned int pri, siz;
};
static int tot;
static node p[maxn];
int root;
void update(int x)
{
p[x].siz = p[p[x].ch[0]].siz + p[p[x].ch[1]].siz + 1;
}
int new_node(int v)
{
p[++tot].siz = 1;
p[tot].val = v;
p[tot].pri = rand() * rand();
return tot;
}
int merge(int x, int y)
{
if (!x || !y)
return x + y;
if (p[x].pri < p[y].pri)
{
p[x].ch[1] = merge(p[x].ch[1], y);
update(x);
// cout << p[p[x].ch[1]].siz<<' '<<p[p[x]] << endl;
return x;
}
else
{
p[y].ch[0] = merge(x, p[y].ch[0]);
update(y);
// cout << p[y].siz << endl;
return y;
}
}
void split(int now, int k, int &x, int &y)
{
if (!now)
{
x = y = 0;
return;
}
if (p[now].val <= k)
x = now, split(p[now].ch[1], k, p[now].ch[1], y);
else
y = now, split(p[now].ch[0], k, x, p[now].ch[0]);
update(now);
}
int kth(int now, int k)
{
while (1)
{
if (k <= p[p[now].ch[0]].siz)
now = p[now].ch[0];
else if (k == p[p[now].ch[0]].siz + 1)
return now;
else
k -= p[p[now].ch[0]].siz + 1, now = p[now].ch[1];
}
}
void insert(int x)
{
int xx, yy;
split(root, x, xx, yy);
root = merge(merge(xx, new_node(x)), yy);
// cout << p[root].siz << endl;
}
void erase(int x)
{
int xx, yy, zz;
split(root, x, xx, yy);
split(xx, x - 1, xx, zz);
zz = merge(p[zz].ch[0], p[zz].ch[1]);
root = merge(merge(xx, zz), yy);
}
int rank(int x)
{
int xx, yy;
split(root, x - 1, xx, yy);
int zz = p[xx].siz + 1;
root = merge(xx, yy);
return zz;
}
int kkth(int x)
{
// cout << root << endl;
return p[kth(root, x)].val;
}
int lower(int x)
{
int xx, yy;
// cout << x << endl;
split(root, x - 1, xx, yy);
// puts("1");
// cout << kth(xx, p[xx].siz) << endl;
int zz = p[kth(xx, p[xx].siz)].val;
root = merge(xx, yy);
return zz;
}
int upper(int x)
{
int xx, yy;
split(root, x, xx, yy);
int zz = p[kth(yy, 1)].val;
// cout << kth(yy, 1) << endl;
root = merge(xx, yy);
return zz;
}
};
struct Seg
{
struct node
{
fhq_treap x;
int ls, rs;
} x[maxn >> 2];
int tot;
void insert(int &p, int d, int y, int l = 1, int r = 1e9)
{
if (!p)
p = ++tot;
x[p].x.insert(y);
if (l == r)
return;
int mid = (l + r) >> 1;
if (d <= mid)
insert(x[p].ls, d, y, l, mid);
else
insert(x[p].rs, d, y, mid + 1, r);
}
void erase(int p, int d, int y, int l = 1, int r = 1e9)
{
x[p].x.erase(y);
if (l == r)
return;
int mid = (l + r) >> 1;
if (d <= mid)
erase(x[p].ls, d, y, l, mid);
else
erase(x[p].rs, d, y, mid + 1, r);
}
int kth(int p, int ll, int rr, int l_, int r_, int l = 1, int r = 1e9)
{
if (!p)
return 0;
if (l == ll && rr == r)
{
int yy = x[p].x.rank(r_ + 1) - x[p].x.rank(l_);
return yy;
}
int mid = l + r >> 1;
if (rr <= mid)
return kth(x[p].ls, ll, rr, l_, r_, l, mid);
else if (ll > mid + 1)
return kth(x[p].rs, ll, rr, l_, r_, mid + 1, r);
else
return kth(x[p].ls, ll, mid, l_, r_, l, mid) + kth(x[p].rs, mid + 1, rr, l_, r_, mid + 1, r);
}
int rank(int p, int k, int ll, int rr, int l = 1, int r = 1e9)
{
if (l == r)
return l;
int ls = x[x[p].ls].x.rank(rr + 1) - x[x[p].ls].x.rank(ll);
// yout << ls << ' ' << k << " " << l << " " << r << endl;
int mid = l + r >> 1;
if (ls < k)
return rank(x[p].rs, k - ls, ll, rr, mid + 1, r);
else
return rank(x[p].ls, k, ll, rr, l, mid);
}
} seg;
int root;
int n, m;
int a[maxn >> 2];
int fhq_treap::tot = 0;
fhq_treap::node fhq_treap::p[maxn];
int main()
{
yin >> n >> m;
for (int i = 1; i <= n; i++)
{
yin >> a[i];
seg.insert(root, a[i], i);
}
while (m--)
{
int opt, l, r, k;
yin >> opt >> l >> r;
if (opt == 1)
{
yin >> k;
if (!k)
puts("0");
else
yout << seg.kth(root, 0, k - 1, l, r) + 1 << endl;
}
if (opt == 2)
{
yin >> k;
yout << seg.rank(root, k, l, r) << endl;
}
if (opt == 3)
{
seg.erase(root, a[l], l);
seg.insert(root, r, l);
a[l] = r;
}
if (opt == 4)
{
yin >> k;
int xx = seg.kth(root, 0, k - 1, l, r);
if (!xx)
yout << -INT_MAX << endl;
else
{
yout << seg.rank(root, xx, l, r) << endl;
}
}
if (opt == 5)
{
yin >> k;
int xx = seg.kth(root, 0, k, l, r);
if (xx > r - l)
yout << INT_MAX << endl;
else
{
yout << seg.rank(root, xx + 1, l, r) << endl;
}
}
}
return 0;
}
|