#include <bits/stdc++.h>
using namespace std;
struct Fenwick {
vector<int> t;
int n, S = 0;
Fenwick() = default;
Fenwick(int n) : n(n), t(n + 1) {}
void modify(int i, int v) {
S += v;
for (int x = i + 1; x <= n; x += x & -x) {
t[x] += v;
}
}
int sum(int i) {
int ans = 0;
for (int x = i + 1; x > 0; x -= x & -x) {
ans += t[x];
}
return ans;
}
int lower_bound(int k) {
int x = 0;
for (int i = 1 << __lg(n); i > 0; i >>= 1) {
if (x + i <= n && t[x + i] < k) {
x += i;
k -= t[x];
}
}
return x;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int &x: a) {
cin >> x;
x -= 1;
}
vector<int> ans(q, -1);
vector<vector<array<int, 2>>> queries(n + 1);
for (int i = 0; i < q; ++i) {
int t, p;
cin >> t >> p;
p -= 1;
if (t == 0) {
ans[i] = a[p];
continue;
}
queries[min(t, n)].push_back({i, p});
}
const int logn = __lg(n) + 1;
auto comp = [&](int i, int j) {
return a[i] > a[j] ? i : j;
};
vector<vector<int>> mx(logn);
mx[0].resize(n);
iota(mx[0].begin(), mx[0].end(), 0);
for (int l = 1; l < logn; ++l) {
mx[l].resize(n - (1 << l) + 1);
for (int i = 0; i + (1 << l) <= n; ++i) {
mx[l][i] = comp(mx[l - 1][i], mx[l - 1][i + (1 << l - 1)]);
}
}
auto rangeMax = [&](int l, int r) {
int lg = __lg(r - l);
return comp(mx[lg][l], mx[lg][r - (1 << lg)]);
};
Fenwick fn(n);
vector<pair<int, int>> segment(n, {-1, -1});
int pref_mx = -1;
for (int i = 0; i < n / 2; ++i) {
if (pref_mx < a[i]) {
if (pref_mx != -1) {
segment[pref_mx].second = i;
}
pref_mx = a[i];
segment[pref_mx].first = i;
}
}
segment[pref_mx].second = n / 2;
pref_mx = -1;
for (int i = n / 2; i < n; ++i) {
if (pref_mx < a[i]) {
if (pref_mx != -1) {
segment[pref_mx].second = i;
}
pref_mx = a[i];
segment[pref_mx].first = i;
}
}
segment[pref_mx].second = n;
for (int x = 0; x < n; ++x) {
if (segment[x].first != -1) {
fn.modify(x, segment[x].second - segment[x].first);
}
}
auto find_next = [&](int l, int r) {
int mx = rangeMax(l, r);
if (l == mx) {
return -1;
}
int lo = l, hi = r;
while (lo + 1 < hi) {
int mid = (lo + hi) >> 1;
if (rangeMax(l, mid + 1) == l) {
lo = mid;
} else {
hi = mid;
}
}
return hi;
};
auto getValue = [&](int p) {
p += 1;
int x = fn.lower_bound(p);
int sum_l = fn.sum(x - 1);
return a[segment[x].first + (p - sum_l) - 1];
};
for (int t = 1; t <= n; ++t) {
for (auto [i, p]: queries[t]) {
ans[i] = getValue(p);
}
int x = fn.lower_bound(n / 2);
int sum = fn.sum(x);
if (sum == n / 2) {
continue;
}
int sum_l = sum - (segment[x].second - segment[x].first);
int cut = segment[x].first + (n / 2 - sum_l);
fn.modify(x, cut - segment[x].second);
int l = cut, r = segment[x].second;
segment[x].second = cut;
while (l < r) {
int mid = find_next(l, r);
if (mid == -1) {
segment[a[l]].first = l, segment[a[l]].second = r;
} else {
segment[a[l]].first = l, segment[a[l]].second = mid;
}
fn.modify(a[l], segment[a[l]].second - segment[a[l]].first);
l = segment[a[l]].second;
}
}
for (int i = 0; i < q; ++i) {
cout << ans[i] + 1 << '\n';
}
return 0;
}
Compilation message
Main.cpp: In constructor 'Fenwick::Fenwick(int)':
Main.cpp:7:9: warning: 'Fenwick::n' will be initialized after [-Wreorder]
7 | int n, S = 0;
| ^
Main.cpp:6:17: warning: 'std::vector<int> Fenwick::t' [-Wreorder]
6 | vector<int> t;
| ^
Main.cpp:11:5: warning: when initialized here [-Wreorder]
11 | Fenwick(int n) : n(n), t(n + 1) {}
| ^~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:87:65: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
87 | mx[l][i] = comp(mx[l - 1][i], mx[l - 1][i + (1 << l - 1)]);
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
271 ms |
18904 KB |
Output is correct |
2 |
Correct |
309 ms |
17580 KB |
Output is correct |
3 |
Correct |
265 ms |
17096 KB |
Output is correct |
4 |
Correct |
212 ms |
16008 KB |
Output is correct |
5 |
Correct |
339 ms |
19296 KB |
Output is correct |
6 |
Correct |
273 ms |
19980 KB |
Output is correct |
7 |
Correct |
242 ms |
20440 KB |
Output is correct |
8 |
Correct |
227 ms |
18280 KB |
Output is correct |
9 |
Correct |
223 ms |
17140 KB |
Output is correct |
10 |
Correct |
290 ms |
17008 KB |
Output is correct |
11 |
Correct |
216 ms |
17380 KB |
Output is correct |
12 |
Correct |
198 ms |
14400 KB |
Output is correct |
13 |
Correct |
220 ms |
16176 KB |
Output is correct |
14 |
Correct |
231 ms |
18752 KB |
Output is correct |
15 |
Correct |
232 ms |
16312 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
182 ms |
8552 KB |
Output is correct |
18 |
Correct |
191 ms |
12216 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
345 ms |
40128 KB |
Output is correct |
2 |
Correct |
335 ms |
40128 KB |
Output is correct |
3 |
Correct |
274 ms |
35600 KB |
Output is correct |
4 |
Correct |
273 ms |
35696 KB |
Output is correct |
5 |
Correct |
256 ms |
36440 KB |
Output is correct |
6 |
Correct |
298 ms |
35028 KB |
Output is correct |
7 |
Correct |
329 ms |
39908 KB |
Output is correct |
8 |
Correct |
304 ms |
38396 KB |
Output is correct |
9 |
Correct |
302 ms |
36260 KB |
Output is correct |
10 |
Correct |
299 ms |
38192 KB |
Output is correct |
11 |
Correct |
237 ms |
35960 KB |
Output is correct |
12 |
Correct |
280 ms |
35056 KB |
Output is correct |
13 |
Correct |
297 ms |
38380 KB |
Output is correct |
14 |
Correct |
252 ms |
36156 KB |
Output is correct |
15 |
Correct |
345 ms |
39216 KB |
Output is correct |
16 |
Correct |
41 ms |
21512 KB |
Output is correct |
17 |
Correct |
268 ms |
31108 KB |
Output is correct |
18 |
Correct |
237 ms |
34984 KB |
Output is correct |
19 |
Correct |
85 ms |
23360 KB |
Output is correct |
20 |
Correct |
113 ms |
24084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
13928 KB |
Output is correct |
2 |
Correct |
93 ms |
13440 KB |
Output is correct |
3 |
Correct |
73 ms |
13216 KB |
Output is correct |
4 |
Correct |
49 ms |
12956 KB |
Output is correct |
5 |
Correct |
55 ms |
13580 KB |
Output is correct |
6 |
Correct |
52 ms |
12816 KB |
Output is correct |
7 |
Correct |
65 ms |
13380 KB |
Output is correct |
8 |
Correct |
53 ms |
12712 KB |
Output is correct |
9 |
Correct |
57 ms |
13368 KB |
Output is correct |
10 |
Correct |
51 ms |
12576 KB |
Output is correct |
11 |
Correct |
52 ms |
13152 KB |
Output is correct |
12 |
Correct |
47 ms |
12624 KB |
Output is correct |
13 |
Correct |
44 ms |
12560 KB |
Output is correct |
14 |
Correct |
45 ms |
13060 KB |
Output is correct |
15 |
Correct |
42 ms |
12796 KB |
Output is correct |
16 |
Correct |
17 ms |
10460 KB |
Output is correct |
17 |
Correct |
40 ms |
11416 KB |
Output is correct |
18 |
Correct |
37 ms |
11884 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
271 ms |
18904 KB |
Output is correct |
2 |
Correct |
309 ms |
17580 KB |
Output is correct |
3 |
Correct |
265 ms |
17096 KB |
Output is correct |
4 |
Correct |
212 ms |
16008 KB |
Output is correct |
5 |
Correct |
339 ms |
19296 KB |
Output is correct |
6 |
Correct |
273 ms |
19980 KB |
Output is correct |
7 |
Correct |
242 ms |
20440 KB |
Output is correct |
8 |
Correct |
227 ms |
18280 KB |
Output is correct |
9 |
Correct |
223 ms |
17140 KB |
Output is correct |
10 |
Correct |
290 ms |
17008 KB |
Output is correct |
11 |
Correct |
216 ms |
17380 KB |
Output is correct |
12 |
Correct |
198 ms |
14400 KB |
Output is correct |
13 |
Correct |
220 ms |
16176 KB |
Output is correct |
14 |
Correct |
231 ms |
18752 KB |
Output is correct |
15 |
Correct |
232 ms |
16312 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
182 ms |
8552 KB |
Output is correct |
18 |
Correct |
191 ms |
12216 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
345 ms |
40128 KB |
Output is correct |
22 |
Correct |
335 ms |
40128 KB |
Output is correct |
23 |
Correct |
274 ms |
35600 KB |
Output is correct |
24 |
Correct |
273 ms |
35696 KB |
Output is correct |
25 |
Correct |
256 ms |
36440 KB |
Output is correct |
26 |
Correct |
298 ms |
35028 KB |
Output is correct |
27 |
Correct |
329 ms |
39908 KB |
Output is correct |
28 |
Correct |
304 ms |
38396 KB |
Output is correct |
29 |
Correct |
302 ms |
36260 KB |
Output is correct |
30 |
Correct |
299 ms |
38192 KB |
Output is correct |
31 |
Correct |
237 ms |
35960 KB |
Output is correct |
32 |
Correct |
280 ms |
35056 KB |
Output is correct |
33 |
Correct |
297 ms |
38380 KB |
Output is correct |
34 |
Correct |
252 ms |
36156 KB |
Output is correct |
35 |
Correct |
345 ms |
39216 KB |
Output is correct |
36 |
Correct |
41 ms |
21512 KB |
Output is correct |
37 |
Correct |
268 ms |
31108 KB |
Output is correct |
38 |
Correct |
237 ms |
34984 KB |
Output is correct |
39 |
Correct |
85 ms |
23360 KB |
Output is correct |
40 |
Correct |
113 ms |
24084 KB |
Output is correct |
41 |
Correct |
83 ms |
13928 KB |
Output is correct |
42 |
Correct |
93 ms |
13440 KB |
Output is correct |
43 |
Correct |
73 ms |
13216 KB |
Output is correct |
44 |
Correct |
49 ms |
12956 KB |
Output is correct |
45 |
Correct |
55 ms |
13580 KB |
Output is correct |
46 |
Correct |
52 ms |
12816 KB |
Output is correct |
47 |
Correct |
65 ms |
13380 KB |
Output is correct |
48 |
Correct |
53 ms |
12712 KB |
Output is correct |
49 |
Correct |
57 ms |
13368 KB |
Output is correct |
50 |
Correct |
51 ms |
12576 KB |
Output is correct |
51 |
Correct |
52 ms |
13152 KB |
Output is correct |
52 |
Correct |
47 ms |
12624 KB |
Output is correct |
53 |
Correct |
44 ms |
12560 KB |
Output is correct |
54 |
Correct |
45 ms |
13060 KB |
Output is correct |
55 |
Correct |
42 ms |
12796 KB |
Output is correct |
56 |
Correct |
17 ms |
10460 KB |
Output is correct |
57 |
Correct |
40 ms |
11416 KB |
Output is correct |
58 |
Correct |
37 ms |
11884 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
1 ms |
212 KB |
Output is correct |
61 |
Correct |
621 ms |
47360 KB |
Output is correct |
62 |
Correct |
532 ms |
45916 KB |
Output is correct |
63 |
Correct |
524 ms |
44636 KB |
Output is correct |
64 |
Correct |
417 ms |
44492 KB |
Output is correct |
65 |
Correct |
435 ms |
46152 KB |
Output is correct |
66 |
Correct |
412 ms |
44188 KB |
Output is correct |
67 |
Correct |
391 ms |
44324 KB |
Output is correct |
68 |
Correct |
370 ms |
42344 KB |
Output is correct |
69 |
Correct |
387 ms |
45108 KB |
Output is correct |
70 |
Correct |
346 ms |
41180 KB |
Output is correct |
71 |
Correct |
316 ms |
44488 KB |
Output is correct |
72 |
Correct |
383 ms |
41764 KB |
Output is correct |
73 |
Correct |
378 ms |
42632 KB |
Output is correct |
74 |
Correct |
341 ms |
44496 KB |
Output is correct |
75 |
Correct |
345 ms |
43124 KB |
Output is correct |
76 |
Correct |
33 ms |
21452 KB |
Output is correct |
77 |
Correct |
250 ms |
31380 KB |
Output is correct |
78 |
Correct |
252 ms |
35156 KB |
Output is correct |
79 |
Correct |
1 ms |
212 KB |
Output is correct |
80 |
Correct |
0 ms |
212 KB |
Output is correct |