#include <bits/stdc++.h>
using namespace std;
const int SZ = 1 << 18;
int st[2 * SZ];
void update(int i, int v) {
for (st[i += SZ] = v; i /= 2;) st[i] = st[2 * i] + st[2 * i + 1];
}
pair<int, int> search(int x) {
int u = 1;
while (u < SZ) {
u *= 2;
if (st[u] <= x) {
x -= st[u];
u++;
}
}
return {u - SZ, x};
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, q;
cin >> n >> q;
vector<int> a(n), r(n), pos(n);
vector<pair<int, int>> query[n + 1];
for (int i = 0; i < n; i++) {
cin >> a[i];
--a[i];
pos[a[i]] = i;
}
for (int i = n - 1; i >= 0; i--) {
r[i] = i + 1;
while (r[i] < n && a[r[i]] < a[i]) {
r[i] = r[r[i]];
}
}
for (int i = 0; i < n; i = r[i]) {
update(a[i], r[i] - i);
}
for (int idx = 0; idx < q; idx++) {
int t, i;
cin >> t >> i;
t = min(t, n);
--i;
query[t].emplace_back(i, idx);
}
vector<int> res(q);
for (int t = 0; t <= n; t++) {
for (auto [i, idx] : query[t]) {
auto [val, d] = search(i);
res[idx] = a[pos[val] + d];
}
auto [val, d] = search(n / 2);
if (d == 0) continue;
int tot = st[val + SZ];
update(val, d);
for (int i = pos[val] + d; i < pos[val] + tot; i = r[i]) {
update(a[i], min(r[i], pos[val] + tot) - i);
}
}
for (int i = 0; i < q; i++) {
cout << res[i] + 1 << '\n';
}
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:56:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
56 | for (auto [i, idx] : query[t]) {
| ^
Main.cpp:57:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
57 | auto [val, d] = search(i);
| ^
Main.cpp:60:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
60 | auto [val, d] = search(n / 2);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
176 ms |
18516 KB |
Output is correct |
2 |
Correct |
191 ms |
24660 KB |
Output is correct |
3 |
Correct |
187 ms |
24144 KB |
Output is correct |
4 |
Correct |
146 ms |
22740 KB |
Output is correct |
5 |
Correct |
152 ms |
26208 KB |
Output is correct |
6 |
Correct |
148 ms |
25948 KB |
Output is correct |
7 |
Correct |
168 ms |
27232 KB |
Output is correct |
8 |
Correct |
142 ms |
24672 KB |
Output is correct |
9 |
Correct |
154 ms |
23404 KB |
Output is correct |
10 |
Correct |
155 ms |
23380 KB |
Output is correct |
11 |
Correct |
142 ms |
23636 KB |
Output is correct |
12 |
Correct |
184 ms |
21136 KB |
Output is correct |
13 |
Correct |
156 ms |
22116 KB |
Output is correct |
14 |
Correct |
176 ms |
25248 KB |
Output is correct |
15 |
Correct |
160 ms |
23252 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
177 ms |
21320 KB |
Output is correct |
18 |
Correct |
148 ms |
21160 KB |
Output is correct |
19 |
Correct |
0 ms |
352 KB |
Output is correct |
20 |
Correct |
0 ms |
352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
225 ms |
27072 KB |
Output is correct |
2 |
Correct |
231 ms |
40640 KB |
Output is correct |
3 |
Correct |
176 ms |
34824 KB |
Output is correct |
4 |
Correct |
173 ms |
33980 KB |
Output is correct |
5 |
Correct |
181 ms |
35000 KB |
Output is correct |
6 |
Correct |
178 ms |
33212 KB |
Output is correct |
7 |
Correct |
216 ms |
40380 KB |
Output is correct |
8 |
Correct |
250 ms |
39284 KB |
Output is correct |
9 |
Correct |
170 ms |
34744 KB |
Output is correct |
10 |
Correct |
191 ms |
38368 KB |
Output is correct |
11 |
Correct |
157 ms |
33308 KB |
Output is correct |
12 |
Correct |
155 ms |
33872 KB |
Output is correct |
13 |
Correct |
177 ms |
36936 KB |
Output is correct |
14 |
Correct |
200 ms |
34232 KB |
Output is correct |
15 |
Correct |
199 ms |
38588 KB |
Output is correct |
16 |
Correct |
20 ms |
10076 KB |
Output is correct |
17 |
Correct |
202 ms |
35360 KB |
Output is correct |
18 |
Correct |
136 ms |
30456 KB |
Output is correct |
19 |
Correct |
59 ms |
15052 KB |
Output is correct |
20 |
Correct |
59 ms |
16584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
7760 KB |
Output is correct |
2 |
Correct |
42 ms |
8784 KB |
Output is correct |
3 |
Correct |
46 ms |
8812 KB |
Output is correct |
4 |
Correct |
35 ms |
8536 KB |
Output is correct |
5 |
Correct |
50 ms |
9044 KB |
Output is correct |
6 |
Correct |
35 ms |
8400 KB |
Output is correct |
7 |
Correct |
37 ms |
9048 KB |
Output is correct |
8 |
Correct |
36 ms |
8528 KB |
Output is correct |
9 |
Correct |
39 ms |
8784 KB |
Output is correct |
10 |
Correct |
29 ms |
8300 KB |
Output is correct |
11 |
Correct |
28 ms |
8528 KB |
Output is correct |
12 |
Correct |
28 ms |
8288 KB |
Output is correct |
13 |
Correct |
30 ms |
8024 KB |
Output is correct |
14 |
Correct |
29 ms |
8536 KB |
Output is correct |
15 |
Correct |
27 ms |
8288 KB |
Output is correct |
16 |
Correct |
11 ms |
5208 KB |
Output is correct |
17 |
Correct |
36 ms |
7632 KB |
Output is correct |
18 |
Correct |
24 ms |
6864 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
176 ms |
18516 KB |
Output is correct |
2 |
Correct |
191 ms |
24660 KB |
Output is correct |
3 |
Correct |
187 ms |
24144 KB |
Output is correct |
4 |
Correct |
146 ms |
22740 KB |
Output is correct |
5 |
Correct |
152 ms |
26208 KB |
Output is correct |
6 |
Correct |
148 ms |
25948 KB |
Output is correct |
7 |
Correct |
168 ms |
27232 KB |
Output is correct |
8 |
Correct |
142 ms |
24672 KB |
Output is correct |
9 |
Correct |
154 ms |
23404 KB |
Output is correct |
10 |
Correct |
155 ms |
23380 KB |
Output is correct |
11 |
Correct |
142 ms |
23636 KB |
Output is correct |
12 |
Correct |
184 ms |
21136 KB |
Output is correct |
13 |
Correct |
156 ms |
22116 KB |
Output is correct |
14 |
Correct |
176 ms |
25248 KB |
Output is correct |
15 |
Correct |
160 ms |
23252 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
177 ms |
21320 KB |
Output is correct |
18 |
Correct |
148 ms |
21160 KB |
Output is correct |
19 |
Correct |
0 ms |
352 KB |
Output is correct |
20 |
Correct |
0 ms |
352 KB |
Output is correct |
21 |
Correct |
225 ms |
27072 KB |
Output is correct |
22 |
Correct |
231 ms |
40640 KB |
Output is correct |
23 |
Correct |
176 ms |
34824 KB |
Output is correct |
24 |
Correct |
173 ms |
33980 KB |
Output is correct |
25 |
Correct |
181 ms |
35000 KB |
Output is correct |
26 |
Correct |
178 ms |
33212 KB |
Output is correct |
27 |
Correct |
216 ms |
40380 KB |
Output is correct |
28 |
Correct |
250 ms |
39284 KB |
Output is correct |
29 |
Correct |
170 ms |
34744 KB |
Output is correct |
30 |
Correct |
191 ms |
38368 KB |
Output is correct |
31 |
Correct |
157 ms |
33308 KB |
Output is correct |
32 |
Correct |
155 ms |
33872 KB |
Output is correct |
33 |
Correct |
177 ms |
36936 KB |
Output is correct |
34 |
Correct |
200 ms |
34232 KB |
Output is correct |
35 |
Correct |
199 ms |
38588 KB |
Output is correct |
36 |
Correct |
20 ms |
10076 KB |
Output is correct |
37 |
Correct |
202 ms |
35360 KB |
Output is correct |
38 |
Correct |
136 ms |
30456 KB |
Output is correct |
39 |
Correct |
59 ms |
15052 KB |
Output is correct |
40 |
Correct |
59 ms |
16584 KB |
Output is correct |
41 |
Correct |
47 ms |
7760 KB |
Output is correct |
42 |
Correct |
42 ms |
8784 KB |
Output is correct |
43 |
Correct |
46 ms |
8812 KB |
Output is correct |
44 |
Correct |
35 ms |
8536 KB |
Output is correct |
45 |
Correct |
50 ms |
9044 KB |
Output is correct |
46 |
Correct |
35 ms |
8400 KB |
Output is correct |
47 |
Correct |
37 ms |
9048 KB |
Output is correct |
48 |
Correct |
36 ms |
8528 KB |
Output is correct |
49 |
Correct |
39 ms |
8784 KB |
Output is correct |
50 |
Correct |
29 ms |
8300 KB |
Output is correct |
51 |
Correct |
28 ms |
8528 KB |
Output is correct |
52 |
Correct |
28 ms |
8288 KB |
Output is correct |
53 |
Correct |
30 ms |
8024 KB |
Output is correct |
54 |
Correct |
29 ms |
8536 KB |
Output is correct |
55 |
Correct |
27 ms |
8288 KB |
Output is correct |
56 |
Correct |
11 ms |
5208 KB |
Output is correct |
57 |
Correct |
36 ms |
7632 KB |
Output is correct |
58 |
Correct |
24 ms |
6864 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
1 ms |
468 KB |
Output is correct |
61 |
Correct |
419 ms |
48208 KB |
Output is correct |
62 |
Correct |
343 ms |
46160 KB |
Output is correct |
63 |
Correct |
359 ms |
44872 KB |
Output is correct |
64 |
Correct |
282 ms |
44592 KB |
Output is correct |
65 |
Correct |
339 ms |
46416 KB |
Output is correct |
66 |
Correct |
271 ms |
44392 KB |
Output is correct |
67 |
Correct |
283 ms |
44116 KB |
Output is correct |
68 |
Correct |
281 ms |
40280 KB |
Output is correct |
69 |
Correct |
274 ms |
44712 KB |
Output is correct |
70 |
Correct |
240 ms |
40648 KB |
Output is correct |
71 |
Correct |
234 ms |
42168 KB |
Output is correct |
72 |
Correct |
265 ms |
41188 KB |
Output is correct |
73 |
Correct |
237 ms |
41600 KB |
Output is correct |
74 |
Correct |
245 ms |
43716 KB |
Output is correct |
75 |
Correct |
236 ms |
42392 KB |
Output is correct |
76 |
Correct |
21 ms |
10076 KB |
Output is correct |
77 |
Correct |
265 ms |
36360 KB |
Output is correct |
78 |
Correct |
179 ms |
33968 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
1 ms |
348 KB |
Output is correct |