#include <bits/stdc++.h>
using namespace std;
#define dbg(x) //x
#define prt(x) dbg(cerr << x)
#define pv(x) dbg(cerr << #x << " = " << x << '\n')
#define pv2(x) dbg(cerr << #x << " = " << x.first << ',' << x.second << '\n')
#define parr(x) dbg(prt(#x << " = { "); for (auto y : x) prt(y << ' '); prt("}\n");)
#define parr2(x) dbg(prt(#x << " = { "); for (auto [y, z] : x) prt(y << ',' << z << " "); prt("}\n");)
#define parr2d(x) dbg(prt(#x << ":\n"); for (auto arr : x) {parr(arr);} prt('\n'));
#define parr2d2(x) dbg(prt(#x << ":\n"); for (auto arr : x) {parr2(arr);} prt('\n'));
struct FenwickTree {
int n;
vector<int> tree;
FenwickTree(int x) {
n = x;
tree.resize(n + 1, 0);
}
void add(int k, int x) {
for (int i = k + 1; i <= n; i += (i & (-i))) {
tree[i] += x;
}
}
int pref(int r) {
int sum = 0;
for (int i = r; i > 0; i -= (i & (-i))) {
sum += tree[i];
}
return sum;
}
int quer(int l, int r) {
return pref(r + 1) - pref(l);
}
};
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int n, q;
cin >> n >> q;
vector<int> a(n), at(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
a[i]--;
at[a[i]] = i;
}
vector<int> nl(n);
stack<int> sta;
for (int i = n - 1; i >= 0; i--) {
while (!sta.empty() && a[sta.top()] < a[i]) {
sta.pop();
}
if (sta.empty()) {
nl[i] = n;
} else {
nl[i] = sta.top();
}
sta.push(i);
}
vector<array<int, 3>> quer(q);
for (int i = 0; i < q; i++) {
cin >> quer[i][0] >> quer[i][1];
quer[i][1]--;
quer[i][2] = i;
}
sort(quer.begin(), quer.end());
vector<int> ans(q);
vector<int> sz(n, 0);
int pmx = -1;
for (int i = 0; i < n / 2; i++) {
if (a[i] > pmx) {
pmx = a[i];
}
sz[pmx]++;
}
pmx = -1;
for (int i = n / 2; i < n; i++) {
if (a[i] > pmx) {
pmx = a[i];
}
sz[pmx]++;
}
FenwickTree fenw(n);
for (int i = 0; i < n; i++) {
fenw.add(i, sz[i]);
}
int ptr = 0; bool done = false;
while (ptr < q && quer[ptr][0] == 0) {
ans[quer[ptr][2]] = a[quer[ptr][1]];
ptr++;
}
for (int it = 1; ptr < q; it++) {
int l = 0, r = n - 1;
while (r > l) {
int m = (l + r) / 2;
if (fenw.quer(0, m) >= n / 2) {
r = m;
} else {
l = m + 1;
}
}
int k = r;
int cnt = fenw.quer(0, k), prev = fenw.quer(0, k - 1);
if (cnt == n / 2) {
done = true;
}
while (ptr < q && (done == true || quer[ptr][0] == it)) {
parr(quer[ptr]);
l = 0, r = n - 1;
while (r > l) {
int m = (l + r) / 2;
if (fenw.quer(0, m) >= quer[ptr][1] + 1) {
r = m;
} else {
l = m + 1;
}
}
pv(l);
int prv = fenw.quer(0, l - 1);
pv(prv);
ans[quer[ptr][2]] = a[at[l] + quer[ptr][1] - prv];
ptr++;
}
fenw.add(k, -(cnt - n / 2));
for (int i = at[k] + n / 2 - prev; i < at[k] + cnt - prev; i = nl[i]) {
fenw.add(a[i], min(at[k] + cnt - prev, nl[i]) - i);
}
}
for (int i = 0; i < q; i++) {
cout << ans[i] + 1 << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
359 ms |
19800 KB |
Output is correct |
2 |
Correct |
380 ms |
27332 KB |
Output is correct |
3 |
Correct |
352 ms |
26428 KB |
Output is correct |
4 |
Correct |
368 ms |
25168 KB |
Output is correct |
5 |
Correct |
385 ms |
26960 KB |
Output is correct |
6 |
Correct |
338 ms |
25424 KB |
Output is correct |
7 |
Correct |
398 ms |
27156 KB |
Output is correct |
8 |
Correct |
347 ms |
25776 KB |
Output is correct |
9 |
Correct |
347 ms |
25164 KB |
Output is correct |
10 |
Correct |
351 ms |
25768 KB |
Output is correct |
11 |
Correct |
361 ms |
25772 KB |
Output is correct |
12 |
Correct |
335 ms |
24516 KB |
Output is correct |
13 |
Correct |
373 ms |
25420 KB |
Output is correct |
14 |
Correct |
366 ms |
26404 KB |
Output is correct |
15 |
Correct |
368 ms |
26192 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
266 ms |
24984 KB |
Output is correct |
18 |
Correct |
322 ms |
24912 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
532 ms |
26444 KB |
Output is correct |
2 |
Correct |
486 ms |
40636 KB |
Output is correct |
3 |
Correct |
426 ms |
32336 KB |
Output is correct |
4 |
Correct |
467 ms |
32320 KB |
Output is correct |
5 |
Correct |
453 ms |
33108 KB |
Output is correct |
6 |
Correct |
437 ms |
31568 KB |
Output is correct |
7 |
Correct |
473 ms |
39504 KB |
Output is correct |
8 |
Correct |
461 ms |
38016 KB |
Output is correct |
9 |
Correct |
435 ms |
32780 KB |
Output is correct |
10 |
Correct |
483 ms |
36760 KB |
Output is correct |
11 |
Correct |
420 ms |
31168 KB |
Output is correct |
12 |
Correct |
421 ms |
30804 KB |
Output is correct |
13 |
Correct |
486 ms |
36188 KB |
Output is correct |
14 |
Correct |
440 ms |
31916 KB |
Output is correct |
15 |
Correct |
496 ms |
37972 KB |
Output is correct |
16 |
Correct |
20 ms |
5464 KB |
Output is correct |
17 |
Correct |
249 ms |
35288 KB |
Output is correct |
18 |
Correct |
390 ms |
28708 KB |
Output is correct |
19 |
Correct |
64 ms |
11332 KB |
Output is correct |
20 |
Correct |
111 ms |
12880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
80 ms |
4436 KB |
Output is correct |
2 |
Correct |
65 ms |
6520 KB |
Output is correct |
3 |
Correct |
75 ms |
6236 KB |
Output is correct |
4 |
Correct |
60 ms |
5956 KB |
Output is correct |
5 |
Correct |
70 ms |
6060 KB |
Output is correct |
6 |
Correct |
60 ms |
5968 KB |
Output is correct |
7 |
Correct |
61 ms |
6228 KB |
Output is correct |
8 |
Correct |
61 ms |
5904 KB |
Output is correct |
9 |
Correct |
59 ms |
6084 KB |
Output is correct |
10 |
Correct |
55 ms |
5804 KB |
Output is correct |
11 |
Correct |
53 ms |
5968 KB |
Output is correct |
12 |
Correct |
54 ms |
5712 KB |
Output is correct |
13 |
Correct |
58 ms |
5972 KB |
Output is correct |
14 |
Correct |
55 ms |
5996 KB |
Output is correct |
15 |
Correct |
57 ms |
5888 KB |
Output is correct |
16 |
Correct |
9 ms |
2908 KB |
Output is correct |
17 |
Correct |
35 ms |
5980 KB |
Output is correct |
18 |
Correct |
44 ms |
5716 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
359 ms |
19800 KB |
Output is correct |
2 |
Correct |
380 ms |
27332 KB |
Output is correct |
3 |
Correct |
352 ms |
26428 KB |
Output is correct |
4 |
Correct |
368 ms |
25168 KB |
Output is correct |
5 |
Correct |
385 ms |
26960 KB |
Output is correct |
6 |
Correct |
338 ms |
25424 KB |
Output is correct |
7 |
Correct |
398 ms |
27156 KB |
Output is correct |
8 |
Correct |
347 ms |
25776 KB |
Output is correct |
9 |
Correct |
347 ms |
25164 KB |
Output is correct |
10 |
Correct |
351 ms |
25768 KB |
Output is correct |
11 |
Correct |
361 ms |
25772 KB |
Output is correct |
12 |
Correct |
335 ms |
24516 KB |
Output is correct |
13 |
Correct |
373 ms |
25420 KB |
Output is correct |
14 |
Correct |
366 ms |
26404 KB |
Output is correct |
15 |
Correct |
368 ms |
26192 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
266 ms |
24984 KB |
Output is correct |
18 |
Correct |
322 ms |
24912 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
532 ms |
26444 KB |
Output is correct |
22 |
Correct |
486 ms |
40636 KB |
Output is correct |
23 |
Correct |
426 ms |
32336 KB |
Output is correct |
24 |
Correct |
467 ms |
32320 KB |
Output is correct |
25 |
Correct |
453 ms |
33108 KB |
Output is correct |
26 |
Correct |
437 ms |
31568 KB |
Output is correct |
27 |
Correct |
473 ms |
39504 KB |
Output is correct |
28 |
Correct |
461 ms |
38016 KB |
Output is correct |
29 |
Correct |
435 ms |
32780 KB |
Output is correct |
30 |
Correct |
483 ms |
36760 KB |
Output is correct |
31 |
Correct |
420 ms |
31168 KB |
Output is correct |
32 |
Correct |
421 ms |
30804 KB |
Output is correct |
33 |
Correct |
486 ms |
36188 KB |
Output is correct |
34 |
Correct |
440 ms |
31916 KB |
Output is correct |
35 |
Correct |
496 ms |
37972 KB |
Output is correct |
36 |
Correct |
20 ms |
5464 KB |
Output is correct |
37 |
Correct |
249 ms |
35288 KB |
Output is correct |
38 |
Correct |
390 ms |
28708 KB |
Output is correct |
39 |
Correct |
64 ms |
11332 KB |
Output is correct |
40 |
Correct |
111 ms |
12880 KB |
Output is correct |
41 |
Correct |
80 ms |
4436 KB |
Output is correct |
42 |
Correct |
65 ms |
6520 KB |
Output is correct |
43 |
Correct |
75 ms |
6236 KB |
Output is correct |
44 |
Correct |
60 ms |
5956 KB |
Output is correct |
45 |
Correct |
70 ms |
6060 KB |
Output is correct |
46 |
Correct |
60 ms |
5968 KB |
Output is correct |
47 |
Correct |
61 ms |
6228 KB |
Output is correct |
48 |
Correct |
61 ms |
5904 KB |
Output is correct |
49 |
Correct |
59 ms |
6084 KB |
Output is correct |
50 |
Correct |
55 ms |
5804 KB |
Output is correct |
51 |
Correct |
53 ms |
5968 KB |
Output is correct |
52 |
Correct |
54 ms |
5712 KB |
Output is correct |
53 |
Correct |
58 ms |
5972 KB |
Output is correct |
54 |
Correct |
55 ms |
5996 KB |
Output is correct |
55 |
Correct |
57 ms |
5888 KB |
Output is correct |
56 |
Correct |
9 ms |
2908 KB |
Output is correct |
57 |
Correct |
35 ms |
5980 KB |
Output is correct |
58 |
Correct |
44 ms |
5716 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
630 ms |
40272 KB |
Output is correct |
62 |
Correct |
620 ms |
40324 KB |
Output is correct |
63 |
Correct |
601 ms |
38476 KB |
Output is correct |
64 |
Correct |
572 ms |
37972 KB |
Output is correct |
65 |
Correct |
608 ms |
39484 KB |
Output is correct |
66 |
Correct |
559 ms |
37704 KB |
Output is correct |
67 |
Correct |
554 ms |
39248 KB |
Output is correct |
68 |
Correct |
554 ms |
37700 KB |
Output is correct |
69 |
Correct |
530 ms |
37640 KB |
Output is correct |
70 |
Correct |
549 ms |
37308 KB |
Output is correct |
71 |
Correct |
494 ms |
36692 KB |
Output is correct |
72 |
Correct |
533 ms |
37288 KB |
Output is correct |
73 |
Correct |
523 ms |
36960 KB |
Output is correct |
74 |
Correct |
524 ms |
38040 KB |
Output is correct |
75 |
Correct |
551 ms |
38164 KB |
Output is correct |
76 |
Correct |
26 ms |
5460 KB |
Output is correct |
77 |
Correct |
308 ms |
35564 KB |
Output is correct |
78 |
Correct |
437 ms |
34632 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
0 ms |
348 KB |
Output is correct |