#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
using namespace std;
typedef long long lint;
typedef pair<int, int> pi;
using VI = vector<int>;
const int NN = 2e5 + 4;
struct BIT {
int C[NN];
void add(int x, int v) {
while(x < NN) C[x] += v, x += x & -x;
// for (int i = x; i < NN; i += i & -i) C[i] += v;
}
int sum(int x) {
int ret = 0;
while (x) ret += C[x], x -= x & -x;
return ret;
}
int kth(int x) {
int pos = 0;
for (int i = 17; i >= 0; i--)
if (pos + (1 << i) < NN && C[pos + (1 << i)] < x)
pos += (1 << i), x -= C[pos];
return pos + 1;
}
};
BIT S;
int main() {
int n, q;
cin >> n >> q;
vector<int> a(n);
vector<int> rev(n + 1);
vector<vector<pi>> v(n + 1);
for (int i = 0; i < n; i++) cin >> a[i], rev[a[i]] = i;
vector<int> ans(q);
for (int i = 0; i < q; i++) {
int t, x;
cin >> t >> x;
t = min(t, n);
if (t == 0)
ans[i] = a[x - 1];
else
v[t].emplace_back(x - 1, i);
}
vector<int> nxt(n);
vector<int> ds(n + 1);
auto SetVal = [&](int x, int v) { S.add(x, v - ds[x]), ds[x] = v; };
for (int i = 0; i < n;) {
int j = i + 1;
while (j < n && a[j] < a[i]) j++;
SetVal(a[i], j - i);
i = j;
}
{
vector<int> v;
for (int i = n - 1; i >= 0; i--) {
while (sz(v) && a[v.back()] < a[i]) v.pop_back();
if (sz(v))
nxt[i] = v.back();
else
nxt[i] = n;
v.push_back(i);
}
}
for (int i = 1; i <= n; i++) {
int itf = S.kth(n / 2 + 1);
int its = ds[itf];
int sum = S.sum(itf - 1);
if (sum < n / 2) {
int l = rev[itf];
int r = l + its;
SetVal(a[l], n / 2 - sum);
l += n / 2 - sum;
for (int i = l; i < r; i = nxt[i]) SetVal(a[i], min(r, nxt[i]) - i);
}
for (auto [pos, idx] : v[i]) {
int qos = S.kth(pos + 1);
int sum = S.sum(qos - 1);
ans[idx] = a[rev[qos] + pos - sum];
}
}
for (auto &x : ans) cout << x << "\n";
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:93:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
93 | for (auto [pos, idx] : v[i]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
514 ms |
18452 KB |
Output is correct |
2 |
Correct |
531 ms |
17036 KB |
Output is correct |
3 |
Correct |
578 ms |
16640 KB |
Output is correct |
4 |
Correct |
452 ms |
15580 KB |
Output is correct |
5 |
Correct |
490 ms |
18824 KB |
Output is correct |
6 |
Correct |
455 ms |
19444 KB |
Output is correct |
7 |
Correct |
480 ms |
19924 KB |
Output is correct |
8 |
Correct |
434 ms |
17864 KB |
Output is correct |
9 |
Correct |
444 ms |
17228 KB |
Output is correct |
10 |
Correct |
462 ms |
16260 KB |
Output is correct |
11 |
Correct |
429 ms |
16740 KB |
Output is correct |
12 |
Correct |
438 ms |
13888 KB |
Output is correct |
13 |
Correct |
470 ms |
15884 KB |
Output is correct |
14 |
Correct |
512 ms |
18424 KB |
Output is correct |
15 |
Correct |
457 ms |
15756 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
393 ms |
8024 KB |
Output is correct |
18 |
Correct |
402 ms |
11712 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
746 ms |
27240 KB |
Output is correct |
2 |
Correct |
738 ms |
28708 KB |
Output is correct |
3 |
Correct |
554 ms |
23768 KB |
Output is correct |
4 |
Correct |
560 ms |
22936 KB |
Output is correct |
5 |
Correct |
562 ms |
23340 KB |
Output is correct |
6 |
Correct |
535 ms |
22504 KB |
Output is correct |
7 |
Correct |
667 ms |
27048 KB |
Output is correct |
8 |
Correct |
646 ms |
25888 KB |
Output is correct |
9 |
Correct |
561 ms |
23388 KB |
Output is correct |
10 |
Correct |
622 ms |
25684 KB |
Output is correct |
11 |
Correct |
502 ms |
23176 KB |
Output is correct |
12 |
Correct |
511 ms |
22544 KB |
Output is correct |
13 |
Correct |
591 ms |
26152 KB |
Output is correct |
14 |
Correct |
527 ms |
23336 KB |
Output is correct |
15 |
Correct |
632 ms |
26452 KB |
Output is correct |
16 |
Correct |
63 ms |
8780 KB |
Output is correct |
17 |
Correct |
521 ms |
18660 KB |
Output is correct |
18 |
Correct |
446 ms |
21848 KB |
Output is correct |
19 |
Correct |
154 ms |
10688 KB |
Output is correct |
20 |
Correct |
204 ms |
11908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
115 ms |
7628 KB |
Output is correct |
2 |
Correct |
109 ms |
7224 KB |
Output is correct |
3 |
Correct |
106 ms |
7228 KB |
Output is correct |
4 |
Correct |
93 ms |
6732 KB |
Output is correct |
5 |
Correct |
104 ms |
7244 KB |
Output is correct |
6 |
Correct |
91 ms |
6716 KB |
Output is correct |
7 |
Correct |
101 ms |
7188 KB |
Output is correct |
8 |
Correct |
95 ms |
6748 KB |
Output is correct |
9 |
Correct |
98 ms |
7064 KB |
Output is correct |
10 |
Correct |
86 ms |
6620 KB |
Output is correct |
11 |
Correct |
87 ms |
6888 KB |
Output is correct |
12 |
Correct |
84 ms |
6604 KB |
Output is correct |
13 |
Correct |
91 ms |
6472 KB |
Output is correct |
14 |
Correct |
88 ms |
6740 KB |
Output is correct |
15 |
Correct |
87 ms |
6732 KB |
Output is correct |
16 |
Correct |
30 ms |
4564 KB |
Output is correct |
17 |
Correct |
73 ms |
5504 KB |
Output is correct |
18 |
Correct |
76 ms |
5560 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
514 ms |
18452 KB |
Output is correct |
2 |
Correct |
531 ms |
17036 KB |
Output is correct |
3 |
Correct |
578 ms |
16640 KB |
Output is correct |
4 |
Correct |
452 ms |
15580 KB |
Output is correct |
5 |
Correct |
490 ms |
18824 KB |
Output is correct |
6 |
Correct |
455 ms |
19444 KB |
Output is correct |
7 |
Correct |
480 ms |
19924 KB |
Output is correct |
8 |
Correct |
434 ms |
17864 KB |
Output is correct |
9 |
Correct |
444 ms |
17228 KB |
Output is correct |
10 |
Correct |
462 ms |
16260 KB |
Output is correct |
11 |
Correct |
429 ms |
16740 KB |
Output is correct |
12 |
Correct |
438 ms |
13888 KB |
Output is correct |
13 |
Correct |
470 ms |
15884 KB |
Output is correct |
14 |
Correct |
512 ms |
18424 KB |
Output is correct |
15 |
Correct |
457 ms |
15756 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
393 ms |
8024 KB |
Output is correct |
18 |
Correct |
402 ms |
11712 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
746 ms |
27240 KB |
Output is correct |
22 |
Correct |
738 ms |
28708 KB |
Output is correct |
23 |
Correct |
554 ms |
23768 KB |
Output is correct |
24 |
Correct |
560 ms |
22936 KB |
Output is correct |
25 |
Correct |
562 ms |
23340 KB |
Output is correct |
26 |
Correct |
535 ms |
22504 KB |
Output is correct |
27 |
Correct |
667 ms |
27048 KB |
Output is correct |
28 |
Correct |
646 ms |
25888 KB |
Output is correct |
29 |
Correct |
561 ms |
23388 KB |
Output is correct |
30 |
Correct |
622 ms |
25684 KB |
Output is correct |
31 |
Correct |
502 ms |
23176 KB |
Output is correct |
32 |
Correct |
511 ms |
22544 KB |
Output is correct |
33 |
Correct |
591 ms |
26152 KB |
Output is correct |
34 |
Correct |
527 ms |
23336 KB |
Output is correct |
35 |
Correct |
632 ms |
26452 KB |
Output is correct |
36 |
Correct |
63 ms |
8780 KB |
Output is correct |
37 |
Correct |
521 ms |
18660 KB |
Output is correct |
38 |
Correct |
446 ms |
21848 KB |
Output is correct |
39 |
Correct |
154 ms |
10688 KB |
Output is correct |
40 |
Correct |
204 ms |
11908 KB |
Output is correct |
41 |
Correct |
115 ms |
7628 KB |
Output is correct |
42 |
Correct |
109 ms |
7224 KB |
Output is correct |
43 |
Correct |
106 ms |
7228 KB |
Output is correct |
44 |
Correct |
93 ms |
6732 KB |
Output is correct |
45 |
Correct |
104 ms |
7244 KB |
Output is correct |
46 |
Correct |
91 ms |
6716 KB |
Output is correct |
47 |
Correct |
101 ms |
7188 KB |
Output is correct |
48 |
Correct |
95 ms |
6748 KB |
Output is correct |
49 |
Correct |
98 ms |
7064 KB |
Output is correct |
50 |
Correct |
86 ms |
6620 KB |
Output is correct |
51 |
Correct |
87 ms |
6888 KB |
Output is correct |
52 |
Correct |
84 ms |
6604 KB |
Output is correct |
53 |
Correct |
91 ms |
6472 KB |
Output is correct |
54 |
Correct |
88 ms |
6740 KB |
Output is correct |
55 |
Correct |
87 ms |
6732 KB |
Output is correct |
56 |
Correct |
30 ms |
4564 KB |
Output is correct |
57 |
Correct |
73 ms |
5504 KB |
Output is correct |
58 |
Correct |
76 ms |
5560 KB |
Output is correct |
59 |
Correct |
0 ms |
340 KB |
Output is correct |
60 |
Correct |
0 ms |
340 KB |
Output is correct |
61 |
Correct |
975 ms |
34316 KB |
Output is correct |
62 |
Correct |
872 ms |
32852 KB |
Output is correct |
63 |
Correct |
905 ms |
32176 KB |
Output is correct |
64 |
Correct |
758 ms |
31660 KB |
Output is correct |
65 |
Correct |
798 ms |
33224 KB |
Output is correct |
66 |
Correct |
758 ms |
31736 KB |
Output is correct |
67 |
Correct |
725 ms |
31156 KB |
Output is correct |
68 |
Correct |
711 ms |
29972 KB |
Output is correct |
69 |
Correct |
761 ms |
32084 KB |
Output is correct |
70 |
Correct |
680 ms |
28792 KB |
Output is correct |
71 |
Correct |
630 ms |
31476 KB |
Output is correct |
72 |
Correct |
689 ms |
29180 KB |
Output is correct |
73 |
Correct |
777 ms |
30348 KB |
Output is correct |
74 |
Correct |
682 ms |
31596 KB |
Output is correct |
75 |
Correct |
677 ms |
30360 KB |
Output is correct |
76 |
Correct |
63 ms |
8840 KB |
Output is correct |
77 |
Correct |
617 ms |
18796 KB |
Output is correct |
78 |
Correct |
557 ms |
21644 KB |
Output is correct |
79 |
Correct |
0 ms |
340 KB |
Output is correct |
80 |
Correct |
0 ms |
340 KB |
Output is correct |