#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, Q;
cin >> N >> Q;
vector<int> A(N);
for (int &x : A) {
cin >> x;
}
vector<int> queries(Q), ans(Q);
vector<vector<int>> q(N + 1);
for (int i = 0; i < Q; i++) {
int t, p;
cin >> t >> p;
t = min(t, N);
p--;
queries[i] = p;
if (t == 0) {
ans[i] = A[p];
} else {
q[t].push_back(i);
}
}
vector<int> f(N, N), stk;
for (int i = N - 1; i >= 0; i--) {
while (!stk.empty() && A[stk.back()] < A[i]) {
stk.pop_back();
}
if (!stk.empty()) {
f[i] = stk.back();
}
stk.push_back(i);
}
vector<int> bit(N + 1);
auto add = [&](int p, int x) {
for (int i = p; i <= N; i += i & -i) {
bit[i] += x;
}
};
auto sum = [&](int p) {
int s = 0;
for (int i = p; i; i -= i & -i) {
s += bit[i];
}
return s;
};
set<tuple<int, int, int>> s, t;
for (int i = 0; i < N; i = f[i]) {
s.emplace(A[i], i, f[i]);
add(A[i], f[i] - i);
}
int T = N;
for (int i = 1; i <= N; i++) {
if (T > N / 2) {
while (1) {
auto [_, l, r] = *prev(s.end());
if (T - r + l < N / 2) {
break;
}
T -= r - l;
t.insert(*prev(s.end()));
s.erase(prev(s.end()));
}
if (T > N / 2) {
auto [v, l, r] = *prev(s.end());
s.erase(prev(s.end()));
int m = r - T + N / 2;
s.emplace(v, l, m);
add(v, m - r);
for (int i = m; i < r; i = f[i]) {
s.emplace(A[i], i, min(r, f[i]));
add(A[i], min(r, f[i]) - i);
}
}
}
for (int k : q[i]) {
int p = queries[k];
int l = 1, r = N;
while (l < r) {
int m = (l + r) / 2;
if (sum(m) > p) {
r = m;
} else {
l = m + 1;
}
}
auto [v, L, R] = *s.lower_bound({l, 0, 0});
if (v == l) {
ans[k] = A[R - sum(l) + p];
} else {
auto [v, L, R] = *t.lower_bound({l, 0, 0});
ans[k] = A[R - sum(l) + p];
}
}
}
for (int x : ans) {
cout << x << "\n";
}
return 6/22;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
306 ms |
17788 KB |
Output is correct |
2 |
Correct |
335 ms |
16784 KB |
Output is correct |
3 |
Correct |
325 ms |
16212 KB |
Output is correct |
4 |
Correct |
210 ms |
16312 KB |
Output is correct |
5 |
Correct |
258 ms |
24868 KB |
Output is correct |
6 |
Correct |
240 ms |
23928 KB |
Output is correct |
7 |
Correct |
254 ms |
25228 KB |
Output is correct |
8 |
Correct |
246 ms |
23188 KB |
Output is correct |
9 |
Correct |
236 ms |
23316 KB |
Output is correct |
10 |
Correct |
239 ms |
22388 KB |
Output is correct |
11 |
Correct |
216 ms |
22540 KB |
Output is correct |
12 |
Correct |
213 ms |
20220 KB |
Output is correct |
13 |
Correct |
211 ms |
22084 KB |
Output is correct |
14 |
Correct |
236 ms |
24000 KB |
Output is correct |
15 |
Correct |
242 ms |
23068 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
142 ms |
17508 KB |
Output is correct |
18 |
Correct |
167 ms |
19096 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
562 ms |
38440 KB |
Output is correct |
2 |
Correct |
518 ms |
44476 KB |
Output is correct |
3 |
Correct |
420 ms |
41384 KB |
Output is correct |
4 |
Correct |
320 ms |
32076 KB |
Output is correct |
5 |
Correct |
341 ms |
32800 KB |
Output is correct |
6 |
Correct |
307 ms |
31824 KB |
Output is correct |
7 |
Correct |
374 ms |
32888 KB |
Output is correct |
8 |
Correct |
370 ms |
32804 KB |
Output is correct |
9 |
Correct |
322 ms |
31932 KB |
Output is correct |
10 |
Correct |
329 ms |
31592 KB |
Output is correct |
11 |
Correct |
264 ms |
30796 KB |
Output is correct |
12 |
Correct |
272 ms |
30120 KB |
Output is correct |
13 |
Correct |
328 ms |
32152 KB |
Output is correct |
14 |
Correct |
285 ms |
30128 KB |
Output is correct |
15 |
Correct |
340 ms |
31720 KB |
Output is correct |
16 |
Correct |
20 ms |
8532 KB |
Output is correct |
17 |
Correct |
230 ms |
42528 KB |
Output is correct |
18 |
Correct |
229 ms |
28688 KB |
Output is correct |
19 |
Correct |
56 ms |
12852 KB |
Output is correct |
20 |
Correct |
87 ms |
15232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
100 ms |
13516 KB |
Output is correct |
2 |
Correct |
108 ms |
14512 KB |
Output is correct |
3 |
Correct |
128 ms |
14332 KB |
Output is correct |
4 |
Correct |
56 ms |
8060 KB |
Output is correct |
5 |
Correct |
69 ms |
10960 KB |
Output is correct |
6 |
Correct |
50 ms |
8236 KB |
Output is correct |
7 |
Correct |
58 ms |
9856 KB |
Output is correct |
8 |
Correct |
57 ms |
8928 KB |
Output is correct |
9 |
Correct |
62 ms |
9908 KB |
Output is correct |
10 |
Correct |
43 ms |
7140 KB |
Output is correct |
11 |
Correct |
42 ms |
7496 KB |
Output is correct |
12 |
Correct |
42 ms |
7108 KB |
Output is correct |
13 |
Correct |
46 ms |
7312 KB |
Output is correct |
14 |
Correct |
48 ms |
7360 KB |
Output is correct |
15 |
Correct |
42 ms |
7132 KB |
Output is correct |
16 |
Correct |
9 ms |
4544 KB |
Output is correct |
17 |
Correct |
44 ms |
12708 KB |
Output is correct |
18 |
Correct |
29 ms |
6652 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
306 ms |
17788 KB |
Output is correct |
2 |
Correct |
335 ms |
16784 KB |
Output is correct |
3 |
Correct |
325 ms |
16212 KB |
Output is correct |
4 |
Correct |
210 ms |
16312 KB |
Output is correct |
5 |
Correct |
258 ms |
24868 KB |
Output is correct |
6 |
Correct |
240 ms |
23928 KB |
Output is correct |
7 |
Correct |
254 ms |
25228 KB |
Output is correct |
8 |
Correct |
246 ms |
23188 KB |
Output is correct |
9 |
Correct |
236 ms |
23316 KB |
Output is correct |
10 |
Correct |
239 ms |
22388 KB |
Output is correct |
11 |
Correct |
216 ms |
22540 KB |
Output is correct |
12 |
Correct |
213 ms |
20220 KB |
Output is correct |
13 |
Correct |
211 ms |
22084 KB |
Output is correct |
14 |
Correct |
236 ms |
24000 KB |
Output is correct |
15 |
Correct |
242 ms |
23068 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
142 ms |
17508 KB |
Output is correct |
18 |
Correct |
167 ms |
19096 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
21 |
Correct |
562 ms |
38440 KB |
Output is correct |
22 |
Correct |
518 ms |
44476 KB |
Output is correct |
23 |
Correct |
420 ms |
41384 KB |
Output is correct |
24 |
Correct |
320 ms |
32076 KB |
Output is correct |
25 |
Correct |
341 ms |
32800 KB |
Output is correct |
26 |
Correct |
307 ms |
31824 KB |
Output is correct |
27 |
Correct |
374 ms |
32888 KB |
Output is correct |
28 |
Correct |
370 ms |
32804 KB |
Output is correct |
29 |
Correct |
322 ms |
31932 KB |
Output is correct |
30 |
Correct |
329 ms |
31592 KB |
Output is correct |
31 |
Correct |
264 ms |
30796 KB |
Output is correct |
32 |
Correct |
272 ms |
30120 KB |
Output is correct |
33 |
Correct |
328 ms |
32152 KB |
Output is correct |
34 |
Correct |
285 ms |
30128 KB |
Output is correct |
35 |
Correct |
340 ms |
31720 KB |
Output is correct |
36 |
Correct |
20 ms |
8532 KB |
Output is correct |
37 |
Correct |
230 ms |
42528 KB |
Output is correct |
38 |
Correct |
229 ms |
28688 KB |
Output is correct |
39 |
Correct |
56 ms |
12852 KB |
Output is correct |
40 |
Correct |
87 ms |
15232 KB |
Output is correct |
41 |
Correct |
100 ms |
13516 KB |
Output is correct |
42 |
Correct |
108 ms |
14512 KB |
Output is correct |
43 |
Correct |
128 ms |
14332 KB |
Output is correct |
44 |
Correct |
56 ms |
8060 KB |
Output is correct |
45 |
Correct |
69 ms |
10960 KB |
Output is correct |
46 |
Correct |
50 ms |
8236 KB |
Output is correct |
47 |
Correct |
58 ms |
9856 KB |
Output is correct |
48 |
Correct |
57 ms |
8928 KB |
Output is correct |
49 |
Correct |
62 ms |
9908 KB |
Output is correct |
50 |
Correct |
43 ms |
7140 KB |
Output is correct |
51 |
Correct |
42 ms |
7496 KB |
Output is correct |
52 |
Correct |
42 ms |
7108 KB |
Output is correct |
53 |
Correct |
46 ms |
7312 KB |
Output is correct |
54 |
Correct |
48 ms |
7360 KB |
Output is correct |
55 |
Correct |
42 ms |
7132 KB |
Output is correct |
56 |
Correct |
9 ms |
4544 KB |
Output is correct |
57 |
Correct |
44 ms |
12708 KB |
Output is correct |
58 |
Correct |
29 ms |
6652 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
1 ms |
212 KB |
Output is correct |
61 |
Correct |
983 ms |
57684 KB |
Output is correct |
62 |
Correct |
1116 ms |
54708 KB |
Output is correct |
63 |
Correct |
1107 ms |
53680 KB |
Output is correct |
64 |
Correct |
582 ms |
43068 KB |
Output is correct |
65 |
Correct |
532 ms |
45120 KB |
Output is correct |
66 |
Correct |
494 ms |
42956 KB |
Output is correct |
67 |
Correct |
499 ms |
41440 KB |
Output is correct |
68 |
Correct |
511 ms |
40732 KB |
Output is correct |
69 |
Correct |
607 ms |
42420 KB |
Output is correct |
70 |
Correct |
493 ms |
37856 KB |
Output is correct |
71 |
Correct |
364 ms |
38716 KB |
Output is correct |
72 |
Correct |
479 ms |
37956 KB |
Output is correct |
73 |
Correct |
449 ms |
37988 KB |
Output is correct |
74 |
Correct |
419 ms |
39736 KB |
Output is correct |
75 |
Correct |
483 ms |
38920 KB |
Output is correct |
76 |
Correct |
22 ms |
8636 KB |
Output is correct |
77 |
Correct |
248 ms |
42960 KB |
Output is correct |
78 |
Correct |
253 ms |
31904 KB |
Output is correct |
79 |
Correct |
1 ms |
212 KB |
Output is correct |
80 |
Correct |
1 ms |
212 KB |
Output is correct |