#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5;
const int MAXQ = 1e6;
typedef pair<int, int> pii;
int nxt[MAXN];
int sz[MAXN];
int bit[2*MAXN];
int n_round;
int arr[MAXN];
int inv[MAXN];
int queries[MAXQ];
int ans[MAXQ];
pii sorted[MAXQ];
int n, q;
void upd(int x, int v) {
for (; x < n_round; x|=(x+1)) bit[x] += v;
}
pii get_pos(int l = 0, int r = n_round, int v = n/2) { // first pos such that including it is greater than v, along with how much v left
if (r == l+1) return pii(l, v);
if (bit[(l+r)/2-1] > v) return get_pos(l, (l+r)/2, v);
return get_pos((l+r)/2, r, v-bit[(l+r)/2-1]);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> n >> q;
n_round = 1<<(32-__builtin_clz(n-1));
vector<int> stck;
for (int i = 0; i < n; i++) {
cin >> arr[i]; arr[i]--;
inv[arr[i]] = i;
while (!stck.empty() && arr[i] > arr[stck.back()]) {
nxt[stck.back()] = i;
stck.pop_back();
}
stck.push_back(i);
}
for (int v: stck) nxt[v] = n;
int cur = 0;
while (cur != n) {
sz[arr[cur]] = nxt[cur]-cur;
upd(arr[cur], sz[arr[cur]]);
cur = nxt[cur];
}
for (int i = 0; i < q; i++) {
int p, t; cin >> t >> p;
queries[i] = p;
sorted[i] = pii(t, i);
}
sort(sorted, sorted+q);
int t = 0;
int j = 0;
while (j < q) {
while (j < q && sorted[j].first == t) {
pii p = get_pos(0, n_round, queries[sorted[j].second]-1);
ans[sorted[j].second] = (1+arr[inv[p.first]+p.second]);
j++;
}
t++;
pii p = get_pos();
if (p.second == 0) break; // stabilized
upd(p.first, -sz[p.first]+p.second);
sz[p.first] = p.second;
int cur = inv[p.first]+p.second;
while (cur < nxt[inv[p.first]]) {
assert(sz[arr[cur]] == 0);
nxt[cur] = min(nxt[cur], nxt[inv[p.first]]);
sz[arr[cur]] = nxt[cur]-cur;
upd(arr[cur], sz[arr[cur]]);
cur = nxt[cur];
}
nxt[inv[p.first]] = inv[p.first]+p.second;
}
while (j < q) {
pii p = get_pos(0, n_round, queries[sorted[j].second]-1);
ans[sorted[j].second] = 1+arr[inv[p.first]+p.second];
j++;
}
for (int i = 0; i < q; i++) cout << ans[i] << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
364 ms |
23044 KB |
Output is correct |
2 |
Correct |
382 ms |
27212 KB |
Output is correct |
3 |
Correct |
382 ms |
26324 KB |
Output is correct |
4 |
Correct |
313 ms |
25064 KB |
Output is correct |
5 |
Correct |
359 ms |
26828 KB |
Output is correct |
6 |
Correct |
313 ms |
25400 KB |
Output is correct |
7 |
Correct |
341 ms |
27032 KB |
Output is correct |
8 |
Correct |
316 ms |
25608 KB |
Output is correct |
9 |
Correct |
309 ms |
25240 KB |
Output is correct |
10 |
Correct |
324 ms |
25760 KB |
Output is correct |
11 |
Correct |
315 ms |
25668 KB |
Output is correct |
12 |
Correct |
289 ms |
24484 KB |
Output is correct |
13 |
Correct |
327 ms |
25500 KB |
Output is correct |
14 |
Correct |
339 ms |
26196 KB |
Output is correct |
15 |
Correct |
320 ms |
26168 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
360 ms |
24892 KB |
Output is correct |
18 |
Correct |
277 ms |
24612 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
351 ms |
29076 KB |
Output is correct |
2 |
Correct |
333 ms |
40100 KB |
Output is correct |
3 |
Correct |
258 ms |
32148 KB |
Output is correct |
4 |
Correct |
245 ms |
32188 KB |
Output is correct |
5 |
Correct |
251 ms |
32844 KB |
Output is correct |
6 |
Correct |
243 ms |
31648 KB |
Output is correct |
7 |
Correct |
348 ms |
39512 KB |
Output is correct |
8 |
Correct |
317 ms |
38076 KB |
Output is correct |
9 |
Correct |
256 ms |
32924 KB |
Output is correct |
10 |
Correct |
330 ms |
36852 KB |
Output is correct |
11 |
Correct |
281 ms |
31016 KB |
Output is correct |
12 |
Correct |
241 ms |
30812 KB |
Output is correct |
13 |
Correct |
296 ms |
36196 KB |
Output is correct |
14 |
Correct |
250 ms |
31748 KB |
Output is correct |
15 |
Correct |
326 ms |
37848 KB |
Output is correct |
16 |
Correct |
21 ms |
4996 KB |
Output is correct |
17 |
Correct |
289 ms |
34468 KB |
Output is correct |
18 |
Correct |
206 ms |
28220 KB |
Output is correct |
19 |
Correct |
85 ms |
10072 KB |
Output is correct |
20 |
Correct |
85 ms |
12776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
6480 KB |
Output is correct |
2 |
Correct |
57 ms |
6284 KB |
Output is correct |
3 |
Correct |
60 ms |
6252 KB |
Output is correct |
4 |
Correct |
46 ms |
5852 KB |
Output is correct |
5 |
Correct |
50 ms |
6140 KB |
Output is correct |
6 |
Correct |
44 ms |
5780 KB |
Output is correct |
7 |
Correct |
57 ms |
6132 KB |
Output is correct |
8 |
Correct |
48 ms |
5864 KB |
Output is correct |
9 |
Correct |
48 ms |
5996 KB |
Output is correct |
10 |
Correct |
45 ms |
5728 KB |
Output is correct |
11 |
Correct |
46 ms |
5904 KB |
Output is correct |
12 |
Correct |
45 ms |
5712 KB |
Output is correct |
13 |
Correct |
46 ms |
5768 KB |
Output is correct |
14 |
Correct |
51 ms |
5880 KB |
Output is correct |
15 |
Correct |
45 ms |
5788 KB |
Output is correct |
16 |
Correct |
11 ms |
2896 KB |
Output is correct |
17 |
Correct |
49 ms |
5540 KB |
Output is correct |
18 |
Correct |
37 ms |
5340 KB |
Output is correct |
19 |
Correct |
1 ms |
328 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
364 ms |
23044 KB |
Output is correct |
2 |
Correct |
382 ms |
27212 KB |
Output is correct |
3 |
Correct |
382 ms |
26324 KB |
Output is correct |
4 |
Correct |
313 ms |
25064 KB |
Output is correct |
5 |
Correct |
359 ms |
26828 KB |
Output is correct |
6 |
Correct |
313 ms |
25400 KB |
Output is correct |
7 |
Correct |
341 ms |
27032 KB |
Output is correct |
8 |
Correct |
316 ms |
25608 KB |
Output is correct |
9 |
Correct |
309 ms |
25240 KB |
Output is correct |
10 |
Correct |
324 ms |
25760 KB |
Output is correct |
11 |
Correct |
315 ms |
25668 KB |
Output is correct |
12 |
Correct |
289 ms |
24484 KB |
Output is correct |
13 |
Correct |
327 ms |
25500 KB |
Output is correct |
14 |
Correct |
339 ms |
26196 KB |
Output is correct |
15 |
Correct |
320 ms |
26168 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
360 ms |
24892 KB |
Output is correct |
18 |
Correct |
277 ms |
24612 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
351 ms |
29076 KB |
Output is correct |
22 |
Correct |
333 ms |
40100 KB |
Output is correct |
23 |
Correct |
258 ms |
32148 KB |
Output is correct |
24 |
Correct |
245 ms |
32188 KB |
Output is correct |
25 |
Correct |
251 ms |
32844 KB |
Output is correct |
26 |
Correct |
243 ms |
31648 KB |
Output is correct |
27 |
Correct |
348 ms |
39512 KB |
Output is correct |
28 |
Correct |
317 ms |
38076 KB |
Output is correct |
29 |
Correct |
256 ms |
32924 KB |
Output is correct |
30 |
Correct |
330 ms |
36852 KB |
Output is correct |
31 |
Correct |
281 ms |
31016 KB |
Output is correct |
32 |
Correct |
241 ms |
30812 KB |
Output is correct |
33 |
Correct |
296 ms |
36196 KB |
Output is correct |
34 |
Correct |
250 ms |
31748 KB |
Output is correct |
35 |
Correct |
326 ms |
37848 KB |
Output is correct |
36 |
Correct |
21 ms |
4996 KB |
Output is correct |
37 |
Correct |
289 ms |
34468 KB |
Output is correct |
38 |
Correct |
206 ms |
28220 KB |
Output is correct |
39 |
Correct |
85 ms |
10072 KB |
Output is correct |
40 |
Correct |
85 ms |
12776 KB |
Output is correct |
41 |
Correct |
57 ms |
6480 KB |
Output is correct |
42 |
Correct |
57 ms |
6284 KB |
Output is correct |
43 |
Correct |
60 ms |
6252 KB |
Output is correct |
44 |
Correct |
46 ms |
5852 KB |
Output is correct |
45 |
Correct |
50 ms |
6140 KB |
Output is correct |
46 |
Correct |
44 ms |
5780 KB |
Output is correct |
47 |
Correct |
57 ms |
6132 KB |
Output is correct |
48 |
Correct |
48 ms |
5864 KB |
Output is correct |
49 |
Correct |
48 ms |
5996 KB |
Output is correct |
50 |
Correct |
45 ms |
5728 KB |
Output is correct |
51 |
Correct |
46 ms |
5904 KB |
Output is correct |
52 |
Correct |
45 ms |
5712 KB |
Output is correct |
53 |
Correct |
46 ms |
5768 KB |
Output is correct |
54 |
Correct |
51 ms |
5880 KB |
Output is correct |
55 |
Correct |
45 ms |
5788 KB |
Output is correct |
56 |
Correct |
11 ms |
2896 KB |
Output is correct |
57 |
Correct |
49 ms |
5540 KB |
Output is correct |
58 |
Correct |
37 ms |
5340 KB |
Output is correct |
59 |
Correct |
1 ms |
328 KB |
Output is correct |
60 |
Correct |
1 ms |
332 KB |
Output is correct |
61 |
Correct |
518 ms |
40716 KB |
Output is correct |
62 |
Correct |
566 ms |
39876 KB |
Output is correct |
63 |
Correct |
539 ms |
38612 KB |
Output is correct |
64 |
Correct |
449 ms |
37824 KB |
Output is correct |
65 |
Correct |
463 ms |
39508 KB |
Output is correct |
66 |
Correct |
465 ms |
37868 KB |
Output is correct |
67 |
Correct |
446 ms |
39020 KB |
Output is correct |
68 |
Correct |
443 ms |
37580 KB |
Output is correct |
69 |
Correct |
455 ms |
37744 KB |
Output is correct |
70 |
Correct |
450 ms |
37304 KB |
Output is correct |
71 |
Correct |
479 ms |
36508 KB |
Output is correct |
72 |
Correct |
648 ms |
36956 KB |
Output is correct |
73 |
Correct |
453 ms |
36896 KB |
Output is correct |
74 |
Correct |
482 ms |
38036 KB |
Output is correct |
75 |
Correct |
560 ms |
37988 KB |
Output is correct |
76 |
Correct |
23 ms |
4952 KB |
Output is correct |
77 |
Correct |
527 ms |
34664 KB |
Output is correct |
78 |
Correct |
374 ms |
33928 KB |
Output is correct |
79 |
Correct |
1 ms |
340 KB |
Output is correct |
80 |
Correct |
1 ms |
328 KB |
Output is correct |