#include <bits/stdc++.h>
#define pii pair<int, int>
using namespace std;
const int SZ = 1 << 18;
int A[SZ], B[SZ], R[SZ], IB[SZ], ans[1000007];
struct Seg {
int T[SZ << 1];
void Update(int i, int v) {
T[i += SZ - 1] = v;
while (i >>= 1) T[i] = T[i * 2] + T[i * 2 + 1];
}
pii Query(int k) {
int sL = 1, sR = SZ, n = 1;
while (sL != sR) {
int mid = (sL + sR) >> 1;
if (k <= T[n * 2]) sR = mid, n = n * 2;
else sL = mid + 1, k -= T[n * 2], n = n * 2 + 1;
}
return {sL, k};
}
} T;
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int N, Q;
cin >> N >> Q;
for (int i = 1; i <= N; ++i) cin >> A[i];
map<int, vector<pii>> P;
for (int i = 1; i <= Q; ++i) {
int a, b;
cin >> a >> b;
P[a].emplace_back(b, i);
}
if (P.count(0)) for (auto [a, b] : P[0]) ans[b] = A[a];
int a = 1, b = (N / 2) + 1, idx = 1;
while (a <= N / 2 || b <= N) {
if (N < b) B[idx++] = A[a++];
else if (N / 2 < a) B[idx++] = A[b++];
else B[idx++] = A[(A[a] < A[b] ? a : b)++];
}
vector<int> ST;
memset(R, 0x3f, sizeof(R));
for (int i = 1; i <= N; ++i) {
IB[B[i]] = i;
while (!ST.empty() && B[ST.back()] < B[i]) {
R[ST.back()] = i;
ST.pop_back();
}
ST.push_back(i);
}
priority_queue<tuple<int, int, int>> PQ;
int Z = N, l = 1;
for (int i = 1; i <= N; ++i) {
if (B[l] < B[i]) {
PQ.emplace(B[l], l, i - 1);
T.Update(B[l], i - l);
l = i;
}
}
PQ.emplace(B[l], l, N);
T.Update(B[l], N + 1 - l);
int turn = 1;
while (1) {
if (P.count(turn)) {
for (auto [k, id] : P[turn]) {
auto [p, off] = T.Query(k);
ans[id] = B[IB[p] + off - 1];
}
}
tuple<int, int, int> last;
while (N / 2 < Z) {
last = PQ.top(); PQ.pop();
auto [_, a, b] = last;
Z -= b - a + 1;
}
if (Z == N / 2) break;
turn++;
auto [v, a, b] = last;
int m = a + (N / 2 - Z) - 1;
PQ.emplace(v, a, m);
T.Update(v, m - a + 1);
int l = m + 1;
while (l <= b) {
int r = min(R[l] - 1, b);
PQ.emplace(B[l], l, r);
T.Update(B[l], r - l + 1);
l = r + 1;
}
Z += b - a + 1;
}
for (auto& [_, v] : P) for (auto [k, id] : v) {
if (ans[id]) continue;
auto [p, off] = T.Query(k);
ans[id] = B[IB[p] + off - 1];
}
for (int i = 1; i <= Q; ++i) cout << ans[i] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
227 ms |
23888 KB |
Output is correct |
2 |
Correct |
235 ms |
28444 KB |
Output is correct |
3 |
Correct |
227 ms |
27832 KB |
Output is correct |
4 |
Correct |
179 ms |
26544 KB |
Output is correct |
5 |
Correct |
204 ms |
29528 KB |
Output is correct |
6 |
Correct |
204 ms |
30288 KB |
Output is correct |
7 |
Correct |
209 ms |
31124 KB |
Output is correct |
8 |
Correct |
189 ms |
28992 KB |
Output is correct |
9 |
Correct |
187 ms |
27424 KB |
Output is correct |
10 |
Correct |
193 ms |
27204 KB |
Output is correct |
11 |
Correct |
193 ms |
28024 KB |
Output is correct |
12 |
Correct |
163 ms |
25376 KB |
Output is correct |
13 |
Correct |
177 ms |
25812 KB |
Output is correct |
14 |
Correct |
217 ms |
29388 KB |
Output is correct |
15 |
Correct |
184 ms |
27212 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
138 ms |
25556 KB |
Output is correct |
18 |
Correct |
148 ms |
26036 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
244 ms |
27628 KB |
Output is correct |
2 |
Correct |
234 ms |
42676 KB |
Output is correct |
3 |
Correct |
191 ms |
36280 KB |
Output is correct |
4 |
Correct |
165 ms |
34532 KB |
Output is correct |
5 |
Correct |
176 ms |
33976 KB |
Output is correct |
6 |
Correct |
197 ms |
33356 KB |
Output is correct |
7 |
Correct |
210 ms |
38324 KB |
Output is correct |
8 |
Correct |
232 ms |
37488 KB |
Output is correct |
9 |
Correct |
169 ms |
35028 KB |
Output is correct |
10 |
Correct |
188 ms |
35512 KB |
Output is correct |
11 |
Correct |
155 ms |
32204 KB |
Output is correct |
12 |
Correct |
152 ms |
31160 KB |
Output is correct |
13 |
Correct |
180 ms |
34592 KB |
Output is correct |
14 |
Correct |
171 ms |
31928 KB |
Output is correct |
15 |
Correct |
186 ms |
36020 KB |
Output is correct |
16 |
Correct |
15 ms |
9040 KB |
Output is correct |
17 |
Correct |
166 ms |
39096 KB |
Output is correct |
18 |
Correct |
135 ms |
30904 KB |
Output is correct |
19 |
Correct |
44 ms |
15828 KB |
Output is correct |
20 |
Correct |
53 ms |
15736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
17744 KB |
Output is correct |
2 |
Correct |
69 ms |
14544 KB |
Output is correct |
3 |
Correct |
78 ms |
15436 KB |
Output is correct |
4 |
Correct |
46 ms |
12308 KB |
Output is correct |
5 |
Correct |
67 ms |
15288 KB |
Output is correct |
6 |
Correct |
45 ms |
12676 KB |
Output is correct |
7 |
Correct |
58 ms |
14156 KB |
Output is correct |
8 |
Correct |
50 ms |
12972 KB |
Output is correct |
9 |
Correct |
59 ms |
14188 KB |
Output is correct |
10 |
Correct |
32 ms |
11092 KB |
Output is correct |
11 |
Correct |
33 ms |
11348 KB |
Output is correct |
12 |
Correct |
30 ms |
11088 KB |
Output is correct |
13 |
Correct |
35 ms |
11092 KB |
Output is correct |
14 |
Correct |
35 ms |
11356 KB |
Output is correct |
15 |
Correct |
28 ms |
10832 KB |
Output is correct |
16 |
Correct |
7 ms |
7004 KB |
Output is correct |
17 |
Correct |
31 ms |
12324 KB |
Output is correct |
18 |
Correct |
22 ms |
10980 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
227 ms |
23888 KB |
Output is correct |
2 |
Correct |
235 ms |
28444 KB |
Output is correct |
3 |
Correct |
227 ms |
27832 KB |
Output is correct |
4 |
Correct |
179 ms |
26544 KB |
Output is correct |
5 |
Correct |
204 ms |
29528 KB |
Output is correct |
6 |
Correct |
204 ms |
30288 KB |
Output is correct |
7 |
Correct |
209 ms |
31124 KB |
Output is correct |
8 |
Correct |
189 ms |
28992 KB |
Output is correct |
9 |
Correct |
187 ms |
27424 KB |
Output is correct |
10 |
Correct |
193 ms |
27204 KB |
Output is correct |
11 |
Correct |
193 ms |
28024 KB |
Output is correct |
12 |
Correct |
163 ms |
25376 KB |
Output is correct |
13 |
Correct |
177 ms |
25812 KB |
Output is correct |
14 |
Correct |
217 ms |
29388 KB |
Output is correct |
15 |
Correct |
184 ms |
27212 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
138 ms |
25556 KB |
Output is correct |
18 |
Correct |
148 ms |
26036 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
244 ms |
27628 KB |
Output is correct |
22 |
Correct |
234 ms |
42676 KB |
Output is correct |
23 |
Correct |
191 ms |
36280 KB |
Output is correct |
24 |
Correct |
165 ms |
34532 KB |
Output is correct |
25 |
Correct |
176 ms |
33976 KB |
Output is correct |
26 |
Correct |
197 ms |
33356 KB |
Output is correct |
27 |
Correct |
210 ms |
38324 KB |
Output is correct |
28 |
Correct |
232 ms |
37488 KB |
Output is correct |
29 |
Correct |
169 ms |
35028 KB |
Output is correct |
30 |
Correct |
188 ms |
35512 KB |
Output is correct |
31 |
Correct |
155 ms |
32204 KB |
Output is correct |
32 |
Correct |
152 ms |
31160 KB |
Output is correct |
33 |
Correct |
180 ms |
34592 KB |
Output is correct |
34 |
Correct |
171 ms |
31928 KB |
Output is correct |
35 |
Correct |
186 ms |
36020 KB |
Output is correct |
36 |
Correct |
15 ms |
9040 KB |
Output is correct |
37 |
Correct |
166 ms |
39096 KB |
Output is correct |
38 |
Correct |
135 ms |
30904 KB |
Output is correct |
39 |
Correct |
44 ms |
15828 KB |
Output is correct |
40 |
Correct |
53 ms |
15736 KB |
Output is correct |
41 |
Correct |
79 ms |
17744 KB |
Output is correct |
42 |
Correct |
69 ms |
14544 KB |
Output is correct |
43 |
Correct |
78 ms |
15436 KB |
Output is correct |
44 |
Correct |
46 ms |
12308 KB |
Output is correct |
45 |
Correct |
67 ms |
15288 KB |
Output is correct |
46 |
Correct |
45 ms |
12676 KB |
Output is correct |
47 |
Correct |
58 ms |
14156 KB |
Output is correct |
48 |
Correct |
50 ms |
12972 KB |
Output is correct |
49 |
Correct |
59 ms |
14188 KB |
Output is correct |
50 |
Correct |
32 ms |
11092 KB |
Output is correct |
51 |
Correct |
33 ms |
11348 KB |
Output is correct |
52 |
Correct |
30 ms |
11088 KB |
Output is correct |
53 |
Correct |
35 ms |
11092 KB |
Output is correct |
54 |
Correct |
35 ms |
11356 KB |
Output is correct |
55 |
Correct |
28 ms |
10832 KB |
Output is correct |
56 |
Correct |
7 ms |
7004 KB |
Output is correct |
57 |
Correct |
31 ms |
12324 KB |
Output is correct |
58 |
Correct |
22 ms |
10980 KB |
Output is correct |
59 |
Correct |
1 ms |
6492 KB |
Output is correct |
60 |
Correct |
1 ms |
6492 KB |
Output is correct |
61 |
Correct |
850 ms |
65168 KB |
Output is correct |
62 |
Correct |
630 ms |
51772 KB |
Output is correct |
63 |
Correct |
624 ms |
54544 KB |
Output is correct |
64 |
Correct |
490 ms |
49092 KB |
Output is correct |
65 |
Correct |
516 ms |
51308 KB |
Output is correct |
66 |
Correct |
493 ms |
48972 KB |
Output is correct |
67 |
Correct |
549 ms |
44392 KB |
Output is correct |
68 |
Correct |
450 ms |
42804 KB |
Output is correct |
69 |
Correct |
448 ms |
46372 KB |
Output is correct |
70 |
Correct |
341 ms |
39700 KB |
Output is correct |
71 |
Correct |
260 ms |
40788 KB |
Output is correct |
72 |
Correct |
356 ms |
40332 KB |
Output is correct |
73 |
Correct |
278 ms |
40304 KB |
Output is correct |
74 |
Correct |
344 ms |
42576 KB |
Output is correct |
75 |
Correct |
315 ms |
41068 KB |
Output is correct |
76 |
Correct |
15 ms |
9052 KB |
Output is correct |
77 |
Correct |
192 ms |
39772 KB |
Output is correct |
78 |
Correct |
191 ms |
34364 KB |
Output is correct |
79 |
Correct |
1 ms |
6492 KB |
Output is correct |
80 |
Correct |
1 ms |
6492 KB |
Output is correct |