#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
const int Q = 1e6 + 10;
int n, in[N], a[N], xi[N], yi[N], nxt[N], q, px = 1, py = 1, po[N], ans[Q];
array<int, 3> qry[Q];
struct SegTree {
int size = 1;
vector<int> seg;
void init(int _n) {
while (size < _n) size *= 2;
seg.assign(2 * size + 10, 0);
}
void update(int pos, int v, int l, int r, int id) {
if (l == r) {
seg[id] += v;
return;
}
int mid = (l + r) / 2;
if (pos <= mid) update(pos, v, l, mid, id * 2);
else update(pos, v, mid + 1, r, id * 2 + 1);
seg[id] = seg[id * 2] + seg[id * 2 + 1];
}
int query(int ql, int qr, int l, int r, int id) {
if (qr < l || r < ql) return 0;
if (ql <= l && r <= qr) return seg[id];
int mid = (l + r) / 2;
return query(ql, qr, l, mid, id * 2) + query(ql, qr, mid + 1, r, id * 2 + 1);
}
int find(int l, int r, int sum, int id) {
if (l == r) return l;
int mid = (l + r) / 2;
if (seg[id * 2] >= sum) return find(l, mid, sum, id * 2);
else return find(mid + 1, r, sum - seg[id * 2], id * 2 + 1);
}
} ST;
int get_next(int value, int dist) {
return a[po[value] + dist];
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> in[i];
if (i <= n / 2) xi[i] = in[i];
else yi[i - n / 2] = in[i];
}
for (int i = 1; i <= n; i++) {
if (px > n / 2) a[i] = yi[py++];
else if (py > n / 2) a[i] = xi[px++];
else if (xi[px] < yi[py]) a[i] = xi[px++];
else a[i] = yi[py++];
po[a[i]] = i;
}
// for (int i = 1; i <= n; i++) cout << a[i] << ' '; cout << '\n';
stack<int> st;
for (int i = 1; i <= n; i++) nxt[i] = 1e9;
for (int i = n; i >= 1; i--) {
while (!st.empty() && a[st.top()] < a[i]) st.pop();
if (!st.empty()) nxt[i] = st.top() - i;
st.push(i);
}
// for (int i = 1; i <= n; i++) cout << nxt[i] << ' '; cout << '\n';
vector<pair<int, int>> piv;
for (int i = 1; i <= n; i++) {
if (piv.empty() || piv.back().first < a[i]) piv.push_back({a[i], 1});
else piv.back().second++;
}
for (int i = 1; i <= q; i++) {
cin >> qry[i][0] >> qry[i][1];
qry[i][2] = i;
}
sort(qry + 1, qry + q + 1, [] (auto x, auto y) {
return x[0] < y[0];
});
ST.init(n + 1);
for (auto& [pi, pj] : piv) ST.update(pi, pj, 1, n, 1);
int cur = 1;
bool firm = 0;
// for (int i = 1; i <= n; i++) cout << ST.query(i, i, 1, n, 1) << ' ';
// cout << '\n';
for (int i = 1; i <= q; i++) {
int t = qry[i][0], id = qry[i][1];
if (!t) {
ans[qry[i][2]] = in[id];
continue;
}
if (t == 1) {
ans[qry[i][2]] = a[id];
continue;
}
while (cur < t && !firm) {
int rt = ST.find(1, n, n / 2, 1);
int actl = ST.query(1, rt, 1, n, 1);
int pdist = ST.query(1, rt - 1, 1, n, 1);
if (actl == n / 2) {
firm = 1;
break;
}
// cout << "rt: " << rt << '\n';
int ex = actl - n / 2, elapsed = 0;
ST.update(rt, -ex, 1, n, 1);
while (ex > 0) {
int hd = get_next(rt, n / 2 + elapsed - pdist);
// cout << "hd: " << hd << '\n';
int len = min(nxt[po[hd]] - 1, ex - 1);
// cout << "len: " << nxt[po[hd]] - 1 << " " << ex << '\n';
ST.update(hd, len + 1, 1, n, 1);
ex -= len + 1, elapsed += len + 1;
}
cur++;
// for (int j = 1; j <= n; j++) cout << ST.query(j, j, 1, n, 1) << ' ';
// cout << '\n';
}
int cutoff = ST.find(1, n, id, 1); // >=
int actl = ST.query(1, cutoff - 1, 1, n, 1);
int dista = id - actl - 1;
ans[qry[i][2]] = a[po[cutoff] + dista];
}
// for (int i = 1; i <= n; i++) cout << ST.query(i, i, 1, n, 1) << ' ';
// cout << '\n';
for (int i = 1; i <= q; i++) cout << ans[i] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
230 ms |
22856 KB |
Output is correct |
2 |
Correct |
265 ms |
30280 KB |
Output is correct |
3 |
Correct |
241 ms |
30024 KB |
Output is correct |
4 |
Correct |
200 ms |
28796 KB |
Output is correct |
5 |
Correct |
237 ms |
30024 KB |
Output is correct |
6 |
Correct |
239 ms |
29256 KB |
Output is correct |
7 |
Correct |
240 ms |
30024 KB |
Output is correct |
8 |
Correct |
219 ms |
29052 KB |
Output is correct |
9 |
Correct |
227 ms |
29256 KB |
Output is correct |
10 |
Correct |
251 ms |
29320 KB |
Output is correct |
11 |
Correct |
217 ms |
29256 KB |
Output is correct |
12 |
Correct |
191 ms |
28232 KB |
Output is correct |
13 |
Correct |
197 ms |
29000 KB |
Output is correct |
14 |
Correct |
231 ms |
29568 KB |
Output is correct |
15 |
Correct |
231 ms |
29256 KB |
Output is correct |
16 |
Correct |
1 ms |
6480 KB |
Output is correct |
17 |
Correct |
148 ms |
28428 KB |
Output is correct |
18 |
Correct |
179 ms |
28232 KB |
Output is correct |
19 |
Correct |
2 ms |
6480 KB |
Output is correct |
20 |
Correct |
1 ms |
6480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
355 ms |
29004 KB |
Output is correct |
2 |
Correct |
378 ms |
45240 KB |
Output is correct |
3 |
Correct |
276 ms |
39544 KB |
Output is correct |
4 |
Correct |
270 ms |
38320 KB |
Output is correct |
5 |
Correct |
248 ms |
38796 KB |
Output is correct |
6 |
Correct |
221 ms |
37960 KB |
Output is correct |
7 |
Correct |
283 ms |
42316 KB |
Output is correct |
8 |
Correct |
273 ms |
41544 KB |
Output is correct |
9 |
Correct |
236 ms |
38736 KB |
Output is correct |
10 |
Correct |
264 ms |
40520 KB |
Output is correct |
11 |
Correct |
211 ms |
36936 KB |
Output is correct |
12 |
Correct |
215 ms |
37204 KB |
Output is correct |
13 |
Correct |
273 ms |
39632 KB |
Output is correct |
14 |
Correct |
231 ms |
37704 KB |
Output is correct |
15 |
Correct |
274 ms |
40968 KB |
Output is correct |
16 |
Correct |
18 ms |
11344 KB |
Output is correct |
17 |
Correct |
186 ms |
40904 KB |
Output is correct |
18 |
Correct |
135 ms |
35124 KB |
Output is correct |
19 |
Correct |
60 ms |
20464 KB |
Output is correct |
20 |
Correct |
73 ms |
22088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
11080 KB |
Output is correct |
2 |
Correct |
60 ms |
13756 KB |
Output is correct |
3 |
Correct |
73 ms |
13532 KB |
Output is correct |
4 |
Correct |
41 ms |
12636 KB |
Output is correct |
5 |
Correct |
58 ms |
12936 KB |
Output is correct |
6 |
Correct |
42 ms |
12616 KB |
Output is correct |
7 |
Correct |
51 ms |
12872 KB |
Output is correct |
8 |
Correct |
46 ms |
12728 KB |
Output is correct |
9 |
Correct |
55 ms |
12820 KB |
Output is correct |
10 |
Correct |
42 ms |
12596 KB |
Output is correct |
11 |
Correct |
40 ms |
12616 KB |
Output is correct |
12 |
Correct |
37 ms |
12624 KB |
Output is correct |
13 |
Correct |
39 ms |
12664 KB |
Output is correct |
14 |
Correct |
37 ms |
12616 KB |
Output is correct |
15 |
Correct |
38 ms |
12524 KB |
Output is correct |
16 |
Correct |
12 ms |
8784 KB |
Output is correct |
17 |
Correct |
41 ms |
13764 KB |
Output is correct |
18 |
Correct |
25 ms |
12368 KB |
Output is correct |
19 |
Correct |
1 ms |
6480 KB |
Output is correct |
20 |
Correct |
1 ms |
6480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
230 ms |
22856 KB |
Output is correct |
2 |
Correct |
265 ms |
30280 KB |
Output is correct |
3 |
Correct |
241 ms |
30024 KB |
Output is correct |
4 |
Correct |
200 ms |
28796 KB |
Output is correct |
5 |
Correct |
237 ms |
30024 KB |
Output is correct |
6 |
Correct |
239 ms |
29256 KB |
Output is correct |
7 |
Correct |
240 ms |
30024 KB |
Output is correct |
8 |
Correct |
219 ms |
29052 KB |
Output is correct |
9 |
Correct |
227 ms |
29256 KB |
Output is correct |
10 |
Correct |
251 ms |
29320 KB |
Output is correct |
11 |
Correct |
217 ms |
29256 KB |
Output is correct |
12 |
Correct |
191 ms |
28232 KB |
Output is correct |
13 |
Correct |
197 ms |
29000 KB |
Output is correct |
14 |
Correct |
231 ms |
29568 KB |
Output is correct |
15 |
Correct |
231 ms |
29256 KB |
Output is correct |
16 |
Correct |
1 ms |
6480 KB |
Output is correct |
17 |
Correct |
148 ms |
28428 KB |
Output is correct |
18 |
Correct |
179 ms |
28232 KB |
Output is correct |
19 |
Correct |
2 ms |
6480 KB |
Output is correct |
20 |
Correct |
1 ms |
6480 KB |
Output is correct |
21 |
Correct |
355 ms |
29004 KB |
Output is correct |
22 |
Correct |
378 ms |
45240 KB |
Output is correct |
23 |
Correct |
276 ms |
39544 KB |
Output is correct |
24 |
Correct |
270 ms |
38320 KB |
Output is correct |
25 |
Correct |
248 ms |
38796 KB |
Output is correct |
26 |
Correct |
221 ms |
37960 KB |
Output is correct |
27 |
Correct |
283 ms |
42316 KB |
Output is correct |
28 |
Correct |
273 ms |
41544 KB |
Output is correct |
29 |
Correct |
236 ms |
38736 KB |
Output is correct |
30 |
Correct |
264 ms |
40520 KB |
Output is correct |
31 |
Correct |
211 ms |
36936 KB |
Output is correct |
32 |
Correct |
215 ms |
37204 KB |
Output is correct |
33 |
Correct |
273 ms |
39632 KB |
Output is correct |
34 |
Correct |
231 ms |
37704 KB |
Output is correct |
35 |
Correct |
274 ms |
40968 KB |
Output is correct |
36 |
Correct |
18 ms |
11344 KB |
Output is correct |
37 |
Correct |
186 ms |
40904 KB |
Output is correct |
38 |
Correct |
135 ms |
35124 KB |
Output is correct |
39 |
Correct |
60 ms |
20464 KB |
Output is correct |
40 |
Correct |
73 ms |
22088 KB |
Output is correct |
41 |
Correct |
70 ms |
11080 KB |
Output is correct |
42 |
Correct |
60 ms |
13756 KB |
Output is correct |
43 |
Correct |
73 ms |
13532 KB |
Output is correct |
44 |
Correct |
41 ms |
12636 KB |
Output is correct |
45 |
Correct |
58 ms |
12936 KB |
Output is correct |
46 |
Correct |
42 ms |
12616 KB |
Output is correct |
47 |
Correct |
51 ms |
12872 KB |
Output is correct |
48 |
Correct |
46 ms |
12728 KB |
Output is correct |
49 |
Correct |
55 ms |
12820 KB |
Output is correct |
50 |
Correct |
42 ms |
12596 KB |
Output is correct |
51 |
Correct |
40 ms |
12616 KB |
Output is correct |
52 |
Correct |
37 ms |
12624 KB |
Output is correct |
53 |
Correct |
39 ms |
12664 KB |
Output is correct |
54 |
Correct |
37 ms |
12616 KB |
Output is correct |
55 |
Correct |
38 ms |
12524 KB |
Output is correct |
56 |
Correct |
12 ms |
8784 KB |
Output is correct |
57 |
Correct |
41 ms |
13764 KB |
Output is correct |
58 |
Correct |
25 ms |
12368 KB |
Output is correct |
59 |
Correct |
1 ms |
6480 KB |
Output is correct |
60 |
Correct |
1 ms |
6480 KB |
Output is correct |
61 |
Correct |
472 ms |
43052 KB |
Output is correct |
62 |
Correct |
496 ms |
45184 KB |
Output is correct |
63 |
Correct |
497 ms |
42944 KB |
Output is correct |
64 |
Correct |
407 ms |
41288 KB |
Output is correct |
65 |
Correct |
397 ms |
42272 KB |
Output is correct |
66 |
Correct |
357 ms |
41568 KB |
Output is correct |
67 |
Correct |
368 ms |
41880 KB |
Output is correct |
68 |
Correct |
443 ms |
41092 KB |
Output is correct |
69 |
Correct |
393 ms |
41100 KB |
Output is correct |
70 |
Correct |
395 ms |
40600 KB |
Output is correct |
71 |
Correct |
330 ms |
39768 KB |
Output is correct |
72 |
Correct |
361 ms |
40520 KB |
Output is correct |
73 |
Correct |
381 ms |
40008 KB |
Output is correct |
74 |
Correct |
365 ms |
41156 KB |
Output is correct |
75 |
Correct |
356 ms |
40768 KB |
Output is correct |
76 |
Correct |
18 ms |
11344 KB |
Output is correct |
77 |
Correct |
210 ms |
41080 KB |
Output is correct |
78 |
Correct |
207 ms |
37960 KB |
Output is correct |
79 |
Correct |
1 ms |
6480 KB |
Output is correct |
80 |
Correct |
2 ms |
6480 KB |
Output is correct |