#include<bits/stdc++.h>
using namespace std;
typedef long long llint;
typedef pair <int, int> pi;
const int MAXN = 200005;
const int MAXQ = 1000005;
int n, q, siz, ofs = 1, flg;
int p[MAXN], nxt[MAXN], sol[MAXQ];
int t[MAXN * 8];
set < pair <int, pi> > st;
vector < pair <int, pi> > v;
vector <pi> queries[MAXN];
void compute_next () {
vector <int> st;
st.push_back(n + 1);
p[n + 1] = 1e9;
for (int i = n; i >= 1; i--) {
while (p[i] > p[st.back()]) st.pop_back();
nxt[i] = st.back();
st.push_back(i);
}
}
int get_idx (int a, int b) {
return lower_bound(v.begin(), v.end(), make_pair(p[a], make_pair(a, b))) - v.begin();
}
void tour_init () {
sort(v.begin(), v.end());
while (ofs < v.size()) ofs *= 2;
}
void update (int i, int tip) {
t[i + ofs] = v[i].second.second - v[i].second.first + 1;
if (tip == -1) t[i + ofs] = 0;
i = (i + ofs) / 2;
while (i > 0) {
t[i] = t[2 * i] + t[2 * i + 1];
i /= 2;
}
}
int upit (int x, int val, int lo = 0, int hi = ofs - 1) {
if (lo == hi) return p[v[lo].second.first + val - 1];
if (val <= t[2 * x]) return upit(2 * x, val, lo, (lo + hi) / 2);
return upit(2 * x + 1, val - t[2 * x], (lo + hi) / 2 + 1, hi);
}
void ubaci (int a, int b) {
st.insert({p[a], {a, b}});
siz += b - a + 1;
if (flg == 0) v.push_back({p[a], {a, b}});
if (flg == 1) update(get_idx(a, b), 1);
}
void podijeli (int a, int b) {
while (a <= b) {
int lim = min(nxt[a] - 1, b);
ubaci(a, lim);
a = nxt[a];
}
}
void remove_from_back () {
while (1) {
int a = (st.rbegin()->second).first;
int b = (st.rbegin()->second).second;
if (siz - (b - a + 1) < n / 2) break;
auto it = st.end(); it--;
st.erase(it);
siz -= b - a + 1;
}
}
void update_mid () {
int a = (st.rbegin()->second).first;
int b = (st.rbegin()->second).second;
st.erase({p[a], {a, b}});
siz -= b - a + 1;
if (flg) update(get_idx(a, b), -1);
int ofs = n / 2 - siz;
ubaci(a, a + ofs - 1);
podijeli(a + ofs, b);
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
for (int i = 1; i <= q; i++) {
int t, pos;
cin >> t >> pos;
t = min(t, n);
queries[t].push_back({i, pos});
}
compute_next();
podijeli(1, n);
while (1) {
remove_from_back();
if (siz == n / 2) break;
update_mid();
}
tour_init();
siz = 0; flg = 1;
st.clear();
podijeli(1, n);
for (int t = 0; t <= n; t++) {
for (auto pp : queries[t]) {
int i = pp.first, pos = pp.second;
sol[i] = upit(1, pos);
}
if (siz > n / 2) {
remove_from_back();
update_mid();
}
}
for (int i = 1; i <= q; i++) {
cout << sol[i] << '\n';
}
return 0;
}
Compilation message
Main.cpp: In function 'void tour_init()':
Main.cpp:35:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | while (ofs < v.size()) ofs *= 2;
| ~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
280 ms |
24296 KB |
Output is correct |
2 |
Correct |
269 ms |
25668 KB |
Output is correct |
3 |
Correct |
239 ms |
24984 KB |
Output is correct |
4 |
Correct |
195 ms |
27408 KB |
Output is correct |
5 |
Correct |
220 ms |
29296 KB |
Output is correct |
6 |
Correct |
188 ms |
29524 KB |
Output is correct |
7 |
Correct |
220 ms |
29004 KB |
Output is correct |
8 |
Correct |
196 ms |
27512 KB |
Output is correct |
9 |
Correct |
197 ms |
26888 KB |
Output is correct |
10 |
Correct |
204 ms |
26264 KB |
Output is correct |
11 |
Correct |
200 ms |
26700 KB |
Output is correct |
12 |
Correct |
211 ms |
24540 KB |
Output is correct |
13 |
Correct |
197 ms |
25196 KB |
Output is correct |
14 |
Correct |
215 ms |
29400 KB |
Output is correct |
15 |
Correct |
226 ms |
27516 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
226 ms |
25516 KB |
Output is correct |
18 |
Correct |
182 ms |
25492 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
512 ms |
52560 KB |
Output is correct |
2 |
Correct |
457 ms |
52080 KB |
Output is correct |
3 |
Correct |
421 ms |
47440 KB |
Output is correct |
4 |
Correct |
303 ms |
33900 KB |
Output is correct |
5 |
Correct |
349 ms |
34892 KB |
Output is correct |
6 |
Correct |
313 ms |
35540 KB |
Output is correct |
7 |
Correct |
312 ms |
35424 KB |
Output is correct |
8 |
Correct |
332 ms |
37960 KB |
Output is correct |
9 |
Correct |
297 ms |
33528 KB |
Output is correct |
10 |
Correct |
268 ms |
33472 KB |
Output is correct |
11 |
Correct |
212 ms |
27312 KB |
Output is correct |
12 |
Correct |
236 ms |
27720 KB |
Output is correct |
13 |
Correct |
280 ms |
33004 KB |
Output is correct |
14 |
Correct |
231 ms |
31332 KB |
Output is correct |
15 |
Correct |
331 ms |
36268 KB |
Output is correct |
16 |
Correct |
23 ms |
7760 KB |
Output is correct |
17 |
Correct |
359 ms |
46544 KB |
Output is correct |
18 |
Correct |
174 ms |
25596 KB |
Output is correct |
19 |
Correct |
69 ms |
12316 KB |
Output is correct |
20 |
Correct |
84 ms |
13384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
165 ms |
17688 KB |
Output is correct |
2 |
Correct |
138 ms |
16700 KB |
Output is correct |
3 |
Correct |
148 ms |
15776 KB |
Output is correct |
4 |
Correct |
53 ms |
10392 KB |
Output is correct |
5 |
Correct |
150 ms |
14124 KB |
Output is correct |
6 |
Correct |
67 ms |
10996 KB |
Output is correct |
7 |
Correct |
87 ms |
12212 KB |
Output is correct |
8 |
Correct |
73 ms |
11576 KB |
Output is correct |
9 |
Correct |
108 ms |
13180 KB |
Output is correct |
10 |
Correct |
42 ms |
9420 KB |
Output is correct |
11 |
Correct |
42 ms |
9320 KB |
Output is correct |
12 |
Correct |
37 ms |
9432 KB |
Output is correct |
13 |
Correct |
41 ms |
9552 KB |
Output is correct |
14 |
Correct |
42 ms |
9736 KB |
Output is correct |
15 |
Correct |
45 ms |
8900 KB |
Output is correct |
16 |
Correct |
13 ms |
6356 KB |
Output is correct |
17 |
Correct |
88 ms |
16248 KB |
Output is correct |
18 |
Correct |
30 ms |
8532 KB |
Output is correct |
19 |
Correct |
4 ms |
4948 KB |
Output is correct |
20 |
Correct |
4 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
280 ms |
24296 KB |
Output is correct |
2 |
Correct |
269 ms |
25668 KB |
Output is correct |
3 |
Correct |
239 ms |
24984 KB |
Output is correct |
4 |
Correct |
195 ms |
27408 KB |
Output is correct |
5 |
Correct |
220 ms |
29296 KB |
Output is correct |
6 |
Correct |
188 ms |
29524 KB |
Output is correct |
7 |
Correct |
220 ms |
29004 KB |
Output is correct |
8 |
Correct |
196 ms |
27512 KB |
Output is correct |
9 |
Correct |
197 ms |
26888 KB |
Output is correct |
10 |
Correct |
204 ms |
26264 KB |
Output is correct |
11 |
Correct |
200 ms |
26700 KB |
Output is correct |
12 |
Correct |
211 ms |
24540 KB |
Output is correct |
13 |
Correct |
197 ms |
25196 KB |
Output is correct |
14 |
Correct |
215 ms |
29400 KB |
Output is correct |
15 |
Correct |
226 ms |
27516 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
226 ms |
25516 KB |
Output is correct |
18 |
Correct |
182 ms |
25492 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
512 ms |
52560 KB |
Output is correct |
22 |
Correct |
457 ms |
52080 KB |
Output is correct |
23 |
Correct |
421 ms |
47440 KB |
Output is correct |
24 |
Correct |
303 ms |
33900 KB |
Output is correct |
25 |
Correct |
349 ms |
34892 KB |
Output is correct |
26 |
Correct |
313 ms |
35540 KB |
Output is correct |
27 |
Correct |
312 ms |
35424 KB |
Output is correct |
28 |
Correct |
332 ms |
37960 KB |
Output is correct |
29 |
Correct |
297 ms |
33528 KB |
Output is correct |
30 |
Correct |
268 ms |
33472 KB |
Output is correct |
31 |
Correct |
212 ms |
27312 KB |
Output is correct |
32 |
Correct |
236 ms |
27720 KB |
Output is correct |
33 |
Correct |
280 ms |
33004 KB |
Output is correct |
34 |
Correct |
231 ms |
31332 KB |
Output is correct |
35 |
Correct |
331 ms |
36268 KB |
Output is correct |
36 |
Correct |
23 ms |
7760 KB |
Output is correct |
37 |
Correct |
359 ms |
46544 KB |
Output is correct |
38 |
Correct |
174 ms |
25596 KB |
Output is correct |
39 |
Correct |
69 ms |
12316 KB |
Output is correct |
40 |
Correct |
84 ms |
13384 KB |
Output is correct |
41 |
Correct |
165 ms |
17688 KB |
Output is correct |
42 |
Correct |
138 ms |
16700 KB |
Output is correct |
43 |
Correct |
148 ms |
15776 KB |
Output is correct |
44 |
Correct |
53 ms |
10392 KB |
Output is correct |
45 |
Correct |
150 ms |
14124 KB |
Output is correct |
46 |
Correct |
67 ms |
10996 KB |
Output is correct |
47 |
Correct |
87 ms |
12212 KB |
Output is correct |
48 |
Correct |
73 ms |
11576 KB |
Output is correct |
49 |
Correct |
108 ms |
13180 KB |
Output is correct |
50 |
Correct |
42 ms |
9420 KB |
Output is correct |
51 |
Correct |
42 ms |
9320 KB |
Output is correct |
52 |
Correct |
37 ms |
9432 KB |
Output is correct |
53 |
Correct |
41 ms |
9552 KB |
Output is correct |
54 |
Correct |
42 ms |
9736 KB |
Output is correct |
55 |
Correct |
45 ms |
8900 KB |
Output is correct |
56 |
Correct |
13 ms |
6356 KB |
Output is correct |
57 |
Correct |
88 ms |
16248 KB |
Output is correct |
58 |
Correct |
30 ms |
8532 KB |
Output is correct |
59 |
Correct |
4 ms |
4948 KB |
Output is correct |
60 |
Correct |
4 ms |
4948 KB |
Output is correct |
61 |
Correct |
784 ms |
55612 KB |
Output is correct |
62 |
Correct |
632 ms |
54304 KB |
Output is correct |
63 |
Correct |
771 ms |
49016 KB |
Output is correct |
64 |
Correct |
539 ms |
42340 KB |
Output is correct |
65 |
Correct |
584 ms |
45540 KB |
Output is correct |
66 |
Correct |
548 ms |
42840 KB |
Output is correct |
67 |
Correct |
426 ms |
39472 KB |
Output is correct |
68 |
Correct |
417 ms |
39988 KB |
Output is correct |
69 |
Correct |
456 ms |
41248 KB |
Output is correct |
70 |
Correct |
347 ms |
36472 KB |
Output is correct |
71 |
Correct |
279 ms |
37600 KB |
Output is correct |
72 |
Correct |
341 ms |
34800 KB |
Output is correct |
73 |
Correct |
310 ms |
35716 KB |
Output is correct |
74 |
Correct |
338 ms |
38492 KB |
Output is correct |
75 |
Correct |
364 ms |
35656 KB |
Output is correct |
76 |
Correct |
22 ms |
7212 KB |
Output is correct |
77 |
Correct |
454 ms |
45368 KB |
Output is correct |
78 |
Correct |
231 ms |
31068 KB |
Output is correct |
79 |
Correct |
3 ms |
4948 KB |
Output is correct |
80 |
Correct |
3 ms |
4948 KB |
Output is correct |