#include<bits/stdc++.h>
using namespace std;
mt19937 rnd(51);
#define ll long long
#define pb push_back
#define ld long double
const int N = 2e5 + 10;
int pos[N];
struct SegTree {
vector<int> t;
SegTree(int n) {
t.resize(4 * n);
}
void update(int v, int l, int r, int pos, int val) {
if (l == r) {
t[v] += val;
return;
}
int m = (l + r) / 2;
if (pos <= m) {
update(2 * v, l, m, pos, val);
} else {
update(2 * v + 1, m + 1, r, pos, val);
}
t[v] = t[v * 2] + t[v * 2 + 1];
}
int get_ind(int v, int l, int r, int val) {
if (l == r) {
return l;
}
int m = (l + r) / 2;
if (t[v * 2] >= val) {
return get_ind(2 * v, l, m, val);
} else {
return get_ind(2 * v + 1, m + 1, r, val - t[v * 2]);
}
}
int get_sum(int v, int tl, int tr, int l, int r) {
if (l > r) return 0;
if (tl == l && tr == r) {
return t[v];
}
int tm = (tl + tr) / 2;
return get_sum(2 * v, tl, tm, l, min(r, tm)) + get_sum(2 * v + 1, tm + 1, tr, max(l, tm + 1), r);
}
} t(N);
set<array<int, 3>> st;
void add_seg(int val, int l, int r) {
st.insert({val, l, r});
t.update(1, 0, N - 1, val, (r - l + 1));
}
void del_seg(int val, int l, int r) {
st.erase({val, l, r});
t.update(1, 0, N - 1, val, -(r - l + 1));
}
int get_ind(int need) {
return pos[t.get_ind(1, 0, N - 1, need)];
}
signed main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif // LOCAL
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<int> a(n), ans(q, -1), go(n, n);
vector<vector<pair<int,int>>> need(n);
int last = 0;
deque<int> qu;
for (int i = 0; i < n; i++) {
cin >> a[i];
pos[a[i]] = i;
if (a[i] > a[last]) {
add_seg(a[last], last, i - 1);
last = i;
}
}
for (int i = 0; i < n; i++) {
while (qu.size() > 0 && a[i] > a[qu.back()]) {
go[qu.back()] = i;
qu.pop_back();
}
qu.pb(i);
}
add_seg(a[last], last, n - 1);
for (int i = 0; i < q; i++) {
int t, ind;
cin >> t >> ind;
if (t == 0) {
ans[i] = a[ind - 1];
} else {
need[min(n - 1, t - 1)].pb({ind, i});
}
}
for (int i = 0; i < n; i++) {
int pos = get_ind(n / 2 + 1);
if (t.get_sum(1, 0, N - 1, 0, a[pos] - 1) < n / 2) {
auto seg = *st.lower_bound({a[pos], 0, 0});
int ind = seg[2] - (t.get_sum(1, 0, N - 1, 0, a[pos]) - n / 2);
del_seg(seg[0], seg[1], seg[2]);
add_seg(seg[0], seg[1], ind);
int last = ind + 1;
while (go[last] <= seg[2]) {
add_seg(a[last], last, go[last] - 1);
last = go[last];
}
add_seg(a[last], last, seg[2]);
}
for (auto to : need[i]) {
int pos = get_ind(to.first);
auto seg = *st.lower_bound({a[pos], 0, 0});
ans[to.second] = a[seg[2] - (t.get_sum(1, 0, N - 1, 0, a[pos]) - to.first)];
}
}
for (int i = 0; i < q; i++) {
cout << ans[i] << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
364 ms |
22200 KB |
Output is correct |
2 |
Correct |
406 ms |
22988 KB |
Output is correct |
3 |
Correct |
373 ms |
23428 KB |
Output is correct |
4 |
Correct |
260 ms |
24720 KB |
Output is correct |
5 |
Correct |
293 ms |
27892 KB |
Output is correct |
6 |
Correct |
261 ms |
27852 KB |
Output is correct |
7 |
Correct |
311 ms |
27404 KB |
Output is correct |
8 |
Correct |
286 ms |
25508 KB |
Output is correct |
9 |
Correct |
268 ms |
24904 KB |
Output is correct |
10 |
Correct |
282 ms |
24056 KB |
Output is correct |
11 |
Correct |
271 ms |
24424 KB |
Output is correct |
12 |
Correct |
245 ms |
20752 KB |
Output is correct |
13 |
Correct |
257 ms |
22720 KB |
Output is correct |
14 |
Correct |
282 ms |
28064 KB |
Output is correct |
15 |
Correct |
281 ms |
25104 KB |
Output is correct |
16 |
Correct |
2 ms |
3412 KB |
Output is correct |
17 |
Correct |
179 ms |
16688 KB |
Output is correct |
18 |
Correct |
208 ms |
20176 KB |
Output is correct |
19 |
Correct |
2 ms |
3412 KB |
Output is correct |
20 |
Correct |
2 ms |
3412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
713 ms |
50992 KB |
Output is correct |
2 |
Correct |
581 ms |
50856 KB |
Output is correct |
3 |
Correct |
521 ms |
46816 KB |
Output is correct |
4 |
Correct |
365 ms |
35424 KB |
Output is correct |
5 |
Correct |
378 ms |
35504 KB |
Output is correct |
6 |
Correct |
351 ms |
37148 KB |
Output is correct |
7 |
Correct |
420 ms |
37144 KB |
Output is correct |
8 |
Correct |
405 ms |
39856 KB |
Output is correct |
9 |
Correct |
367 ms |
34664 KB |
Output is correct |
10 |
Correct |
363 ms |
36236 KB |
Output is correct |
11 |
Correct |
301 ms |
30424 KB |
Output is correct |
12 |
Correct |
306 ms |
29356 KB |
Output is correct |
13 |
Correct |
349 ms |
36104 KB |
Output is correct |
14 |
Correct |
317 ms |
34860 KB |
Output is correct |
15 |
Correct |
374 ms |
38268 KB |
Output is correct |
16 |
Correct |
45 ms |
11560 KB |
Output is correct |
17 |
Correct |
278 ms |
38124 KB |
Output is correct |
18 |
Correct |
261 ms |
29468 KB |
Output is correct |
19 |
Correct |
84 ms |
14260 KB |
Output is correct |
20 |
Correct |
113 ms |
16516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
214 ms |
18252 KB |
Output is correct |
2 |
Correct |
166 ms |
16716 KB |
Output is correct |
3 |
Correct |
178 ms |
17156 KB |
Output is correct |
4 |
Correct |
69 ms |
11088 KB |
Output is correct |
5 |
Correct |
114 ms |
13004 KB |
Output is correct |
6 |
Correct |
73 ms |
11472 KB |
Output is correct |
7 |
Correct |
97 ms |
11896 KB |
Output is correct |
8 |
Correct |
86 ms |
11596 KB |
Output is correct |
9 |
Correct |
103 ms |
12988 KB |
Output is correct |
10 |
Correct |
58 ms |
10380 KB |
Output is correct |
11 |
Correct |
65 ms |
9916 KB |
Output is correct |
12 |
Correct |
57 ms |
10260 KB |
Output is correct |
13 |
Correct |
63 ms |
10312 KB |
Output is correct |
14 |
Correct |
65 ms |
10660 KB |
Output is correct |
15 |
Correct |
58 ms |
9904 KB |
Output is correct |
16 |
Correct |
24 ms |
7228 KB |
Output is correct |
17 |
Correct |
69 ms |
14724 KB |
Output is correct |
18 |
Correct |
43 ms |
9420 KB |
Output is correct |
19 |
Correct |
2 ms |
3412 KB |
Output is correct |
20 |
Correct |
2 ms |
3412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
364 ms |
22200 KB |
Output is correct |
2 |
Correct |
406 ms |
22988 KB |
Output is correct |
3 |
Correct |
373 ms |
23428 KB |
Output is correct |
4 |
Correct |
260 ms |
24720 KB |
Output is correct |
5 |
Correct |
293 ms |
27892 KB |
Output is correct |
6 |
Correct |
261 ms |
27852 KB |
Output is correct |
7 |
Correct |
311 ms |
27404 KB |
Output is correct |
8 |
Correct |
286 ms |
25508 KB |
Output is correct |
9 |
Correct |
268 ms |
24904 KB |
Output is correct |
10 |
Correct |
282 ms |
24056 KB |
Output is correct |
11 |
Correct |
271 ms |
24424 KB |
Output is correct |
12 |
Correct |
245 ms |
20752 KB |
Output is correct |
13 |
Correct |
257 ms |
22720 KB |
Output is correct |
14 |
Correct |
282 ms |
28064 KB |
Output is correct |
15 |
Correct |
281 ms |
25104 KB |
Output is correct |
16 |
Correct |
2 ms |
3412 KB |
Output is correct |
17 |
Correct |
179 ms |
16688 KB |
Output is correct |
18 |
Correct |
208 ms |
20176 KB |
Output is correct |
19 |
Correct |
2 ms |
3412 KB |
Output is correct |
20 |
Correct |
2 ms |
3412 KB |
Output is correct |
21 |
Correct |
713 ms |
50992 KB |
Output is correct |
22 |
Correct |
581 ms |
50856 KB |
Output is correct |
23 |
Correct |
521 ms |
46816 KB |
Output is correct |
24 |
Correct |
365 ms |
35424 KB |
Output is correct |
25 |
Correct |
378 ms |
35504 KB |
Output is correct |
26 |
Correct |
351 ms |
37148 KB |
Output is correct |
27 |
Correct |
420 ms |
37144 KB |
Output is correct |
28 |
Correct |
405 ms |
39856 KB |
Output is correct |
29 |
Correct |
367 ms |
34664 KB |
Output is correct |
30 |
Correct |
363 ms |
36236 KB |
Output is correct |
31 |
Correct |
301 ms |
30424 KB |
Output is correct |
32 |
Correct |
306 ms |
29356 KB |
Output is correct |
33 |
Correct |
349 ms |
36104 KB |
Output is correct |
34 |
Correct |
317 ms |
34860 KB |
Output is correct |
35 |
Correct |
374 ms |
38268 KB |
Output is correct |
36 |
Correct |
45 ms |
11560 KB |
Output is correct |
37 |
Correct |
278 ms |
38124 KB |
Output is correct |
38 |
Correct |
261 ms |
29468 KB |
Output is correct |
39 |
Correct |
84 ms |
14260 KB |
Output is correct |
40 |
Correct |
113 ms |
16516 KB |
Output is correct |
41 |
Correct |
214 ms |
18252 KB |
Output is correct |
42 |
Correct |
166 ms |
16716 KB |
Output is correct |
43 |
Correct |
178 ms |
17156 KB |
Output is correct |
44 |
Correct |
69 ms |
11088 KB |
Output is correct |
45 |
Correct |
114 ms |
13004 KB |
Output is correct |
46 |
Correct |
73 ms |
11472 KB |
Output is correct |
47 |
Correct |
97 ms |
11896 KB |
Output is correct |
48 |
Correct |
86 ms |
11596 KB |
Output is correct |
49 |
Correct |
103 ms |
12988 KB |
Output is correct |
50 |
Correct |
58 ms |
10380 KB |
Output is correct |
51 |
Correct |
65 ms |
9916 KB |
Output is correct |
52 |
Correct |
57 ms |
10260 KB |
Output is correct |
53 |
Correct |
63 ms |
10312 KB |
Output is correct |
54 |
Correct |
65 ms |
10660 KB |
Output is correct |
55 |
Correct |
58 ms |
9904 KB |
Output is correct |
56 |
Correct |
24 ms |
7228 KB |
Output is correct |
57 |
Correct |
69 ms |
14724 KB |
Output is correct |
58 |
Correct |
43 ms |
9420 KB |
Output is correct |
59 |
Correct |
2 ms |
3412 KB |
Output is correct |
60 |
Correct |
2 ms |
3412 KB |
Output is correct |
61 |
Correct |
1275 ms |
57360 KB |
Output is correct |
62 |
Correct |
1383 ms |
55456 KB |
Output is correct |
63 |
Correct |
1278 ms |
52092 KB |
Output is correct |
64 |
Correct |
628 ms |
42316 KB |
Output is correct |
65 |
Correct |
649 ms |
46016 KB |
Output is correct |
66 |
Correct |
624 ms |
43136 KB |
Output is correct |
67 |
Correct |
535 ms |
41420 KB |
Output is correct |
68 |
Correct |
572 ms |
41804 KB |
Output is correct |
69 |
Correct |
646 ms |
42724 KB |
Output is correct |
70 |
Correct |
509 ms |
39092 KB |
Output is correct |
71 |
Correct |
435 ms |
40952 KB |
Output is correct |
72 |
Correct |
485 ms |
37020 KB |
Output is correct |
73 |
Correct |
450 ms |
38540 KB |
Output is correct |
74 |
Correct |
477 ms |
40912 KB |
Output is correct |
75 |
Correct |
471 ms |
37972 KB |
Output is correct |
76 |
Correct |
45 ms |
10600 KB |
Output is correct |
77 |
Correct |
303 ms |
36772 KB |
Output is correct |
78 |
Correct |
315 ms |
31344 KB |
Output is correct |
79 |
Correct |
2 ms |
3412 KB |
Output is correct |
80 |
Correct |
2 ms |
3412 KB |
Output is correct |