// 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 p = 0, nb = ceil(log2(n));
for (int b = 1 << nb; b >= 1; b >>= 1)
if (p + b < n && C[p + b] < x) p += b, x -= C[p];
return p + 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 |
269 ms |
18456 KB |
Output is correct |
2 |
Correct |
294 ms |
17128 KB |
Output is correct |
3 |
Correct |
290 ms |
16716 KB |
Output is correct |
4 |
Correct |
264 ms |
15508 KB |
Output is correct |
5 |
Correct |
279 ms |
18872 KB |
Output is correct |
6 |
Correct |
271 ms |
19212 KB |
Output is correct |
7 |
Correct |
270 ms |
19720 KB |
Output is correct |
8 |
Correct |
312 ms |
17712 KB |
Output is correct |
9 |
Correct |
240 ms |
16644 KB |
Output is correct |
10 |
Correct |
261 ms |
16484 KB |
Output is correct |
11 |
Correct |
264 ms |
16844 KB |
Output is correct |
12 |
Correct |
224 ms |
13904 KB |
Output is correct |
13 |
Correct |
256 ms |
15644 KB |
Output is correct |
14 |
Correct |
269 ms |
18312 KB |
Output is correct |
15 |
Correct |
330 ms |
15740 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
217 ms |
8048 KB |
Output is correct |
18 |
Correct |
237 ms |
11804 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 |
360 ms |
27080 KB |
Output is correct |
2 |
Correct |
371 ms |
28768 KB |
Output is correct |
3 |
Correct |
270 ms |
23640 KB |
Output is correct |
4 |
Correct |
259 ms |
22860 KB |
Output is correct |
5 |
Correct |
262 ms |
23404 KB |
Output is correct |
6 |
Correct |
267 ms |
22540 KB |
Output is correct |
7 |
Correct |
320 ms |
26948 KB |
Output is correct |
8 |
Correct |
304 ms |
25860 KB |
Output is correct |
9 |
Correct |
258 ms |
23440 KB |
Output is correct |
10 |
Correct |
300 ms |
25756 KB |
Output is correct |
11 |
Correct |
247 ms |
23260 KB |
Output is correct |
12 |
Correct |
253 ms |
22452 KB |
Output is correct |
13 |
Correct |
287 ms |
26028 KB |
Output is correct |
14 |
Correct |
252 ms |
23308 KB |
Output is correct |
15 |
Correct |
301 ms |
26420 KB |
Output is correct |
16 |
Correct |
31 ms |
8788 KB |
Output is correct |
17 |
Correct |
199 ms |
19400 KB |
Output is correct |
18 |
Correct |
221 ms |
22696 KB |
Output is correct |
19 |
Correct |
84 ms |
10748 KB |
Output is correct |
20 |
Correct |
94 ms |
11992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
62 ms |
7628 KB |
Output is correct |
2 |
Correct |
63 ms |
7520 KB |
Output is correct |
3 |
Correct |
70 ms |
7376 KB |
Output is correct |
4 |
Correct |
48 ms |
6800 KB |
Output is correct |
5 |
Correct |
54 ms |
7280 KB |
Output is correct |
6 |
Correct |
52 ms |
6776 KB |
Output is correct |
7 |
Correct |
52 ms |
7188 KB |
Output is correct |
8 |
Correct |
48 ms |
6756 KB |
Output is correct |
9 |
Correct |
52 ms |
7016 KB |
Output is correct |
10 |
Correct |
43 ms |
6604 KB |
Output is correct |
11 |
Correct |
45 ms |
6876 KB |
Output is correct |
12 |
Correct |
51 ms |
6640 KB |
Output is correct |
13 |
Correct |
46 ms |
6472 KB |
Output is correct |
14 |
Correct |
45 ms |
6792 KB |
Output is correct |
15 |
Correct |
43 ms |
6644 KB |
Output is correct |
16 |
Correct |
15 ms |
4564 KB |
Output is correct |
17 |
Correct |
34 ms |
5896 KB |
Output is correct |
18 |
Correct |
38 ms |
5876 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 |
269 ms |
18456 KB |
Output is correct |
2 |
Correct |
294 ms |
17128 KB |
Output is correct |
3 |
Correct |
290 ms |
16716 KB |
Output is correct |
4 |
Correct |
264 ms |
15508 KB |
Output is correct |
5 |
Correct |
279 ms |
18872 KB |
Output is correct |
6 |
Correct |
271 ms |
19212 KB |
Output is correct |
7 |
Correct |
270 ms |
19720 KB |
Output is correct |
8 |
Correct |
312 ms |
17712 KB |
Output is correct |
9 |
Correct |
240 ms |
16644 KB |
Output is correct |
10 |
Correct |
261 ms |
16484 KB |
Output is correct |
11 |
Correct |
264 ms |
16844 KB |
Output is correct |
12 |
Correct |
224 ms |
13904 KB |
Output is correct |
13 |
Correct |
256 ms |
15644 KB |
Output is correct |
14 |
Correct |
269 ms |
18312 KB |
Output is correct |
15 |
Correct |
330 ms |
15740 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
217 ms |
8048 KB |
Output is correct |
18 |
Correct |
237 ms |
11804 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
360 ms |
27080 KB |
Output is correct |
22 |
Correct |
371 ms |
28768 KB |
Output is correct |
23 |
Correct |
270 ms |
23640 KB |
Output is correct |
24 |
Correct |
259 ms |
22860 KB |
Output is correct |
25 |
Correct |
262 ms |
23404 KB |
Output is correct |
26 |
Correct |
267 ms |
22540 KB |
Output is correct |
27 |
Correct |
320 ms |
26948 KB |
Output is correct |
28 |
Correct |
304 ms |
25860 KB |
Output is correct |
29 |
Correct |
258 ms |
23440 KB |
Output is correct |
30 |
Correct |
300 ms |
25756 KB |
Output is correct |
31 |
Correct |
247 ms |
23260 KB |
Output is correct |
32 |
Correct |
253 ms |
22452 KB |
Output is correct |
33 |
Correct |
287 ms |
26028 KB |
Output is correct |
34 |
Correct |
252 ms |
23308 KB |
Output is correct |
35 |
Correct |
301 ms |
26420 KB |
Output is correct |
36 |
Correct |
31 ms |
8788 KB |
Output is correct |
37 |
Correct |
199 ms |
19400 KB |
Output is correct |
38 |
Correct |
221 ms |
22696 KB |
Output is correct |
39 |
Correct |
84 ms |
10748 KB |
Output is correct |
40 |
Correct |
94 ms |
11992 KB |
Output is correct |
41 |
Correct |
62 ms |
7628 KB |
Output is correct |
42 |
Correct |
63 ms |
7520 KB |
Output is correct |
43 |
Correct |
70 ms |
7376 KB |
Output is correct |
44 |
Correct |
48 ms |
6800 KB |
Output is correct |
45 |
Correct |
54 ms |
7280 KB |
Output is correct |
46 |
Correct |
52 ms |
6776 KB |
Output is correct |
47 |
Correct |
52 ms |
7188 KB |
Output is correct |
48 |
Correct |
48 ms |
6756 KB |
Output is correct |
49 |
Correct |
52 ms |
7016 KB |
Output is correct |
50 |
Correct |
43 ms |
6604 KB |
Output is correct |
51 |
Correct |
45 ms |
6876 KB |
Output is correct |
52 |
Correct |
51 ms |
6640 KB |
Output is correct |
53 |
Correct |
46 ms |
6472 KB |
Output is correct |
54 |
Correct |
45 ms |
6792 KB |
Output is correct |
55 |
Correct |
43 ms |
6644 KB |
Output is correct |
56 |
Correct |
15 ms |
4564 KB |
Output is correct |
57 |
Correct |
34 ms |
5896 KB |
Output is correct |
58 |
Correct |
38 ms |
5876 KB |
Output is correct |
59 |
Correct |
0 ms |
212 KB |
Output is correct |
60 |
Correct |
0 ms |
212 KB |
Output is correct |
61 |
Correct |
622 ms |
34184 KB |
Output is correct |
62 |
Correct |
522 ms |
33516 KB |
Output is correct |
63 |
Correct |
532 ms |
32464 KB |
Output is correct |
64 |
Correct |
418 ms |
31752 KB |
Output is correct |
65 |
Correct |
421 ms |
33100 KB |
Output is correct |
66 |
Correct |
402 ms |
31692 KB |
Output is correct |
67 |
Correct |
373 ms |
31180 KB |
Output is correct |
68 |
Correct |
389 ms |
29948 KB |
Output is correct |
69 |
Correct |
396 ms |
32148 KB |
Output is correct |
70 |
Correct |
366 ms |
28748 KB |
Output is correct |
71 |
Correct |
332 ms |
31696 KB |
Output is correct |
72 |
Correct |
357 ms |
29284 KB |
Output is correct |
73 |
Correct |
339 ms |
30156 KB |
Output is correct |
74 |
Correct |
368 ms |
31564 KB |
Output is correct |
75 |
Correct |
361 ms |
30444 KB |
Output is correct |
76 |
Correct |
32 ms |
8780 KB |
Output is correct |
77 |
Correct |
221 ms |
19624 KB |
Output is correct |
78 |
Correct |
267 ms |
22456 KB |
Output is correct |
79 |
Correct |
1 ms |
212 KB |
Output is correct |
80 |
Correct |
0 ms |
212 KB |
Output is correct |