//BITARO BITARO BITARO
#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
int ans[N], a[N], nxt[N], fen[N], total_len, ttp;
vector<array<int, 3>> values;
vector<pair<int, int>> queries[N];
set<array<int, 3>> s;
int n, q;
void modif(int u, int val) {
for(int i = u + 1; i < N; i += i & -i) fen[i] += val;
}
int query(int u) {
int ret = 0;
for(int i = u + 1; i >= 1; i -= i & -i) ret += fen[i];
return ret;
}
int id(array<int, 3> x) {
return lower_bound(values.begin(), values.end(), x) - values.begin();
}
void fix() {
while(true) {
int A = (*s.rbegin())[1], B = (*s.rbegin())[2];
if(total_len - (B - A + 1) >= n / 2) {
s.erase(--s.end());
total_len -= B - A + 1;
}
else break;
}
}
void add(int x, int y) {
s.insert({a[x], x, y});
total_len += y - x + 1;
if(!ttp) values.push_back({a[x], x, y});
else modif(id({a[x], x, y}), y - x + 1);
}
void go(int l, int r) {
for(int i = l; i <= r; i = nxt[i]) {
add(i, min(nxt[i] - 1, r));
}
}
void remove_middle() {
if(total_len == n / 2) return;
array<int, 3> x = *--s.end();
s.erase(--s.end());
if(ttp) modif(id(x), -(x[2] - x[1] + 1));
int l = x[1], r = x[2];
total_len -= r - l + 1;
int need_add = n / 2 - total_len;
add(l, l + need_add - 1);
go(l + need_add, r);
}
int main() {
cin >> n >> q;
for(int i = 1; i <= n; ++i) cin >> a[i];
for(int i = 1, t, pos; i <= q; ++i) {
cin >> t >> pos; t = min(t, n);
queries[t].push_back(make_pair(i, pos));
}
stack<int> st;
for(int i = n; i >= 1; --i) {
while(!st.empty() && a[st.top()] < a[i]) st.pop();
nxt[i] = (st.empty() ? n + 1 : st.top());
st.push(i);
}
go(1, n);
while(true) {
fix();
if(total_len == n / 2) break;
remove_middle();
}
s.clear(); ttp = 1; total_len = 0;
sort(values.begin(), values.end());
values.erase(unique(values.begin(), values.end()), values.end());
go(1, n);
for(int t = 0; t <= n; ++t) {
for(auto x: queries[t]) {
int pos = x.second, idx = x.first;
int f = -1, l = 0, r = N - 1;
while(l <= r) {
int mid = l + r >> 1;
if(query(mid) < pos) {
l = mid + 1;
f = mid;
} else {
r = mid - 1;
}
}
if(f >= 0) pos -= query(f);
++f;
ans[idx] = a[values[f][1] + pos - 1];
}
if(total_len > n / 2) {
fix();
remove_middle();
}
}
for(int i = 1; i <= q; ++i) {
cout << ans[i] << "\n";
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:85:29: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
85 | int mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
811 ms |
65548 KB |
Output is correct |
2 |
Correct |
768 ms |
71620 KB |
Output is correct |
3 |
Correct |
726 ms |
71080 KB |
Output is correct |
4 |
Correct |
631 ms |
68924 KB |
Output is correct |
5 |
Correct |
678 ms |
73100 KB |
Output is correct |
6 |
Correct |
662 ms |
73036 KB |
Output is correct |
7 |
Correct |
689 ms |
73672 KB |
Output is correct |
8 |
Correct |
638 ms |
71404 KB |
Output is correct |
9 |
Correct |
671 ms |
70648 KB |
Output is correct |
10 |
Correct |
715 ms |
70200 KB |
Output is correct |
11 |
Correct |
637 ms |
70512 KB |
Output is correct |
12 |
Correct |
611 ms |
67956 KB |
Output is correct |
13 |
Correct |
616 ms |
69404 KB |
Output is correct |
14 |
Correct |
664 ms |
72328 KB |
Output is correct |
15 |
Correct |
633 ms |
70240 KB |
Output is correct |
16 |
Correct |
22 ms |
47316 KB |
Output is correct |
17 |
Correct |
709 ms |
68228 KB |
Output is correct |
18 |
Correct |
605 ms |
68104 KB |
Output is correct |
19 |
Correct |
24 ms |
47316 KB |
Output is correct |
20 |
Correct |
23 ms |
47256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1227 ms |
79676 KB |
Output is correct |
2 |
Correct |
1160 ms |
94692 KB |
Output is correct |
3 |
Correct |
947 ms |
89272 KB |
Output is correct |
4 |
Correct |
851 ms |
78544 KB |
Output is correct |
5 |
Correct |
881 ms |
79504 KB |
Output is correct |
6 |
Correct |
833 ms |
78056 KB |
Output is correct |
7 |
Correct |
997 ms |
82684 KB |
Output is correct |
8 |
Correct |
961 ms |
82148 KB |
Output is correct |
9 |
Correct |
822 ms |
78760 KB |
Output is correct |
10 |
Correct |
897 ms |
78596 KB |
Output is correct |
11 |
Correct |
711 ms |
72716 KB |
Output is correct |
12 |
Correct |
718 ms |
73620 KB |
Output is correct |
13 |
Correct |
833 ms |
77692 KB |
Output is correct |
14 |
Correct |
710 ms |
74060 KB |
Output is correct |
15 |
Correct |
866 ms |
79164 KB |
Output is correct |
16 |
Correct |
93 ms |
50128 KB |
Output is correct |
17 |
Correct |
965 ms |
91388 KB |
Output is correct |
18 |
Correct |
643 ms |
70784 KB |
Output is correct |
19 |
Correct |
218 ms |
55500 KB |
Output is correct |
20 |
Correct |
272 ms |
57128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
251 ms |
57404 KB |
Output is correct |
2 |
Correct |
238 ms |
57440 KB |
Output is correct |
3 |
Correct |
256 ms |
56172 KB |
Output is correct |
4 |
Correct |
157 ms |
51252 KB |
Output is correct |
5 |
Correct |
251 ms |
55076 KB |
Output is correct |
6 |
Correct |
161 ms |
51592 KB |
Output is correct |
7 |
Correct |
195 ms |
53496 KB |
Output is correct |
8 |
Correct |
176 ms |
52532 KB |
Output is correct |
9 |
Correct |
201 ms |
53760 KB |
Output is correct |
10 |
Correct |
135 ms |
50336 KB |
Output is correct |
11 |
Correct |
135 ms |
50696 KB |
Output is correct |
12 |
Correct |
127 ms |
50364 KB |
Output is correct |
13 |
Correct |
143 ms |
50404 KB |
Output is correct |
14 |
Correct |
132 ms |
50496 KB |
Output is correct |
15 |
Correct |
123 ms |
50176 KB |
Output is correct |
16 |
Correct |
52 ms |
48208 KB |
Output is correct |
17 |
Correct |
213 ms |
57660 KB |
Output is correct |
18 |
Correct |
115 ms |
49868 KB |
Output is correct |
19 |
Correct |
24 ms |
47316 KB |
Output is correct |
20 |
Correct |
24 ms |
47264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
811 ms |
65548 KB |
Output is correct |
2 |
Correct |
768 ms |
71620 KB |
Output is correct |
3 |
Correct |
726 ms |
71080 KB |
Output is correct |
4 |
Correct |
631 ms |
68924 KB |
Output is correct |
5 |
Correct |
678 ms |
73100 KB |
Output is correct |
6 |
Correct |
662 ms |
73036 KB |
Output is correct |
7 |
Correct |
689 ms |
73672 KB |
Output is correct |
8 |
Correct |
638 ms |
71404 KB |
Output is correct |
9 |
Correct |
671 ms |
70648 KB |
Output is correct |
10 |
Correct |
715 ms |
70200 KB |
Output is correct |
11 |
Correct |
637 ms |
70512 KB |
Output is correct |
12 |
Correct |
611 ms |
67956 KB |
Output is correct |
13 |
Correct |
616 ms |
69404 KB |
Output is correct |
14 |
Correct |
664 ms |
72328 KB |
Output is correct |
15 |
Correct |
633 ms |
70240 KB |
Output is correct |
16 |
Correct |
22 ms |
47316 KB |
Output is correct |
17 |
Correct |
709 ms |
68228 KB |
Output is correct |
18 |
Correct |
605 ms |
68104 KB |
Output is correct |
19 |
Correct |
24 ms |
47316 KB |
Output is correct |
20 |
Correct |
23 ms |
47256 KB |
Output is correct |
21 |
Correct |
1227 ms |
79676 KB |
Output is correct |
22 |
Correct |
1160 ms |
94692 KB |
Output is correct |
23 |
Correct |
947 ms |
89272 KB |
Output is correct |
24 |
Correct |
851 ms |
78544 KB |
Output is correct |
25 |
Correct |
881 ms |
79504 KB |
Output is correct |
26 |
Correct |
833 ms |
78056 KB |
Output is correct |
27 |
Correct |
997 ms |
82684 KB |
Output is correct |
28 |
Correct |
961 ms |
82148 KB |
Output is correct |
29 |
Correct |
822 ms |
78760 KB |
Output is correct |
30 |
Correct |
897 ms |
78596 KB |
Output is correct |
31 |
Correct |
711 ms |
72716 KB |
Output is correct |
32 |
Correct |
718 ms |
73620 KB |
Output is correct |
33 |
Correct |
833 ms |
77692 KB |
Output is correct |
34 |
Correct |
710 ms |
74060 KB |
Output is correct |
35 |
Correct |
866 ms |
79164 KB |
Output is correct |
36 |
Correct |
93 ms |
50128 KB |
Output is correct |
37 |
Correct |
965 ms |
91388 KB |
Output is correct |
38 |
Correct |
643 ms |
70784 KB |
Output is correct |
39 |
Correct |
218 ms |
55500 KB |
Output is correct |
40 |
Correct |
272 ms |
57128 KB |
Output is correct |
41 |
Correct |
251 ms |
57404 KB |
Output is correct |
42 |
Correct |
238 ms |
57440 KB |
Output is correct |
43 |
Correct |
256 ms |
56172 KB |
Output is correct |
44 |
Correct |
157 ms |
51252 KB |
Output is correct |
45 |
Correct |
251 ms |
55076 KB |
Output is correct |
46 |
Correct |
161 ms |
51592 KB |
Output is correct |
47 |
Correct |
195 ms |
53496 KB |
Output is correct |
48 |
Correct |
176 ms |
52532 KB |
Output is correct |
49 |
Correct |
201 ms |
53760 KB |
Output is correct |
50 |
Correct |
135 ms |
50336 KB |
Output is correct |
51 |
Correct |
135 ms |
50696 KB |
Output is correct |
52 |
Correct |
127 ms |
50364 KB |
Output is correct |
53 |
Correct |
143 ms |
50404 KB |
Output is correct |
54 |
Correct |
132 ms |
50496 KB |
Output is correct |
55 |
Correct |
123 ms |
50176 KB |
Output is correct |
56 |
Correct |
52 ms |
48208 KB |
Output is correct |
57 |
Correct |
213 ms |
57660 KB |
Output is correct |
58 |
Correct |
115 ms |
49868 KB |
Output is correct |
59 |
Correct |
24 ms |
47316 KB |
Output is correct |
60 |
Correct |
24 ms |
47264 KB |
Output is correct |
61 |
Correct |
1569 ms |
100116 KB |
Output is correct |
62 |
Correct |
1448 ms |
98944 KB |
Output is correct |
63 |
Correct |
1452 ms |
95140 KB |
Output is correct |
64 |
Correct |
1142 ms |
88100 KB |
Output is correct |
65 |
Correct |
1314 ms |
90140 KB |
Output is correct |
66 |
Correct |
1203 ms |
88120 KB |
Output is correct |
67 |
Correct |
1085 ms |
84916 KB |
Output is correct |
68 |
Correct |
1083 ms |
84444 KB |
Output is correct |
69 |
Correct |
1082 ms |
86960 KB |
Output is correct |
70 |
Correct |
944 ms |
81192 KB |
Output is correct |
71 |
Correct |
871 ms |
82100 KB |
Output is correct |
72 |
Correct |
948 ms |
81668 KB |
Output is correct |
73 |
Correct |
891 ms |
81752 KB |
Output is correct |
74 |
Correct |
911 ms |
84040 KB |
Output is correct |
75 |
Correct |
931 ms |
82580 KB |
Output is correct |
76 |
Correct |
81 ms |
50176 KB |
Output is correct |
77 |
Correct |
1135 ms |
91484 KB |
Output is correct |
78 |
Correct |
730 ms |
75536 KB |
Output is correct |
79 |
Correct |
28 ms |
47276 KB |
Output is correct |
80 |
Correct |
25 ms |
47280 KB |
Output is correct |