#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 2e5 + 5, Q = 1e6 + 5;
int n, q;
int a[N], nxt[N], pos[N], s[4 * N], res[Q];
void upd(int i, int x, int id = 1, int l = 1, int r = n) {
if (l == r) {
s[id] = x;
return;
}
int m = (l + r) / 2;
if (i <= m) {
upd(i, x, id * 2, l, m);
} else {
upd(i, x, id * 2 + 1, m + 1, r);
}
s[id] = s[id * 2] + s[id * 2 + 1];
}
array<int, 2> walk(int k) {
int id = 1, l = 1, r = n;
while (l ^ r) {
int m = (l + r) / 2;
id *= 2;
if (s[id] >= k) {
r = m;
} else {
l = m + 1;
k -= s[id];
id += 1;
}
}
return {k, l};
}
void add(int i, int j) {
while (i < j) {
nxt[i] = min(j, nxt[i]);
upd(a[i], nxt[i] - i);
i = nxt[i];
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> q;
vector<int> st;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
pos[a[i]] = i;
while (st.size() && a[st.back()] < a[i]) {
nxt[st.back()] = i;
st.pop_back();
}
st.push_back(i);
}
while (st.size()) {
nxt[st.back()] = n + 1;
st.pop_back();
}
vector<array<int, 3>> queries;
for (int i = 1; i <= q; ++i) {
int t, p; cin >> t >> p;
queries.push_back({min(t, n), p, i});
}
sort(queries.begin(), queries.end());
add(1, n + 1);
bool c = 1;
int m = n / 2;
for (int i = 0, j = 0; i <= n; ++i) {
while (j < q && queries[j][0] == i) {
auto [t, p, id] = queries[j++];
auto [l, x] = walk(p);
res[id] = a[pos[x] + l - 1];
}
if (!c) {
continue;
}
auto [l, x] = walk(m);
if (l == nxt[pos[x]] - pos[x]) {
c = 0;
continue;
}
add(pos[x] + l, nxt[pos[x]]);
nxt[pos[x]] = pos[x] + l;
upd(x, l);
}
for (int i = 1; i <= q; ++i) {
cout << res[i] << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
280 ms |
27292 KB |
Output is correct |
2 |
Correct |
302 ms |
27300 KB |
Output is correct |
3 |
Correct |
279 ms |
26532 KB |
Output is correct |
4 |
Correct |
281 ms |
25252 KB |
Output is correct |
5 |
Correct |
291 ms |
27044 KB |
Output is correct |
6 |
Correct |
266 ms |
25508 KB |
Output is correct |
7 |
Correct |
277 ms |
27232 KB |
Output is correct |
8 |
Correct |
267 ms |
25764 KB |
Output is correct |
9 |
Correct |
274 ms |
25252 KB |
Output is correct |
10 |
Correct |
274 ms |
25760 KB |
Output is correct |
11 |
Correct |
279 ms |
25760 KB |
Output is correct |
12 |
Correct |
284 ms |
24816 KB |
Output is correct |
13 |
Correct |
284 ms |
25524 KB |
Output is correct |
14 |
Correct |
286 ms |
26532 KB |
Output is correct |
15 |
Correct |
284 ms |
26272 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
284 ms |
25052 KB |
Output is correct |
18 |
Correct |
280 ms |
24732 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
340 ms |
40972 KB |
Output is correct |
2 |
Correct |
311 ms |
40488 KB |
Output is correct |
3 |
Correct |
278 ms |
33268 KB |
Output is correct |
4 |
Correct |
287 ms |
32928 KB |
Output is correct |
5 |
Correct |
295 ms |
33460 KB |
Output is correct |
6 |
Correct |
262 ms |
32336 KB |
Output is correct |
7 |
Correct |
313 ms |
40152 KB |
Output is correct |
8 |
Correct |
279 ms |
38692 KB |
Output is correct |
9 |
Correct |
277 ms |
33696 KB |
Output is correct |
10 |
Correct |
268 ms |
37516 KB |
Output is correct |
11 |
Correct |
256 ms |
31500 KB |
Output is correct |
12 |
Correct |
256 ms |
31392 KB |
Output is correct |
13 |
Correct |
280 ms |
36768 KB |
Output is correct |
14 |
Correct |
265 ms |
32416 KB |
Output is correct |
15 |
Correct |
268 ms |
38408 KB |
Output is correct |
16 |
Correct |
15 ms |
5724 KB |
Output is correct |
17 |
Correct |
280 ms |
35152 KB |
Output is correct |
18 |
Correct |
239 ms |
28844 KB |
Output is correct |
19 |
Correct |
61 ms |
11652 KB |
Output is correct |
20 |
Correct |
64 ms |
13456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
6684 KB |
Output is correct |
2 |
Correct |
50 ms |
6664 KB |
Output is correct |
3 |
Correct |
62 ms |
6512 KB |
Output is correct |
4 |
Correct |
38 ms |
6292 KB |
Output is correct |
5 |
Correct |
50 ms |
6600 KB |
Output is correct |
6 |
Correct |
39 ms |
6340 KB |
Output is correct |
7 |
Correct |
44 ms |
6600 KB |
Output is correct |
8 |
Correct |
45 ms |
6260 KB |
Output is correct |
9 |
Correct |
44 ms |
6480 KB |
Output is correct |
10 |
Correct |
41 ms |
6336 KB |
Output is correct |
11 |
Correct |
39 ms |
6340 KB |
Output is correct |
12 |
Correct |
37 ms |
6188 KB |
Output is correct |
13 |
Correct |
37 ms |
6344 KB |
Output is correct |
14 |
Correct |
39 ms |
6344 KB |
Output is correct |
15 |
Correct |
36 ms |
6088 KB |
Output is correct |
16 |
Correct |
9 ms |
3160 KB |
Output is correct |
17 |
Correct |
46 ms |
6140 KB |
Output is correct |
18 |
Correct |
34 ms |
5708 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
280 ms |
27292 KB |
Output is correct |
2 |
Correct |
302 ms |
27300 KB |
Output is correct |
3 |
Correct |
279 ms |
26532 KB |
Output is correct |
4 |
Correct |
281 ms |
25252 KB |
Output is correct |
5 |
Correct |
291 ms |
27044 KB |
Output is correct |
6 |
Correct |
266 ms |
25508 KB |
Output is correct |
7 |
Correct |
277 ms |
27232 KB |
Output is correct |
8 |
Correct |
267 ms |
25764 KB |
Output is correct |
9 |
Correct |
274 ms |
25252 KB |
Output is correct |
10 |
Correct |
274 ms |
25760 KB |
Output is correct |
11 |
Correct |
279 ms |
25760 KB |
Output is correct |
12 |
Correct |
284 ms |
24816 KB |
Output is correct |
13 |
Correct |
284 ms |
25524 KB |
Output is correct |
14 |
Correct |
286 ms |
26532 KB |
Output is correct |
15 |
Correct |
284 ms |
26272 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
284 ms |
25052 KB |
Output is correct |
18 |
Correct |
280 ms |
24732 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
340 ms |
40972 KB |
Output is correct |
22 |
Correct |
311 ms |
40488 KB |
Output is correct |
23 |
Correct |
278 ms |
33268 KB |
Output is correct |
24 |
Correct |
287 ms |
32928 KB |
Output is correct |
25 |
Correct |
295 ms |
33460 KB |
Output is correct |
26 |
Correct |
262 ms |
32336 KB |
Output is correct |
27 |
Correct |
313 ms |
40152 KB |
Output is correct |
28 |
Correct |
279 ms |
38692 KB |
Output is correct |
29 |
Correct |
277 ms |
33696 KB |
Output is correct |
30 |
Correct |
268 ms |
37516 KB |
Output is correct |
31 |
Correct |
256 ms |
31500 KB |
Output is correct |
32 |
Correct |
256 ms |
31392 KB |
Output is correct |
33 |
Correct |
280 ms |
36768 KB |
Output is correct |
34 |
Correct |
265 ms |
32416 KB |
Output is correct |
35 |
Correct |
268 ms |
38408 KB |
Output is correct |
36 |
Correct |
15 ms |
5724 KB |
Output is correct |
37 |
Correct |
280 ms |
35152 KB |
Output is correct |
38 |
Correct |
239 ms |
28844 KB |
Output is correct |
39 |
Correct |
61 ms |
11652 KB |
Output is correct |
40 |
Correct |
64 ms |
13456 KB |
Output is correct |
41 |
Correct |
56 ms |
6684 KB |
Output is correct |
42 |
Correct |
50 ms |
6664 KB |
Output is correct |
43 |
Correct |
62 ms |
6512 KB |
Output is correct |
44 |
Correct |
38 ms |
6292 KB |
Output is correct |
45 |
Correct |
50 ms |
6600 KB |
Output is correct |
46 |
Correct |
39 ms |
6340 KB |
Output is correct |
47 |
Correct |
44 ms |
6600 KB |
Output is correct |
48 |
Correct |
45 ms |
6260 KB |
Output is correct |
49 |
Correct |
44 ms |
6480 KB |
Output is correct |
50 |
Correct |
41 ms |
6336 KB |
Output is correct |
51 |
Correct |
39 ms |
6340 KB |
Output is correct |
52 |
Correct |
37 ms |
6188 KB |
Output is correct |
53 |
Correct |
37 ms |
6344 KB |
Output is correct |
54 |
Correct |
39 ms |
6344 KB |
Output is correct |
55 |
Correct |
36 ms |
6088 KB |
Output is correct |
56 |
Correct |
9 ms |
3160 KB |
Output is correct |
57 |
Correct |
46 ms |
6140 KB |
Output is correct |
58 |
Correct |
34 ms |
5708 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
348 KB |
Output is correct |
61 |
Correct |
418 ms |
41544 KB |
Output is correct |
62 |
Correct |
447 ms |
40416 KB |
Output is correct |
63 |
Correct |
416 ms |
39352 KB |
Output is correct |
64 |
Correct |
341 ms |
38652 KB |
Output is correct |
65 |
Correct |
371 ms |
40224 KB |
Output is correct |
66 |
Correct |
340 ms |
38560 KB |
Output is correct |
67 |
Correct |
352 ms |
39848 KB |
Output is correct |
68 |
Correct |
353 ms |
38220 KB |
Output is correct |
69 |
Correct |
348 ms |
38308 KB |
Output is correct |
70 |
Correct |
330 ms |
38052 KB |
Output is correct |
71 |
Correct |
312 ms |
36840 KB |
Output is correct |
72 |
Correct |
331 ms |
37596 KB |
Output is correct |
73 |
Correct |
341 ms |
37540 KB |
Output is correct |
74 |
Correct |
339 ms |
38648 KB |
Output is correct |
75 |
Correct |
337 ms |
38620 KB |
Output is correct |
76 |
Correct |
16 ms |
5724 KB |
Output is correct |
77 |
Correct |
329 ms |
35464 KB |
Output is correct |
78 |
Correct |
317 ms |
34476 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
0 ms |
348 KB |
Output is correct |