#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 _ = 1; _ <= n; ++_) {
for (auto [i, p]: queries[_]) {
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:88:65: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
88 | mx[l][i] = comp(mx[l - 1][i], mx[l - 1][i + (1 << l - 1)]);
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
254 ms |
19492 KB |
Output is correct |
2 |
Correct |
301 ms |
18192 KB |
Output is correct |
3 |
Correct |
284 ms |
17852 KB |
Output is correct |
4 |
Correct |
218 ms |
16700 KB |
Output is correct |
5 |
Correct |
226 ms |
19916 KB |
Output is correct |
6 |
Correct |
244 ms |
20564 KB |
Output is correct |
7 |
Correct |
224 ms |
21316 KB |
Output is correct |
8 |
Correct |
219 ms |
18916 KB |
Output is correct |
9 |
Correct |
226 ms |
17692 KB |
Output is correct |
10 |
Correct |
214 ms |
17840 KB |
Output is correct |
11 |
Correct |
216 ms |
18064 KB |
Output is correct |
12 |
Correct |
205 ms |
15012 KB |
Output is correct |
13 |
Correct |
210 ms |
16832 KB |
Output is correct |
14 |
Correct |
225 ms |
19328 KB |
Output is correct |
15 |
Correct |
220 ms |
16900 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
167 ms |
9212 KB |
Output is correct |
18 |
Correct |
185 ms |
12812 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
322 ms |
41720 KB |
Output is correct |
2 |
Correct |
315 ms |
41640 KB |
Output is correct |
3 |
Correct |
270 ms |
37040 KB |
Output is correct |
4 |
Correct |
275 ms |
37296 KB |
Output is correct |
5 |
Correct |
243 ms |
38016 KB |
Output is correct |
6 |
Correct |
255 ms |
36416 KB |
Output is correct |
7 |
Correct |
301 ms |
41260 KB |
Output is correct |
8 |
Correct |
285 ms |
39500 KB |
Output is correct |
9 |
Correct |
253 ms |
37476 KB |
Output is correct |
10 |
Correct |
291 ms |
39116 KB |
Output is correct |
11 |
Correct |
232 ms |
37168 KB |
Output is correct |
12 |
Correct |
252 ms |
36012 KB |
Output is correct |
13 |
Correct |
281 ms |
39316 KB |
Output is correct |
14 |
Correct |
253 ms |
36808 KB |
Output is correct |
15 |
Correct |
323 ms |
39992 KB |
Output is correct |
16 |
Correct |
34 ms |
21820 KB |
Output is correct |
17 |
Correct |
217 ms |
31956 KB |
Output is correct |
18 |
Correct |
200 ms |
35228 KB |
Output is correct |
19 |
Correct |
70 ms |
23612 KB |
Output is correct |
20 |
Correct |
93 ms |
24412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
14156 KB |
Output is correct |
2 |
Correct |
61 ms |
13696 KB |
Output is correct |
3 |
Correct |
62 ms |
13388 KB |
Output is correct |
4 |
Correct |
54 ms |
13168 KB |
Output is correct |
5 |
Correct |
57 ms |
13900 KB |
Output is correct |
6 |
Correct |
47 ms |
13004 KB |
Output is correct |
7 |
Correct |
51 ms |
13676 KB |
Output is correct |
8 |
Correct |
49 ms |
12976 KB |
Output is correct |
9 |
Correct |
61 ms |
13576 KB |
Output is correct |
10 |
Correct |
42 ms |
12848 KB |
Output is correct |
11 |
Correct |
45 ms |
13312 KB |
Output is correct |
12 |
Correct |
43 ms |
12896 KB |
Output is correct |
13 |
Correct |
46 ms |
12748 KB |
Output is correct |
14 |
Correct |
50 ms |
13412 KB |
Output is correct |
15 |
Correct |
41 ms |
13112 KB |
Output is correct |
16 |
Correct |
15 ms |
10836 KB |
Output is correct |
17 |
Correct |
37 ms |
11692 KB |
Output is correct |
18 |
Correct |
38 ms |
12180 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 |
254 ms |
19492 KB |
Output is correct |
2 |
Correct |
301 ms |
18192 KB |
Output is correct |
3 |
Correct |
284 ms |
17852 KB |
Output is correct |
4 |
Correct |
218 ms |
16700 KB |
Output is correct |
5 |
Correct |
226 ms |
19916 KB |
Output is correct |
6 |
Correct |
244 ms |
20564 KB |
Output is correct |
7 |
Correct |
224 ms |
21316 KB |
Output is correct |
8 |
Correct |
219 ms |
18916 KB |
Output is correct |
9 |
Correct |
226 ms |
17692 KB |
Output is correct |
10 |
Correct |
214 ms |
17840 KB |
Output is correct |
11 |
Correct |
216 ms |
18064 KB |
Output is correct |
12 |
Correct |
205 ms |
15012 KB |
Output is correct |
13 |
Correct |
210 ms |
16832 KB |
Output is correct |
14 |
Correct |
225 ms |
19328 KB |
Output is correct |
15 |
Correct |
220 ms |
16900 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
167 ms |
9212 KB |
Output is correct |
18 |
Correct |
185 ms |
12812 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
21 |
Correct |
322 ms |
41720 KB |
Output is correct |
22 |
Correct |
315 ms |
41640 KB |
Output is correct |
23 |
Correct |
270 ms |
37040 KB |
Output is correct |
24 |
Correct |
275 ms |
37296 KB |
Output is correct |
25 |
Correct |
243 ms |
38016 KB |
Output is correct |
26 |
Correct |
255 ms |
36416 KB |
Output is correct |
27 |
Correct |
301 ms |
41260 KB |
Output is correct |
28 |
Correct |
285 ms |
39500 KB |
Output is correct |
29 |
Correct |
253 ms |
37476 KB |
Output is correct |
30 |
Correct |
291 ms |
39116 KB |
Output is correct |
31 |
Correct |
232 ms |
37168 KB |
Output is correct |
32 |
Correct |
252 ms |
36012 KB |
Output is correct |
33 |
Correct |
281 ms |
39316 KB |
Output is correct |
34 |
Correct |
253 ms |
36808 KB |
Output is correct |
35 |
Correct |
323 ms |
39992 KB |
Output is correct |
36 |
Correct |
34 ms |
21820 KB |
Output is correct |
37 |
Correct |
217 ms |
31956 KB |
Output is correct |
38 |
Correct |
200 ms |
35228 KB |
Output is correct |
39 |
Correct |
70 ms |
23612 KB |
Output is correct |
40 |
Correct |
93 ms |
24412 KB |
Output is correct |
41 |
Correct |
67 ms |
14156 KB |
Output is correct |
42 |
Correct |
61 ms |
13696 KB |
Output is correct |
43 |
Correct |
62 ms |
13388 KB |
Output is correct |
44 |
Correct |
54 ms |
13168 KB |
Output is correct |
45 |
Correct |
57 ms |
13900 KB |
Output is correct |
46 |
Correct |
47 ms |
13004 KB |
Output is correct |
47 |
Correct |
51 ms |
13676 KB |
Output is correct |
48 |
Correct |
49 ms |
12976 KB |
Output is correct |
49 |
Correct |
61 ms |
13576 KB |
Output is correct |
50 |
Correct |
42 ms |
12848 KB |
Output is correct |
51 |
Correct |
45 ms |
13312 KB |
Output is correct |
52 |
Correct |
43 ms |
12896 KB |
Output is correct |
53 |
Correct |
46 ms |
12748 KB |
Output is correct |
54 |
Correct |
50 ms |
13412 KB |
Output is correct |
55 |
Correct |
41 ms |
13112 KB |
Output is correct |
56 |
Correct |
15 ms |
10836 KB |
Output is correct |
57 |
Correct |
37 ms |
11692 KB |
Output is correct |
58 |
Correct |
38 ms |
12180 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
1 ms |
212 KB |
Output is correct |
61 |
Correct |
616 ms |
47952 KB |
Output is correct |
62 |
Correct |
527 ms |
46404 KB |
Output is correct |
63 |
Correct |
565 ms |
45160 KB |
Output is correct |
64 |
Correct |
403 ms |
44828 KB |
Output is correct |
65 |
Correct |
444 ms |
46536 KB |
Output is correct |
66 |
Correct |
478 ms |
44536 KB |
Output is correct |
67 |
Correct |
348 ms |
44348 KB |
Output is correct |
68 |
Correct |
366 ms |
42400 KB |
Output is correct |
69 |
Correct |
400 ms |
45168 KB |
Output is correct |
70 |
Correct |
346 ms |
41248 KB |
Output is correct |
71 |
Correct |
300 ms |
44408 KB |
Output is correct |
72 |
Correct |
325 ms |
41532 KB |
Output is correct |
73 |
Correct |
328 ms |
42456 KB |
Output is correct |
74 |
Correct |
353 ms |
44304 KB |
Output is correct |
75 |
Correct |
327 ms |
42908 KB |
Output is correct |
76 |
Correct |
33 ms |
21324 KB |
Output is correct |
77 |
Correct |
227 ms |
31080 KB |
Output is correct |
78 |
Correct |
247 ms |
34888 KB |
Output is correct |
79 |
Correct |
0 ms |
212 KB |
Output is correct |
80 |
Correct |
0 ms |
212 KB |
Output is correct |