#include <bits/stdc++.h>
using namespace std;
#define mid ((l + r) >> 1)
#define tl (node + 1)
#define tr (node + 2 * (mid - l + 1))
const int MAXN = 2e5 + 25;
array <int, 3> v[MAXN << 2];
const int MAXQ = 1e6 + 25;
int n, q;
array <int, 3> queries[MAXQ];
int a[MAXN], nxt[MAXN];
int cnt = 0;
int ans[MAXQ];
int get (array <int, 3> z) {
return lower_bound(v + 1, v + cnt + 1, z) - v;
}
struct SegmentTree {
int tree[MAXN << 3];
void update (int l, int r, int a, int b, int node) {
if (l > a || r < a) return;
if (l == r) {
tree[node] += b;
return;
}
update(l, mid, a, b, tl); update(mid + 1, r, a, b, tr);
tree[node] = tree[tl] + tree[tr];
}
int get (int l, int r, int a, int node) {
if (l == r) return l;
if (tree[tl] >= a) return get(l, mid, a, tl);
return get(mid + 1, r, a - tree[tl], tr);
}
int query (int l, int r, int a, int b, int node) {
if (a > b) return 0;
if (l > b || r < a) return 0;
if (l >= a && r <= b) return tree[node];
return query(l, mid, a, b, tl) + query(mid + 1, r, a, b, tr);
}
} cur;
void ins (array <int, 3> x) {
int z = get(x);
cur.update(1, cnt, z, x[2] - x[1] + 1, 1);
}
void rem (array <int, 3> x) {
int z = get(x);
cur.update(1, cnt, z, -(x[2] - x[1] + 1), 1);
}
int main () {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; i++) cin >> a[i];
stack <int> cur2;
for (int i = 1; i <= n; i++) {
while (!cur2.empty() && a[cur2.top()] < a[i]) {
nxt[cur2.top()] = i;
cur2.pop();
}
cur2.push(i);
}
for (int i = 1; i <= q; i++) {
cin >> queries[i][0] >> queries[i][1]; queries[i][2] = i;
queries[i][0] = min(queries[i][0], n);
}
sort(queries + 1, queries + q + 1);
int sum = 0;
priority_queue <array <int, 3>> pq;
for (int i = 1; i != 0; i = nxt[i]) {
if (nxt[i] == 0) pq.push({a[i], i, n}), v[++cnt] = {a[i], i, n};
else pq.push({a[i], i, nxt[i] - 1}), v[++cnt] = {a[i], i, nxt[i] - 1};
}
while (!pq.empty()) {
if (sum == n / 2) break;
auto z = pq.top(); pq.pop();
if (sum + z[2] - z[1] + 1 <= n / 2) {
sum += z[2] - z[1] + 1; continue;
}
int s = n / 2 - sum, l = z[2] - z[1] + 1;
int dd = l - s;
pq.push({a[z[1]], z[1], z[1] + dd - 1}); v[++cnt] = {a[z[1]], z[1], z[1] + dd - 1};
z[1] += dd;
for (int i = z[1]; i && i <= z[2]; i = nxt[i]) {
if (nxt[i]) {
pq.push({a[i], i, min(nxt[i] - 1, z[2])}); v[++cnt] = {a[i], i, min(nxt[i] - 1, z[2])};
} else {
pq.push({a[i], i, z[2]}); v[++cnt] = {a[i], i, z[2]};
}
}
}
sort(v + 1, v + cnt + 1);
while (!pq.empty()) pq.pop();
sum = 0;
for (int i = 1; i != 0; i = nxt[i]) {
if (nxt[i] == 0) pq.push({a[i], i, n}), ins({a[i], i, n});
else pq.push({a[i], i, nxt[i] - 1}), ins({a[i], i, nxt[i] - 1});
}
int ptr = 1;
while (ptr <= q && queries[ptr][0] == 0) {
int u = cur.get(1, cnt, queries[ptr][1], 1);
int g = queries[ptr][1] - cur.query(1, cnt, 1, u - 1, 1);
ans[queries[ptr][2]] = v[u][1] + g - 1;
ptr++;
}
for (int i = 1; i <= n; i++) {
while (!pq.empty()) {
if (sum == n / 2) break;
auto z = pq.top(); pq.pop();
if (sum + z[2] - z[1] + 1 <= n / 2) {
sum += z[2] - z[1] + 1; continue;
}
rem(z);
int s = n / 2 - sum, l = z[2] - z[1] + 1;
int dd = l - s;
pq.push({a[z[1]], z[1], z[1] + dd - 1}); ins({a[z[1]], z[1], z[1] + dd - 1});
z[1] += dd;
for (int i = z[1]; i && i <= z[2]; i = nxt[i]) {
if (nxt[i]) {
pq.push({a[i], i, min(nxt[i] - 1, z[2])}); ins({a[i], i, min(nxt[i] - 1, z[2])});
} else {
pq.push({a[i], i, z[2]}); ins({a[i], i, z[2]});
}
}
break;
}
while (ptr <= q && queries[ptr][0] == i) {
int u = cur.get(1, cnt, queries[ptr][1], 1);
int g = queries[ptr][1] - cur.query(1, cnt, 1, u - 1, 1);
ans[queries[ptr][2]] = v[u][1] + g - 1;
ptr++;
}
}
for (int i = 1; i <= q; i++) cout << a[ans[i]] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
383 ms |
24808 KB |
Output is correct |
2 |
Correct |
350 ms |
24912 KB |
Output is correct |
3 |
Correct |
341 ms |
24664 KB |
Output is correct |
4 |
Correct |
304 ms |
24536 KB |
Output is correct |
5 |
Correct |
353 ms |
24784 KB |
Output is correct |
6 |
Correct |
309 ms |
24568 KB |
Output is correct |
7 |
Correct |
329 ms |
24824 KB |
Output is correct |
8 |
Correct |
315 ms |
24688 KB |
Output is correct |
9 |
Correct |
299 ms |
24656 KB |
Output is correct |
10 |
Correct |
303 ms |
24660 KB |
Output is correct |
11 |
Correct |
314 ms |
24536 KB |
Output is correct |
12 |
Correct |
291 ms |
24544 KB |
Output is correct |
13 |
Correct |
312 ms |
24796 KB |
Output is correct |
14 |
Correct |
331 ms |
24596 KB |
Output is correct |
15 |
Correct |
316 ms |
24788 KB |
Output is correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
17 |
Correct |
304 ms |
24540 KB |
Output is correct |
18 |
Correct |
274 ms |
24148 KB |
Output is correct |
19 |
Correct |
1 ms |
8792 KB |
Output is correct |
20 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
638 ms |
37548 KB |
Output is correct |
2 |
Correct |
484 ms |
47500 KB |
Output is correct |
3 |
Correct |
463 ms |
46744 KB |
Output is correct |
4 |
Correct |
369 ms |
38108 KB |
Output is correct |
5 |
Correct |
380 ms |
39968 KB |
Output is correct |
6 |
Correct |
371 ms |
39372 KB |
Output is correct |
7 |
Correct |
380 ms |
41556 KB |
Output is correct |
8 |
Correct |
362 ms |
42444 KB |
Output is correct |
9 |
Correct |
352 ms |
38088 KB |
Output is correct |
10 |
Correct |
383 ms |
39112 KB |
Output is correct |
11 |
Correct |
323 ms |
35452 KB |
Output is correct |
12 |
Correct |
337 ms |
35864 KB |
Output is correct |
13 |
Correct |
338 ms |
38224 KB |
Output is correct |
14 |
Correct |
333 ms |
36192 KB |
Output is correct |
15 |
Correct |
332 ms |
39440 KB |
Output is correct |
16 |
Correct |
17 ms |
9816 KB |
Output is correct |
17 |
Correct |
426 ms |
42180 KB |
Output is correct |
18 |
Correct |
239 ms |
33100 KB |
Output is correct |
19 |
Correct |
83 ms |
16724 KB |
Output is correct |
20 |
Correct |
77 ms |
18516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
157 ms |
18932 KB |
Output is correct |
2 |
Correct |
126 ms |
19080 KB |
Output is correct |
3 |
Correct |
119 ms |
18888 KB |
Output is correct |
4 |
Correct |
53 ms |
17176 KB |
Output is correct |
5 |
Correct |
105 ms |
16384 KB |
Output is correct |
6 |
Correct |
61 ms |
15812 KB |
Output is correct |
7 |
Correct |
87 ms |
16032 KB |
Output is correct |
8 |
Correct |
68 ms |
17488 KB |
Output is correct |
9 |
Correct |
93 ms |
15808 KB |
Output is correct |
10 |
Correct |
43 ms |
14720 KB |
Output is correct |
11 |
Correct |
45 ms |
14928 KB |
Output is correct |
12 |
Correct |
45 ms |
14660 KB |
Output is correct |
13 |
Correct |
45 ms |
14808 KB |
Output is correct |
14 |
Correct |
44 ms |
14928 KB |
Output is correct |
15 |
Correct |
47 ms |
14740 KB |
Output is correct |
16 |
Correct |
10 ms |
9308 KB |
Output is correct |
17 |
Correct |
78 ms |
16584 KB |
Output is correct |
18 |
Correct |
37 ms |
12880 KB |
Output is correct |
19 |
Correct |
1 ms |
8792 KB |
Output is correct |
20 |
Correct |
1 ms |
6744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
383 ms |
24808 KB |
Output is correct |
2 |
Correct |
350 ms |
24912 KB |
Output is correct |
3 |
Correct |
341 ms |
24664 KB |
Output is correct |
4 |
Correct |
304 ms |
24536 KB |
Output is correct |
5 |
Correct |
353 ms |
24784 KB |
Output is correct |
6 |
Correct |
309 ms |
24568 KB |
Output is correct |
7 |
Correct |
329 ms |
24824 KB |
Output is correct |
8 |
Correct |
315 ms |
24688 KB |
Output is correct |
9 |
Correct |
299 ms |
24656 KB |
Output is correct |
10 |
Correct |
303 ms |
24660 KB |
Output is correct |
11 |
Correct |
314 ms |
24536 KB |
Output is correct |
12 |
Correct |
291 ms |
24544 KB |
Output is correct |
13 |
Correct |
312 ms |
24796 KB |
Output is correct |
14 |
Correct |
331 ms |
24596 KB |
Output is correct |
15 |
Correct |
316 ms |
24788 KB |
Output is correct |
16 |
Correct |
1 ms |
8540 KB |
Output is correct |
17 |
Correct |
304 ms |
24540 KB |
Output is correct |
18 |
Correct |
274 ms |
24148 KB |
Output is correct |
19 |
Correct |
1 ms |
8792 KB |
Output is correct |
20 |
Correct |
1 ms |
6488 KB |
Output is correct |
21 |
Correct |
638 ms |
37548 KB |
Output is correct |
22 |
Correct |
484 ms |
47500 KB |
Output is correct |
23 |
Correct |
463 ms |
46744 KB |
Output is correct |
24 |
Correct |
369 ms |
38108 KB |
Output is correct |
25 |
Correct |
380 ms |
39968 KB |
Output is correct |
26 |
Correct |
371 ms |
39372 KB |
Output is correct |
27 |
Correct |
380 ms |
41556 KB |
Output is correct |
28 |
Correct |
362 ms |
42444 KB |
Output is correct |
29 |
Correct |
352 ms |
38088 KB |
Output is correct |
30 |
Correct |
383 ms |
39112 KB |
Output is correct |
31 |
Correct |
323 ms |
35452 KB |
Output is correct |
32 |
Correct |
337 ms |
35864 KB |
Output is correct |
33 |
Correct |
338 ms |
38224 KB |
Output is correct |
34 |
Correct |
333 ms |
36192 KB |
Output is correct |
35 |
Correct |
332 ms |
39440 KB |
Output is correct |
36 |
Correct |
17 ms |
9816 KB |
Output is correct |
37 |
Correct |
426 ms |
42180 KB |
Output is correct |
38 |
Correct |
239 ms |
33100 KB |
Output is correct |
39 |
Correct |
83 ms |
16724 KB |
Output is correct |
40 |
Correct |
77 ms |
18516 KB |
Output is correct |
41 |
Correct |
157 ms |
18932 KB |
Output is correct |
42 |
Correct |
126 ms |
19080 KB |
Output is correct |
43 |
Correct |
119 ms |
18888 KB |
Output is correct |
44 |
Correct |
53 ms |
17176 KB |
Output is correct |
45 |
Correct |
105 ms |
16384 KB |
Output is correct |
46 |
Correct |
61 ms |
15812 KB |
Output is correct |
47 |
Correct |
87 ms |
16032 KB |
Output is correct |
48 |
Correct |
68 ms |
17488 KB |
Output is correct |
49 |
Correct |
93 ms |
15808 KB |
Output is correct |
50 |
Correct |
43 ms |
14720 KB |
Output is correct |
51 |
Correct |
45 ms |
14928 KB |
Output is correct |
52 |
Correct |
45 ms |
14660 KB |
Output is correct |
53 |
Correct |
45 ms |
14808 KB |
Output is correct |
54 |
Correct |
44 ms |
14928 KB |
Output is correct |
55 |
Correct |
47 ms |
14740 KB |
Output is correct |
56 |
Correct |
10 ms |
9308 KB |
Output is correct |
57 |
Correct |
78 ms |
16584 KB |
Output is correct |
58 |
Correct |
37 ms |
12880 KB |
Output is correct |
59 |
Correct |
1 ms |
8792 KB |
Output is correct |
60 |
Correct |
1 ms |
6744 KB |
Output is correct |
61 |
Correct |
769 ms |
51408 KB |
Output is correct |
62 |
Correct |
692 ms |
46564 KB |
Output is correct |
63 |
Correct |
691 ms |
49964 KB |
Output is correct |
64 |
Correct |
491 ms |
41164 KB |
Output is correct |
65 |
Correct |
505 ms |
43608 KB |
Output is correct |
66 |
Correct |
465 ms |
42448 KB |
Output is correct |
67 |
Correct |
462 ms |
40856 KB |
Output is correct |
68 |
Correct |
472 ms |
42224 KB |
Output is correct |
69 |
Correct |
508 ms |
40768 KB |
Output is correct |
70 |
Correct |
426 ms |
39156 KB |
Output is correct |
71 |
Correct |
364 ms |
37972 KB |
Output is correct |
72 |
Correct |
390 ms |
39188 KB |
Output is correct |
73 |
Correct |
397 ms |
38536 KB |
Output is correct |
74 |
Correct |
428 ms |
39760 KB |
Output is correct |
75 |
Correct |
403 ms |
39252 KB |
Output is correct |
76 |
Correct |
17 ms |
10072 KB |
Output is correct |
77 |
Correct |
484 ms |
42152 KB |
Output is correct |
78 |
Correct |
307 ms |
36692 KB |
Output is correct |
79 |
Correct |
2 ms |
8540 KB |
Output is correct |
80 |
Correct |
1 ms |
6492 KB |
Output is correct |