#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define md ((l + r) >> 1)
const int N = 200'005;
vector<array<int, 2>> ask[N];
int a[N], ans[1'000'005];
struct segtree {
int seg[1 << 19];
int n;
void build(int i, int l, int r) {
if (l == r) {
seg[i] = 0;
return;
}
build(i << 1, l, md);
build(i << 1 | 1, md + 1, r);
seg[i] = seg[i << 1] + seg[i << 1 | 1];
}
void init(int _n) {
n = _n;
build(1, 0, n - 1);
}
void upd(int i, int l, int r, int p, int x) {
if (l == r) {
seg[i] = x;
return;
}
if (p <= md) {
upd(i << 1, l, md, p, x);
} else {
upd(i << 1 | 1, md + 1, r, p, x);
}
seg[i] = seg[i << 1] + seg[i << 1 | 1];
}
void upd(int i, int x) {
upd(1, 0, n - 1, i, x);
}
array<int, 2> get(int i, int l, int r, int p) {
if (l == r) {
return {l, p};
}
if (p < seg[i << 1]) {
return get(i << 1, l, md, p);
} else {
return get(i << 1 | 1, md + 1, r, p - seg[i << 1]);
}
}
array<int, 2> get(int p) {
return get(1, 0, n - 1, p);
}
int get2(int i, int l, int r, int p) {
if (l == r) {
return seg[i];
}
if (p <= md) {
return get2(i << 1, l, md, p);
} else {
return get2(i << 1 | 1, md + 1, r, p);
}
}
int get2(int p) {
return get2(1, 0, n - 1, p);
}
} seg;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n, q;
cin >> n >> q;
int pos[n + 1];
for (int i = 0; i < n; i++) {
cin >> a[i];
pos[a[i]] = i;
}
for (int i = 0; i < q; i++) {
int t, p;
cin >> t >> p;
t = min(t, n);
ask[t].push_back({p - 1, i});
}
int nxt[n]{};
stack<int> st;
a[n] = 1e9;
st.push(n);
for (int i = n - 1; i >= 0; i--) {
while (a[i] > a[st.top()]) st.pop();
nxt[i] = st.top();
st.push(i);
}
seg.init(n + 1);
for (int i = 0; i < n; i = nxt[i]) {
seg.upd(a[i], nxt[i] - i);
}
bool isover = 0;
for (int j = 0; j <= n; j++) {
for (auto [p, idx] : ask[j]) {
auto [i, v] = seg.get(p);
ans[idx] = a[pos[i] + v];
}
if (isover) continue;
int cur = n / 2;
array<int, 2> V = seg.get(cur);
int i = pos[V[0]];
cur = V[1];
if (cur == 0) {
isover = 1;
continue;
}
int mx = i + seg.get2(a[i]);
seg.upd(a[i], cur);
cur = i + cur;
while (cur < n && seg.get2(a[cur]) == 0) {
int newcur = min(nxt[cur], mx);
seg.upd(a[cur], newcur - cur);
cur = newcur;
}
}
for (int i = 0; i < q; i++) {
cout << ans[i] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
193 ms |
25936 KB |
Output is correct |
2 |
Correct |
198 ms |
24656 KB |
Output is correct |
3 |
Correct |
187 ms |
24272 KB |
Output is correct |
4 |
Correct |
153 ms |
24128 KB |
Output is correct |
5 |
Correct |
174 ms |
26196 KB |
Output is correct |
6 |
Correct |
153 ms |
26832 KB |
Output is correct |
7 |
Correct |
170 ms |
27012 KB |
Output is correct |
8 |
Correct |
156 ms |
25680 KB |
Output is correct |
9 |
Correct |
156 ms |
24476 KB |
Output is correct |
10 |
Correct |
160 ms |
24332 KB |
Output is correct |
11 |
Correct |
157 ms |
24620 KB |
Output is correct |
12 |
Correct |
154 ms |
22840 KB |
Output is correct |
13 |
Correct |
165 ms |
23688 KB |
Output is correct |
14 |
Correct |
166 ms |
25684 KB |
Output is correct |
15 |
Correct |
167 ms |
24004 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
193 ms |
23252 KB |
Output is correct |
18 |
Correct |
148 ms |
23028 KB |
Output is correct |
19 |
Correct |
2 ms |
9564 KB |
Output is correct |
20 |
Correct |
2 ms |
9432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
269 ms |
27656 KB |
Output is correct |
2 |
Correct |
258 ms |
42172 KB |
Output is correct |
3 |
Correct |
205 ms |
36020 KB |
Output is correct |
4 |
Correct |
199 ms |
37392 KB |
Output is correct |
5 |
Correct |
219 ms |
36980 KB |
Output is correct |
6 |
Correct |
190 ms |
37108 KB |
Output is correct |
7 |
Correct |
220 ms |
41016 KB |
Output is correct |
8 |
Correct |
222 ms |
39996 KB |
Output is correct |
9 |
Correct |
193 ms |
37020 KB |
Output is correct |
10 |
Correct |
200 ms |
38736 KB |
Output is correct |
11 |
Correct |
165 ms |
34896 KB |
Output is correct |
12 |
Correct |
166 ms |
35084 KB |
Output is correct |
13 |
Correct |
197 ms |
38072 KB |
Output is correct |
14 |
Correct |
172 ms |
35508 KB |
Output is correct |
15 |
Correct |
207 ms |
39608 KB |
Output is correct |
16 |
Correct |
19 ms |
12472 KB |
Output is correct |
17 |
Correct |
261 ms |
36892 KB |
Output is correct |
18 |
Correct |
147 ms |
33388 KB |
Output is correct |
19 |
Correct |
52 ms |
19228 KB |
Output is correct |
20 |
Correct |
62 ms |
20680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
15160 KB |
Output is correct |
2 |
Correct |
61 ms |
16576 KB |
Output is correct |
3 |
Correct |
55 ms |
16728 KB |
Output is correct |
4 |
Correct |
36 ms |
16152 KB |
Output is correct |
5 |
Correct |
53 ms |
16468 KB |
Output is correct |
6 |
Correct |
38 ms |
16208 KB |
Output is correct |
7 |
Correct |
47 ms |
16464 KB |
Output is correct |
8 |
Correct |
41 ms |
16000 KB |
Output is correct |
9 |
Correct |
46 ms |
16212 KB |
Output is correct |
10 |
Correct |
30 ms |
15728 KB |
Output is correct |
11 |
Correct |
33 ms |
16088 KB |
Output is correct |
12 |
Correct |
30 ms |
15952 KB |
Output is correct |
13 |
Correct |
32 ms |
15708 KB |
Output is correct |
14 |
Correct |
32 ms |
15956 KB |
Output is correct |
15 |
Correct |
30 ms |
15700 KB |
Output is correct |
16 |
Correct |
10 ms |
11096 KB |
Output is correct |
17 |
Correct |
37 ms |
15660 KB |
Output is correct |
18 |
Correct |
28 ms |
15316 KB |
Output is correct |
19 |
Correct |
2 ms |
9560 KB |
Output is correct |
20 |
Correct |
2 ms |
9564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
193 ms |
25936 KB |
Output is correct |
2 |
Correct |
198 ms |
24656 KB |
Output is correct |
3 |
Correct |
187 ms |
24272 KB |
Output is correct |
4 |
Correct |
153 ms |
24128 KB |
Output is correct |
5 |
Correct |
174 ms |
26196 KB |
Output is correct |
6 |
Correct |
153 ms |
26832 KB |
Output is correct |
7 |
Correct |
170 ms |
27012 KB |
Output is correct |
8 |
Correct |
156 ms |
25680 KB |
Output is correct |
9 |
Correct |
156 ms |
24476 KB |
Output is correct |
10 |
Correct |
160 ms |
24332 KB |
Output is correct |
11 |
Correct |
157 ms |
24620 KB |
Output is correct |
12 |
Correct |
154 ms |
22840 KB |
Output is correct |
13 |
Correct |
165 ms |
23688 KB |
Output is correct |
14 |
Correct |
166 ms |
25684 KB |
Output is correct |
15 |
Correct |
167 ms |
24004 KB |
Output is correct |
16 |
Correct |
2 ms |
9564 KB |
Output is correct |
17 |
Correct |
193 ms |
23252 KB |
Output is correct |
18 |
Correct |
148 ms |
23028 KB |
Output is correct |
19 |
Correct |
2 ms |
9564 KB |
Output is correct |
20 |
Correct |
2 ms |
9432 KB |
Output is correct |
21 |
Correct |
269 ms |
27656 KB |
Output is correct |
22 |
Correct |
258 ms |
42172 KB |
Output is correct |
23 |
Correct |
205 ms |
36020 KB |
Output is correct |
24 |
Correct |
199 ms |
37392 KB |
Output is correct |
25 |
Correct |
219 ms |
36980 KB |
Output is correct |
26 |
Correct |
190 ms |
37108 KB |
Output is correct |
27 |
Correct |
220 ms |
41016 KB |
Output is correct |
28 |
Correct |
222 ms |
39996 KB |
Output is correct |
29 |
Correct |
193 ms |
37020 KB |
Output is correct |
30 |
Correct |
200 ms |
38736 KB |
Output is correct |
31 |
Correct |
165 ms |
34896 KB |
Output is correct |
32 |
Correct |
166 ms |
35084 KB |
Output is correct |
33 |
Correct |
197 ms |
38072 KB |
Output is correct |
34 |
Correct |
172 ms |
35508 KB |
Output is correct |
35 |
Correct |
207 ms |
39608 KB |
Output is correct |
36 |
Correct |
19 ms |
12472 KB |
Output is correct |
37 |
Correct |
261 ms |
36892 KB |
Output is correct |
38 |
Correct |
147 ms |
33388 KB |
Output is correct |
39 |
Correct |
52 ms |
19228 KB |
Output is correct |
40 |
Correct |
62 ms |
20680 KB |
Output is correct |
41 |
Correct |
75 ms |
15160 KB |
Output is correct |
42 |
Correct |
61 ms |
16576 KB |
Output is correct |
43 |
Correct |
55 ms |
16728 KB |
Output is correct |
44 |
Correct |
36 ms |
16152 KB |
Output is correct |
45 |
Correct |
53 ms |
16468 KB |
Output is correct |
46 |
Correct |
38 ms |
16208 KB |
Output is correct |
47 |
Correct |
47 ms |
16464 KB |
Output is correct |
48 |
Correct |
41 ms |
16000 KB |
Output is correct |
49 |
Correct |
46 ms |
16212 KB |
Output is correct |
50 |
Correct |
30 ms |
15728 KB |
Output is correct |
51 |
Correct |
33 ms |
16088 KB |
Output is correct |
52 |
Correct |
30 ms |
15952 KB |
Output is correct |
53 |
Correct |
32 ms |
15708 KB |
Output is correct |
54 |
Correct |
32 ms |
15956 KB |
Output is correct |
55 |
Correct |
30 ms |
15700 KB |
Output is correct |
56 |
Correct |
10 ms |
11096 KB |
Output is correct |
57 |
Correct |
37 ms |
15660 KB |
Output is correct |
58 |
Correct |
28 ms |
15316 KB |
Output is correct |
59 |
Correct |
2 ms |
9560 KB |
Output is correct |
60 |
Correct |
2 ms |
9564 KB |
Output is correct |
61 |
Correct |
436 ms |
48692 KB |
Output is correct |
62 |
Correct |
376 ms |
47148 KB |
Output is correct |
63 |
Correct |
391 ms |
46240 KB |
Output is correct |
64 |
Correct |
304 ms |
45300 KB |
Output is correct |
65 |
Correct |
316 ms |
46884 KB |
Output is correct |
66 |
Correct |
321 ms |
45612 KB |
Output is correct |
67 |
Correct |
274 ms |
44624 KB |
Output is correct |
68 |
Correct |
282 ms |
43316 KB |
Output is correct |
69 |
Correct |
311 ms |
45428 KB |
Output is correct |
70 |
Correct |
252 ms |
41556 KB |
Output is correct |
71 |
Correct |
240 ms |
42944 KB |
Output is correct |
72 |
Correct |
247 ms |
42072 KB |
Output is correct |
73 |
Correct |
242 ms |
42460 KB |
Output is correct |
74 |
Correct |
253 ms |
44376 KB |
Output is correct |
75 |
Correct |
253 ms |
43012 KB |
Output is correct |
76 |
Correct |
22 ms |
12624 KB |
Output is correct |
77 |
Correct |
294 ms |
37164 KB |
Output is correct |
78 |
Correct |
198 ms |
36392 KB |
Output is correct |
79 |
Correct |
2 ms |
9560 KB |
Output is correct |
80 |
Correct |
2 ms |
9560 KB |
Output is correct |