#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 |
199 ms |
26196 KB |
Output is correct |
2 |
Correct |
216 ms |
24520 KB |
Output is correct |
3 |
Correct |
201 ms |
23868 KB |
Output is correct |
4 |
Correct |
171 ms |
21796 KB |
Output is correct |
5 |
Correct |
182 ms |
26060 KB |
Output is correct |
6 |
Correct |
175 ms |
25628 KB |
Output is correct |
7 |
Correct |
182 ms |
27220 KB |
Output is correct |
8 |
Correct |
176 ms |
24288 KB |
Output is correct |
9 |
Correct |
173 ms |
23068 KB |
Output is correct |
10 |
Correct |
177 ms |
23092 KB |
Output is correct |
11 |
Correct |
168 ms |
23212 KB |
Output is correct |
12 |
Correct |
161 ms |
19376 KB |
Output is correct |
13 |
Correct |
170 ms |
21920 KB |
Output is correct |
14 |
Correct |
179 ms |
25128 KB |
Output is correct |
15 |
Correct |
178 ms |
22072 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
145 ms |
13772 KB |
Output is correct |
18 |
Correct |
150 ms |
17336 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
268 ms |
41656 KB |
Output is correct |
2 |
Correct |
256 ms |
42172 KB |
Output is correct |
3 |
Correct |
200 ms |
35664 KB |
Output is correct |
4 |
Correct |
189 ms |
35772 KB |
Output is correct |
5 |
Correct |
199 ms |
35416 KB |
Output is correct |
6 |
Correct |
184 ms |
34732 KB |
Output is correct |
7 |
Correct |
232 ms |
40476 KB |
Output is correct |
8 |
Correct |
224 ms |
38812 KB |
Output is correct |
9 |
Correct |
189 ms |
34744 KB |
Output is correct |
10 |
Correct |
216 ms |
37560 KB |
Output is correct |
11 |
Correct |
187 ms |
33616 KB |
Output is correct |
12 |
Correct |
177 ms |
33208 KB |
Output is correct |
13 |
Correct |
212 ms |
37000 KB |
Output is correct |
14 |
Correct |
178 ms |
33816 KB |
Output is correct |
15 |
Correct |
226 ms |
38584 KB |
Output is correct |
16 |
Correct |
23 ms |
10076 KB |
Output is correct |
17 |
Correct |
159 ms |
28640 KB |
Output is correct |
18 |
Correct |
158 ms |
31068 KB |
Output is correct |
19 |
Correct |
55 ms |
13568 KB |
Output is correct |
20 |
Correct |
67 ms |
16588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
9416 KB |
Output is correct |
2 |
Correct |
47 ms |
9408 KB |
Output is correct |
3 |
Correct |
47 ms |
9168 KB |
Output is correct |
4 |
Correct |
36 ms |
8524 KB |
Output is correct |
5 |
Correct |
40 ms |
9044 KB |
Output is correct |
6 |
Correct |
37 ms |
8528 KB |
Output is correct |
7 |
Correct |
42 ms |
8948 KB |
Output is correct |
8 |
Correct |
39 ms |
8532 KB |
Output is correct |
9 |
Correct |
43 ms |
8692 KB |
Output is correct |
10 |
Correct |
31 ms |
8016 KB |
Output is correct |
11 |
Correct |
34 ms |
8540 KB |
Output is correct |
12 |
Correct |
32 ms |
8536 KB |
Output is correct |
13 |
Correct |
34 ms |
8020 KB |
Output is correct |
14 |
Correct |
33 ms |
8276 KB |
Output is correct |
15 |
Correct |
31 ms |
8276 KB |
Output is correct |
16 |
Correct |
12 ms |
5212 KB |
Output is correct |
17 |
Correct |
25 ms |
7168 KB |
Output is correct |
18 |
Correct |
27 ms |
7392 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
199 ms |
26196 KB |
Output is correct |
2 |
Correct |
216 ms |
24520 KB |
Output is correct |
3 |
Correct |
201 ms |
23868 KB |
Output is correct |
4 |
Correct |
171 ms |
21796 KB |
Output is correct |
5 |
Correct |
182 ms |
26060 KB |
Output is correct |
6 |
Correct |
175 ms |
25628 KB |
Output is correct |
7 |
Correct |
182 ms |
27220 KB |
Output is correct |
8 |
Correct |
176 ms |
24288 KB |
Output is correct |
9 |
Correct |
173 ms |
23068 KB |
Output is correct |
10 |
Correct |
177 ms |
23092 KB |
Output is correct |
11 |
Correct |
168 ms |
23212 KB |
Output is correct |
12 |
Correct |
161 ms |
19376 KB |
Output is correct |
13 |
Correct |
170 ms |
21920 KB |
Output is correct |
14 |
Correct |
179 ms |
25128 KB |
Output is correct |
15 |
Correct |
178 ms |
22072 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
145 ms |
13772 KB |
Output is correct |
18 |
Correct |
150 ms |
17336 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
268 ms |
41656 KB |
Output is correct |
22 |
Correct |
256 ms |
42172 KB |
Output is correct |
23 |
Correct |
200 ms |
35664 KB |
Output is correct |
24 |
Correct |
189 ms |
35772 KB |
Output is correct |
25 |
Correct |
199 ms |
35416 KB |
Output is correct |
26 |
Correct |
184 ms |
34732 KB |
Output is correct |
27 |
Correct |
232 ms |
40476 KB |
Output is correct |
28 |
Correct |
224 ms |
38812 KB |
Output is correct |
29 |
Correct |
189 ms |
34744 KB |
Output is correct |
30 |
Correct |
216 ms |
37560 KB |
Output is correct |
31 |
Correct |
187 ms |
33616 KB |
Output is correct |
32 |
Correct |
177 ms |
33208 KB |
Output is correct |
33 |
Correct |
212 ms |
37000 KB |
Output is correct |
34 |
Correct |
178 ms |
33816 KB |
Output is correct |
35 |
Correct |
226 ms |
38584 KB |
Output is correct |
36 |
Correct |
23 ms |
10076 KB |
Output is correct |
37 |
Correct |
159 ms |
28640 KB |
Output is correct |
38 |
Correct |
158 ms |
31068 KB |
Output is correct |
39 |
Correct |
55 ms |
13568 KB |
Output is correct |
40 |
Correct |
67 ms |
16588 KB |
Output is correct |
41 |
Correct |
47 ms |
9416 KB |
Output is correct |
42 |
Correct |
47 ms |
9408 KB |
Output is correct |
43 |
Correct |
47 ms |
9168 KB |
Output is correct |
44 |
Correct |
36 ms |
8524 KB |
Output is correct |
45 |
Correct |
40 ms |
9044 KB |
Output is correct |
46 |
Correct |
37 ms |
8528 KB |
Output is correct |
47 |
Correct |
42 ms |
8948 KB |
Output is correct |
48 |
Correct |
39 ms |
8532 KB |
Output is correct |
49 |
Correct |
43 ms |
8692 KB |
Output is correct |
50 |
Correct |
31 ms |
8016 KB |
Output is correct |
51 |
Correct |
34 ms |
8540 KB |
Output is correct |
52 |
Correct |
32 ms |
8536 KB |
Output is correct |
53 |
Correct |
34 ms |
8020 KB |
Output is correct |
54 |
Correct |
33 ms |
8276 KB |
Output is correct |
55 |
Correct |
31 ms |
8276 KB |
Output is correct |
56 |
Correct |
12 ms |
5212 KB |
Output is correct |
57 |
Correct |
25 ms |
7168 KB |
Output is correct |
58 |
Correct |
27 ms |
7392 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
522 ms |
48428 KB |
Output is correct |
62 |
Correct |
423 ms |
46788 KB |
Output is correct |
63 |
Correct |
391 ms |
45252 KB |
Output is correct |
64 |
Correct |
296 ms |
44364 KB |
Output is correct |
65 |
Correct |
325 ms |
46676 KB |
Output is correct |
66 |
Correct |
337 ms |
44244 KB |
Output is correct |
67 |
Correct |
280 ms |
44368 KB |
Output is correct |
68 |
Correct |
289 ms |
42264 KB |
Output is correct |
69 |
Correct |
303 ms |
44728 KB |
Output is correct |
70 |
Correct |
262 ms |
40788 KB |
Output is correct |
71 |
Correct |
242 ms |
42864 KB |
Output is correct |
72 |
Correct |
257 ms |
41240 KB |
Output is correct |
73 |
Correct |
254 ms |
41556 KB |
Output is correct |
74 |
Correct |
272 ms |
43604 KB |
Output is correct |
75 |
Correct |
262 ms |
42324 KB |
Output is correct |
76 |
Correct |
27 ms |
10076 KB |
Output is correct |
77 |
Correct |
185 ms |
28844 KB |
Output is correct |
78 |
Correct |
195 ms |
31684 KB |
Output is correct |
79 |
Correct |
0 ms |
600 KB |
Output is correct |
80 |
Correct |
0 ms |
348 KB |
Output is correct |