#include <bits/stdc++.h>
using namespace std;
const int LOG = 20;
struct Fenwick {
int n;
vector<int> bit;
vector<int> a;
Fenwick(int n) : n(n), bit(n + 1, 0), a(n, 0) {}
void add(int i, int delta) {
a[i] += delta;
for (i++; i <= n; i += i & -i) {
bit[i] += delta;
}
}
int sum(int i) {
int res = 0;
for (; i > 0; i -= i & -i) {
res += bit[i];
}
return res;
}
int get(int i) {
return a[i];
}
int kth(int k) {
int sum = 0;
int cur = 0;
for (int i = LOG - 1; i >= 0; i--) {
if (cur + (1 << i) <= n && sum + bit[cur + (1 << i)] <= k) {
cur += (1 << i);
sum += bit[cur];
}
}
return cur;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<int> a(n), ia(n);
vector<vector<array<int, 2>>> mp(n + 1);
vector<int> ans(q);
for (int i = 0; i < n; i++) {
cin >> a[i];
a[i]--;
ia[a[i]] = i;
}
for (int id = 0; id < q; id++) {
int t, i;
cin >> t >> i;
i--;
t = min(t, n);
mp[t].push_back({ i, id });
}
vector<int> dec;
vector<int> f(n);
for (int i = n - 1; i >= 0; i--) {
while (!dec.empty() && a[i] > a[dec.back()]) {
dec.pop_back();
}
f[i] = dec.empty() ? n : dec.back();
dec.push_back(i);
}
Fenwick fen(n);
for (int i = 0; i < n; i = f[i]) {
fen.add(a[i], f[i] - i);
}
function<void()> cut = [&]() {
int head = fen.kth(n / 2);
int pos = fen.sum(head);
int fromLen = fen.get(head);
int toLen = n / 2 - pos;
fen.add(head, -fromLen + toLen);
int newHead = a[ia[head] + toLen];
int i = ia[newHead];
int k = n / 2;
int bound = pos + fromLen;
while (k + f[i] - i < bound) {
fen.add(a[i], f[i] - i);
k += f[i] - i;
i = f[i];
}
fen.add(a[i], bound - k);
};
function<int(int)> kth = [&](int k) {
int head = fen.kth(k);
int pos = fen.sum(head);
int delta = k - pos;
return a[ia[head] + delta];
};
for (int t = 0; t <= n; t++) {
for (auto [i, id] : mp[t]) {
ans[id] = kth(i);
}
cut();
}
for (auto x : ans) {
cout << x + 1 << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
260 ms |
22672 KB |
Output is correct |
2 |
Correct |
251 ms |
20884 KB |
Output is correct |
3 |
Correct |
234 ms |
20632 KB |
Output is correct |
4 |
Correct |
219 ms |
19092 KB |
Output is correct |
5 |
Correct |
198 ms |
22756 KB |
Output is correct |
6 |
Correct |
197 ms |
23312 KB |
Output is correct |
7 |
Correct |
199 ms |
23716 KB |
Output is correct |
8 |
Correct |
193 ms |
21700 KB |
Output is correct |
9 |
Correct |
208 ms |
20616 KB |
Output is correct |
10 |
Correct |
207 ms |
20292 KB |
Output is correct |
11 |
Correct |
187 ms |
21080 KB |
Output is correct |
12 |
Correct |
184 ms |
18432 KB |
Output is correct |
13 |
Correct |
195 ms |
19572 KB |
Output is correct |
14 |
Correct |
192 ms |
21956 KB |
Output is correct |
15 |
Correct |
207 ms |
20464 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
194 ms |
18576 KB |
Output is correct |
18 |
Correct |
243 ms |
18648 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
284 ms |
34640 KB |
Output is correct |
2 |
Correct |
311 ms |
34956 KB |
Output is correct |
3 |
Correct |
252 ms |
30928 KB |
Output is correct |
4 |
Correct |
222 ms |
29112 KB |
Output is correct |
5 |
Correct |
219 ms |
31048 KB |
Output is correct |
6 |
Correct |
242 ms |
29308 KB |
Output is correct |
7 |
Correct |
257 ms |
33968 KB |
Output is correct |
8 |
Correct |
282 ms |
38796 KB |
Output is correct |
9 |
Correct |
209 ms |
36028 KB |
Output is correct |
10 |
Correct |
263 ms |
37752 KB |
Output is correct |
11 |
Correct |
197 ms |
33552 KB |
Output is correct |
12 |
Correct |
213 ms |
33560 KB |
Output is correct |
13 |
Correct |
244 ms |
37052 KB |
Output is correct |
14 |
Correct |
207 ms |
34756 KB |
Output is correct |
15 |
Correct |
261 ms |
38588 KB |
Output is correct |
16 |
Correct |
28 ms |
10072 KB |
Output is correct |
17 |
Correct |
244 ms |
36536 KB |
Output is correct |
18 |
Correct |
189 ms |
30616 KB |
Output is correct |
19 |
Correct |
76 ms |
15308 KB |
Output is correct |
20 |
Correct |
80 ms |
16584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
8792 KB |
Output is correct |
2 |
Correct |
54 ms |
8648 KB |
Output is correct |
3 |
Correct |
50 ms |
8400 KB |
Output is correct |
4 |
Correct |
42 ms |
7976 KB |
Output is correct |
5 |
Correct |
46 ms |
8276 KB |
Output is correct |
6 |
Correct |
42 ms |
7764 KB |
Output is correct |
7 |
Correct |
49 ms |
8364 KB |
Output is correct |
8 |
Correct |
43 ms |
8528 KB |
Output is correct |
9 |
Correct |
43 ms |
8832 KB |
Output is correct |
10 |
Correct |
41 ms |
8020 KB |
Output is correct |
11 |
Correct |
36 ms |
8540 KB |
Output is correct |
12 |
Correct |
38 ms |
8280 KB |
Output is correct |
13 |
Correct |
44 ms |
8180 KB |
Output is correct |
14 |
Correct |
53 ms |
8388 KB |
Output is correct |
15 |
Correct |
49 ms |
8272 KB |
Output is correct |
16 |
Correct |
20 ms |
5212 KB |
Output is correct |
17 |
Correct |
43 ms |
7920 KB |
Output is correct |
18 |
Correct |
36 ms |
7748 KB |
Output is correct |
19 |
Correct |
0 ms |
376 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
260 ms |
22672 KB |
Output is correct |
2 |
Correct |
251 ms |
20884 KB |
Output is correct |
3 |
Correct |
234 ms |
20632 KB |
Output is correct |
4 |
Correct |
219 ms |
19092 KB |
Output is correct |
5 |
Correct |
198 ms |
22756 KB |
Output is correct |
6 |
Correct |
197 ms |
23312 KB |
Output is correct |
7 |
Correct |
199 ms |
23716 KB |
Output is correct |
8 |
Correct |
193 ms |
21700 KB |
Output is correct |
9 |
Correct |
208 ms |
20616 KB |
Output is correct |
10 |
Correct |
207 ms |
20292 KB |
Output is correct |
11 |
Correct |
187 ms |
21080 KB |
Output is correct |
12 |
Correct |
184 ms |
18432 KB |
Output is correct |
13 |
Correct |
195 ms |
19572 KB |
Output is correct |
14 |
Correct |
192 ms |
21956 KB |
Output is correct |
15 |
Correct |
207 ms |
20464 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
194 ms |
18576 KB |
Output is correct |
18 |
Correct |
243 ms |
18648 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
284 ms |
34640 KB |
Output is correct |
22 |
Correct |
311 ms |
34956 KB |
Output is correct |
23 |
Correct |
252 ms |
30928 KB |
Output is correct |
24 |
Correct |
222 ms |
29112 KB |
Output is correct |
25 |
Correct |
219 ms |
31048 KB |
Output is correct |
26 |
Correct |
242 ms |
29308 KB |
Output is correct |
27 |
Correct |
257 ms |
33968 KB |
Output is correct |
28 |
Correct |
282 ms |
38796 KB |
Output is correct |
29 |
Correct |
209 ms |
36028 KB |
Output is correct |
30 |
Correct |
263 ms |
37752 KB |
Output is correct |
31 |
Correct |
197 ms |
33552 KB |
Output is correct |
32 |
Correct |
213 ms |
33560 KB |
Output is correct |
33 |
Correct |
244 ms |
37052 KB |
Output is correct |
34 |
Correct |
207 ms |
34756 KB |
Output is correct |
35 |
Correct |
261 ms |
38588 KB |
Output is correct |
36 |
Correct |
28 ms |
10072 KB |
Output is correct |
37 |
Correct |
244 ms |
36536 KB |
Output is correct |
38 |
Correct |
189 ms |
30616 KB |
Output is correct |
39 |
Correct |
76 ms |
15308 KB |
Output is correct |
40 |
Correct |
80 ms |
16584 KB |
Output is correct |
41 |
Correct |
52 ms |
8792 KB |
Output is correct |
42 |
Correct |
54 ms |
8648 KB |
Output is correct |
43 |
Correct |
50 ms |
8400 KB |
Output is correct |
44 |
Correct |
42 ms |
7976 KB |
Output is correct |
45 |
Correct |
46 ms |
8276 KB |
Output is correct |
46 |
Correct |
42 ms |
7764 KB |
Output is correct |
47 |
Correct |
49 ms |
8364 KB |
Output is correct |
48 |
Correct |
43 ms |
8528 KB |
Output is correct |
49 |
Correct |
43 ms |
8832 KB |
Output is correct |
50 |
Correct |
41 ms |
8020 KB |
Output is correct |
51 |
Correct |
36 ms |
8540 KB |
Output is correct |
52 |
Correct |
38 ms |
8280 KB |
Output is correct |
53 |
Correct |
44 ms |
8180 KB |
Output is correct |
54 |
Correct |
53 ms |
8388 KB |
Output is correct |
55 |
Correct |
49 ms |
8272 KB |
Output is correct |
56 |
Correct |
20 ms |
5212 KB |
Output is correct |
57 |
Correct |
43 ms |
7920 KB |
Output is correct |
58 |
Correct |
36 ms |
7748 KB |
Output is correct |
59 |
Correct |
0 ms |
376 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
527 ms |
48224 KB |
Output is correct |
62 |
Correct |
443 ms |
46752 KB |
Output is correct |
63 |
Correct |
438 ms |
45680 KB |
Output is correct |
64 |
Correct |
348 ms |
44568 KB |
Output is correct |
65 |
Correct |
368 ms |
46416 KB |
Output is correct |
66 |
Correct |
338 ms |
44380 KB |
Output is correct |
67 |
Correct |
309 ms |
44252 KB |
Output is correct |
68 |
Correct |
302 ms |
42368 KB |
Output is correct |
69 |
Correct |
317 ms |
44640 KB |
Output is correct |
70 |
Correct |
293 ms |
40740 KB |
Output is correct |
71 |
Correct |
254 ms |
42096 KB |
Output is correct |
72 |
Correct |
287 ms |
41080 KB |
Output is correct |
73 |
Correct |
276 ms |
41552 KB |
Output is correct |
74 |
Correct |
278 ms |
43600 KB |
Output is correct |
75 |
Correct |
299 ms |
42320 KB |
Output is correct |
76 |
Correct |
29 ms |
10076 KB |
Output is correct |
77 |
Correct |
334 ms |
37564 KB |
Output is correct |
78 |
Correct |
244 ms |
35408 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
0 ms |
348 KB |
Output is correct |