// CEOI 2022 Day1 - Abracadabra
#include <bits/stdc++.h>
using namespace std;
using VI = vector<int>;
const int NN = 2e5 + 4;
struct BIT {
// int C[NN];
VI C;
int n;
BIT(int _n) : C(_n + 1), n(_n + 1) {}
void add(int x, int v) {
while (x < n) C[x] += v, x += x & -x;
}
int sum(int x) {
int ret = 0;
while (x) ret += C[x], x -= x & -x;
return ret;
}
int kth(int x) {
int pos = 0;
for (int i = 17; i >= 0; i--)
if (pos + (1 << i) < n && C[pos + (1 << i)] < x)
pos += (1 << i), x -= C[pos];
return pos + 1;
}
};
int main() {
ios::sync_with_stdio(false), cin.tie(0);
int N, q;
cin >> N >> q;
BIT S(N);
VI A(N), P(N + 1), Ans(q);
vector<vector<pair<int, int>>> v(N + 1);
for (int i = 0; i < N; i++) cin >> A[i], P[A[i]] = i;
for (int i = 0, t, x; i < q; i++) {
cin >> t >> x, t = min(t, N);
if (t == 0)
Ans[i] = A[x - 1];
else
v[t].emplace_back(x - 1, i);
}
VI nxt(N), ds(N + 1);
auto SetVal = [&](int x, int v) { S.add(x, v - ds[x]), ds[x] = v; };
for (int i = 0; i < N;) {
int j = i + 1;
while (j < N && A[j] < A[i]) j++;
SetVal(A[i], j - i), i = j;
}
VI v2;
for (int i = N - 1; i >= 0; i--) {
while (!v2.empty() && A[v2.back()] < A[i]) v2.pop_back();
nxt[i] = !v2.empty() ? v2.back() : N, v2.push_back(i);
}
for (int i = 1; i <= N; i++) {
int itf = S.kth(N / 2 + 1), its = ds[itf], sum = S.sum(itf - 1);
if (sum < N / 2) {
int l = P[itf], r = l + its;
SetVal(A[l], N / 2 - sum);
l += N / 2 - sum;
for (int i = l; i < r; i = nxt[i]) SetVal(A[i], min(r, nxt[i]) - i);
}
for (auto p : v[i]) {
int pos = p.first, idx = p.second, qos = S.kth(pos + 1),
sum = S.sum(qos - 1);
Ans[idx] = A[P[qos] + pos - sum];
}
}
for (int x : Ans) cout << x << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
287 ms |
19656 KB |
Output is correct |
2 |
Correct |
260 ms |
19296 KB |
Output is correct |
3 |
Correct |
248 ms |
18780 KB |
Output is correct |
4 |
Correct |
209 ms |
17660 KB |
Output is correct |
5 |
Correct |
227 ms |
20964 KB |
Output is correct |
6 |
Correct |
209 ms |
21396 KB |
Output is correct |
7 |
Correct |
247 ms |
22004 KB |
Output is correct |
8 |
Correct |
223 ms |
19916 KB |
Output is correct |
9 |
Correct |
207 ms |
18968 KB |
Output is correct |
10 |
Correct |
213 ms |
18656 KB |
Output is correct |
11 |
Correct |
220 ms |
19044 KB |
Output is correct |
12 |
Correct |
206 ms |
16024 KB |
Output is correct |
13 |
Correct |
214 ms |
17992 KB |
Output is correct |
14 |
Correct |
225 ms |
20440 KB |
Output is correct |
15 |
Correct |
227 ms |
18068 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
166 ms |
10220 KB |
Output is correct |
18 |
Correct |
214 ms |
13956 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
303 ms |
29320 KB |
Output is correct |
2 |
Correct |
310 ms |
31180 KB |
Output is correct |
3 |
Correct |
263 ms |
25892 KB |
Output is correct |
4 |
Correct |
259 ms |
25156 KB |
Output is correct |
5 |
Correct |
277 ms |
25644 KB |
Output is correct |
6 |
Correct |
306 ms |
24728 KB |
Output is correct |
7 |
Correct |
320 ms |
29092 KB |
Output is correct |
8 |
Correct |
303 ms |
28112 KB |
Output is correct |
9 |
Correct |
284 ms |
25600 KB |
Output is correct |
10 |
Correct |
326 ms |
27916 KB |
Output is correct |
11 |
Correct |
219 ms |
25376 KB |
Output is correct |
12 |
Correct |
240 ms |
24644 KB |
Output is correct |
13 |
Correct |
283 ms |
28100 KB |
Output is correct |
14 |
Correct |
238 ms |
25436 KB |
Output is correct |
15 |
Correct |
279 ms |
28636 KB |
Output is correct |
16 |
Correct |
28 ms |
9932 KB |
Output is correct |
17 |
Correct |
202 ms |
21568 KB |
Output is correct |
18 |
Correct |
191 ms |
24856 KB |
Output is correct |
19 |
Correct |
66 ms |
12828 KB |
Output is correct |
20 |
Correct |
93 ms |
14120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
9420 KB |
Output is correct |
2 |
Correct |
58 ms |
8800 KB |
Output is correct |
3 |
Correct |
60 ms |
8836 KB |
Output is correct |
4 |
Correct |
44 ms |
8132 KB |
Output is correct |
5 |
Correct |
57 ms |
8292 KB |
Output is correct |
6 |
Correct |
47 ms |
8220 KB |
Output is correct |
7 |
Correct |
55 ms |
7940 KB |
Output is correct |
8 |
Correct |
48 ms |
7844 KB |
Output is correct |
9 |
Correct |
49 ms |
8516 KB |
Output is correct |
10 |
Correct |
41 ms |
8020 KB |
Output is correct |
11 |
Correct |
42 ms |
7724 KB |
Output is correct |
12 |
Correct |
45 ms |
8000 KB |
Output is correct |
13 |
Correct |
41 ms |
7884 KB |
Output is correct |
14 |
Correct |
40 ms |
8268 KB |
Output is correct |
15 |
Correct |
41 ms |
7684 KB |
Output is correct |
16 |
Correct |
14 ms |
4832 KB |
Output is correct |
17 |
Correct |
32 ms |
6712 KB |
Output is correct |
18 |
Correct |
32 ms |
6872 KB |
Output is correct |
19 |
Correct |
0 ms |
316 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
287 ms |
19656 KB |
Output is correct |
2 |
Correct |
260 ms |
19296 KB |
Output is correct |
3 |
Correct |
248 ms |
18780 KB |
Output is correct |
4 |
Correct |
209 ms |
17660 KB |
Output is correct |
5 |
Correct |
227 ms |
20964 KB |
Output is correct |
6 |
Correct |
209 ms |
21396 KB |
Output is correct |
7 |
Correct |
247 ms |
22004 KB |
Output is correct |
8 |
Correct |
223 ms |
19916 KB |
Output is correct |
9 |
Correct |
207 ms |
18968 KB |
Output is correct |
10 |
Correct |
213 ms |
18656 KB |
Output is correct |
11 |
Correct |
220 ms |
19044 KB |
Output is correct |
12 |
Correct |
206 ms |
16024 KB |
Output is correct |
13 |
Correct |
214 ms |
17992 KB |
Output is correct |
14 |
Correct |
225 ms |
20440 KB |
Output is correct |
15 |
Correct |
227 ms |
18068 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
166 ms |
10220 KB |
Output is correct |
18 |
Correct |
214 ms |
13956 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
324 KB |
Output is correct |
21 |
Correct |
303 ms |
29320 KB |
Output is correct |
22 |
Correct |
310 ms |
31180 KB |
Output is correct |
23 |
Correct |
263 ms |
25892 KB |
Output is correct |
24 |
Correct |
259 ms |
25156 KB |
Output is correct |
25 |
Correct |
277 ms |
25644 KB |
Output is correct |
26 |
Correct |
306 ms |
24728 KB |
Output is correct |
27 |
Correct |
320 ms |
29092 KB |
Output is correct |
28 |
Correct |
303 ms |
28112 KB |
Output is correct |
29 |
Correct |
284 ms |
25600 KB |
Output is correct |
30 |
Correct |
326 ms |
27916 KB |
Output is correct |
31 |
Correct |
219 ms |
25376 KB |
Output is correct |
32 |
Correct |
240 ms |
24644 KB |
Output is correct |
33 |
Correct |
283 ms |
28100 KB |
Output is correct |
34 |
Correct |
238 ms |
25436 KB |
Output is correct |
35 |
Correct |
279 ms |
28636 KB |
Output is correct |
36 |
Correct |
28 ms |
9932 KB |
Output is correct |
37 |
Correct |
202 ms |
21568 KB |
Output is correct |
38 |
Correct |
191 ms |
24856 KB |
Output is correct |
39 |
Correct |
66 ms |
12828 KB |
Output is correct |
40 |
Correct |
93 ms |
14120 KB |
Output is correct |
41 |
Correct |
66 ms |
9420 KB |
Output is correct |
42 |
Correct |
58 ms |
8800 KB |
Output is correct |
43 |
Correct |
60 ms |
8836 KB |
Output is correct |
44 |
Correct |
44 ms |
8132 KB |
Output is correct |
45 |
Correct |
57 ms |
8292 KB |
Output is correct |
46 |
Correct |
47 ms |
8220 KB |
Output is correct |
47 |
Correct |
55 ms |
7940 KB |
Output is correct |
48 |
Correct |
48 ms |
7844 KB |
Output is correct |
49 |
Correct |
49 ms |
8516 KB |
Output is correct |
50 |
Correct |
41 ms |
8020 KB |
Output is correct |
51 |
Correct |
42 ms |
7724 KB |
Output is correct |
52 |
Correct |
45 ms |
8000 KB |
Output is correct |
53 |
Correct |
41 ms |
7884 KB |
Output is correct |
54 |
Correct |
40 ms |
8268 KB |
Output is correct |
55 |
Correct |
41 ms |
7684 KB |
Output is correct |
56 |
Correct |
14 ms |
4832 KB |
Output is correct |
57 |
Correct |
32 ms |
6712 KB |
Output is correct |
58 |
Correct |
32 ms |
6872 KB |
Output is correct |
59 |
Correct |
0 ms |
316 KB |
Output is correct |
60 |
Correct |
0 ms |
212 KB |
Output is correct |
61 |
Correct |
583 ms |
36360 KB |
Output is correct |
62 |
Correct |
502 ms |
35844 KB |
Output is correct |
63 |
Correct |
520 ms |
34884 KB |
Output is correct |
64 |
Correct |
384 ms |
33964 KB |
Output is correct |
65 |
Correct |
394 ms |
35276 KB |
Output is correct |
66 |
Correct |
433 ms |
33940 KB |
Output is correct |
67 |
Correct |
381 ms |
33568 KB |
Output is correct |
68 |
Correct |
346 ms |
32064 KB |
Output is correct |
69 |
Correct |
378 ms |
34348 KB |
Output is correct |
70 |
Correct |
349 ms |
30980 KB |
Output is correct |
71 |
Correct |
310 ms |
33928 KB |
Output is correct |
72 |
Correct |
332 ms |
31492 KB |
Output is correct |
73 |
Correct |
311 ms |
32448 KB |
Output is correct |
74 |
Correct |
331 ms |
33692 KB |
Output is correct |
75 |
Correct |
318 ms |
32500 KB |
Output is correct |
76 |
Correct |
28 ms |
9440 KB |
Output is correct |
77 |
Correct |
215 ms |
21856 KB |
Output is correct |
78 |
Correct |
255 ms |
24644 KB |
Output is correct |
79 |
Correct |
0 ms |
212 KB |
Output is correct |
80 |
Correct |
0 ms |
212 KB |
Output is correct |