// CEOI 2022 Day1 - Abracadabra
#include <bits/stdc++.h>
using namespace std;
using VI = vector<int>;
struct BIT {
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 b = 1 << 17; b >= 1; b >>= 1)
if (pos + b < n && C[pos + b] < x) pos += b, 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";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
242 ms |
18448 KB |
Output is correct |
2 |
Correct |
251 ms |
17128 KB |
Output is correct |
3 |
Correct |
231 ms |
16680 KB |
Output is correct |
4 |
Correct |
188 ms |
15424 KB |
Output is correct |
5 |
Correct |
214 ms |
18764 KB |
Output is correct |
6 |
Correct |
198 ms |
19152 KB |
Output is correct |
7 |
Correct |
232 ms |
19736 KB |
Output is correct |
8 |
Correct |
201 ms |
17744 KB |
Output is correct |
9 |
Correct |
210 ms |
16668 KB |
Output is correct |
10 |
Correct |
202 ms |
16500 KB |
Output is correct |
11 |
Correct |
202 ms |
16800 KB |
Output is correct |
12 |
Correct |
196 ms |
13928 KB |
Output is correct |
13 |
Correct |
197 ms |
15692 KB |
Output is correct |
14 |
Correct |
220 ms |
18300 KB |
Output is correct |
15 |
Correct |
216 ms |
15660 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
177 ms |
8072 KB |
Output is correct |
18 |
Correct |
177 ms |
11748 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
294 ms |
27128 KB |
Output is correct |
2 |
Correct |
301 ms |
28736 KB |
Output is correct |
3 |
Correct |
220 ms |
23776 KB |
Output is correct |
4 |
Correct |
228 ms |
23032 KB |
Output is correct |
5 |
Correct |
259 ms |
23412 KB |
Output is correct |
6 |
Correct |
219 ms |
22540 KB |
Output is correct |
7 |
Correct |
295 ms |
26984 KB |
Output is correct |
8 |
Correct |
266 ms |
25780 KB |
Output is correct |
9 |
Correct |
235 ms |
23308 KB |
Output is correct |
10 |
Correct |
268 ms |
25788 KB |
Output is correct |
11 |
Correct |
225 ms |
23256 KB |
Output is correct |
12 |
Correct |
223 ms |
22476 KB |
Output is correct |
13 |
Correct |
264 ms |
25960 KB |
Output is correct |
14 |
Correct |
231 ms |
23252 KB |
Output is correct |
15 |
Correct |
267 ms |
26492 KB |
Output is correct |
16 |
Correct |
27 ms |
8788 KB |
Output is correct |
17 |
Correct |
193 ms |
19464 KB |
Output is correct |
18 |
Correct |
181 ms |
22648 KB |
Output is correct |
19 |
Correct |
75 ms |
10824 KB |
Output is correct |
20 |
Correct |
82 ms |
11936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
7720 KB |
Output is correct |
2 |
Correct |
61 ms |
7516 KB |
Output is correct |
3 |
Correct |
57 ms |
7400 KB |
Output is correct |
4 |
Correct |
55 ms |
6780 KB |
Output is correct |
5 |
Correct |
48 ms |
7240 KB |
Output is correct |
6 |
Correct |
42 ms |
6780 KB |
Output is correct |
7 |
Correct |
46 ms |
7104 KB |
Output is correct |
8 |
Correct |
49 ms |
6772 KB |
Output is correct |
9 |
Correct |
49 ms |
7116 KB |
Output is correct |
10 |
Correct |
38 ms |
6580 KB |
Output is correct |
11 |
Correct |
40 ms |
6880 KB |
Output is correct |
12 |
Correct |
38 ms |
6652 KB |
Output is correct |
13 |
Correct |
38 ms |
6476 KB |
Output is correct |
14 |
Correct |
45 ms |
6876 KB |
Output is correct |
15 |
Correct |
36 ms |
6648 KB |
Output is correct |
16 |
Correct |
13 ms |
4564 KB |
Output is correct |
17 |
Correct |
32 ms |
5884 KB |
Output is correct |
18 |
Correct |
32 ms |
5892 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
242 ms |
18448 KB |
Output is correct |
2 |
Correct |
251 ms |
17128 KB |
Output is correct |
3 |
Correct |
231 ms |
16680 KB |
Output is correct |
4 |
Correct |
188 ms |
15424 KB |
Output is correct |
5 |
Correct |
214 ms |
18764 KB |
Output is correct |
6 |
Correct |
198 ms |
19152 KB |
Output is correct |
7 |
Correct |
232 ms |
19736 KB |
Output is correct |
8 |
Correct |
201 ms |
17744 KB |
Output is correct |
9 |
Correct |
210 ms |
16668 KB |
Output is correct |
10 |
Correct |
202 ms |
16500 KB |
Output is correct |
11 |
Correct |
202 ms |
16800 KB |
Output is correct |
12 |
Correct |
196 ms |
13928 KB |
Output is correct |
13 |
Correct |
197 ms |
15692 KB |
Output is correct |
14 |
Correct |
220 ms |
18300 KB |
Output is correct |
15 |
Correct |
216 ms |
15660 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
177 ms |
8072 KB |
Output is correct |
18 |
Correct |
177 ms |
11748 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
294 ms |
27128 KB |
Output is correct |
22 |
Correct |
301 ms |
28736 KB |
Output is correct |
23 |
Correct |
220 ms |
23776 KB |
Output is correct |
24 |
Correct |
228 ms |
23032 KB |
Output is correct |
25 |
Correct |
259 ms |
23412 KB |
Output is correct |
26 |
Correct |
219 ms |
22540 KB |
Output is correct |
27 |
Correct |
295 ms |
26984 KB |
Output is correct |
28 |
Correct |
266 ms |
25780 KB |
Output is correct |
29 |
Correct |
235 ms |
23308 KB |
Output is correct |
30 |
Correct |
268 ms |
25788 KB |
Output is correct |
31 |
Correct |
225 ms |
23256 KB |
Output is correct |
32 |
Correct |
223 ms |
22476 KB |
Output is correct |
33 |
Correct |
264 ms |
25960 KB |
Output is correct |
34 |
Correct |
231 ms |
23252 KB |
Output is correct |
35 |
Correct |
267 ms |
26492 KB |
Output is correct |
36 |
Correct |
27 ms |
8788 KB |
Output is correct |
37 |
Correct |
193 ms |
19464 KB |
Output is correct |
38 |
Correct |
181 ms |
22648 KB |
Output is correct |
39 |
Correct |
75 ms |
10824 KB |
Output is correct |
40 |
Correct |
82 ms |
11936 KB |
Output is correct |
41 |
Correct |
58 ms |
7720 KB |
Output is correct |
42 |
Correct |
61 ms |
7516 KB |
Output is correct |
43 |
Correct |
57 ms |
7400 KB |
Output is correct |
44 |
Correct |
55 ms |
6780 KB |
Output is correct |
45 |
Correct |
48 ms |
7240 KB |
Output is correct |
46 |
Correct |
42 ms |
6780 KB |
Output is correct |
47 |
Correct |
46 ms |
7104 KB |
Output is correct |
48 |
Correct |
49 ms |
6772 KB |
Output is correct |
49 |
Correct |
49 ms |
7116 KB |
Output is correct |
50 |
Correct |
38 ms |
6580 KB |
Output is correct |
51 |
Correct |
40 ms |
6880 KB |
Output is correct |
52 |
Correct |
38 ms |
6652 KB |
Output is correct |
53 |
Correct |
38 ms |
6476 KB |
Output is correct |
54 |
Correct |
45 ms |
6876 KB |
Output is correct |
55 |
Correct |
36 ms |
6648 KB |
Output is correct |
56 |
Correct |
13 ms |
4564 KB |
Output is correct |
57 |
Correct |
32 ms |
5884 KB |
Output is correct |
58 |
Correct |
32 ms |
5892 KB |
Output is correct |
59 |
Correct |
0 ms |
212 KB |
Output is correct |
60 |
Correct |
0 ms |
212 KB |
Output is correct |
61 |
Correct |
574 ms |
34232 KB |
Output is correct |
62 |
Correct |
492 ms |
33512 KB |
Output is correct |
63 |
Correct |
498 ms |
32596 KB |
Output is correct |
64 |
Correct |
387 ms |
31692 KB |
Output is correct |
65 |
Correct |
395 ms |
33060 KB |
Output is correct |
66 |
Correct |
381 ms |
31660 KB |
Output is correct |
67 |
Correct |
354 ms |
31216 KB |
Output is correct |
68 |
Correct |
349 ms |
29844 KB |
Output is correct |
69 |
Correct |
381 ms |
32156 KB |
Output is correct |
70 |
Correct |
339 ms |
28828 KB |
Output is correct |
71 |
Correct |
290 ms |
31564 KB |
Output is correct |
72 |
Correct |
333 ms |
29332 KB |
Output is correct |
73 |
Correct |
309 ms |
30104 KB |
Output is correct |
74 |
Correct |
329 ms |
31532 KB |
Output is correct |
75 |
Correct |
385 ms |
30448 KB |
Output is correct |
76 |
Correct |
27 ms |
8788 KB |
Output is correct |
77 |
Correct |
238 ms |
19704 KB |
Output is correct |
78 |
Correct |
268 ms |
22428 KB |
Output is correct |
79 |
Correct |
0 ms |
212 KB |
Output is correct |
80 |
Correct |
0 ms |
212 KB |
Output is correct |