#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <vector>
#include <stack>
#define pb push_back
#define ff first
#define ss second
using namespace std;
const int MX = 1e6 + 5;
int n, q, l, r, cur;
int p[MX], ans[MX], fix[MX], nxt[MX], tree[4 * MX];
pair<pair<int, int>, int> t[MX];
set<pair<pair<int, int>, int> > s;
void update(int id, int l, int r, int x, int val) {
if(l > x || r < x) return;
tree[id] += val;
if(l == r) return;
int mid = (l + r) / 2;
update(id * 2 + 1, l, mid, x, val);
update(id * 2 + 2, mid + 1, r, x, val);
}
int sum(int id, int l, int r, int L, int R) {
if(L > r || l > R) return 0;
if(L <= l && r <= R) return tree[id];
int mid = (l + r) / 2;
return sum(id * 2 + 1, l, mid, L, R) + sum(id * 2 + 2, mid + 1, r, L, R);
}
int find(int id, int l, int r, int x) {
if(l == r) {
if(tree[id] < x) return l;
else return l - 1;
}
int mid = (l + r) / 2;
if(tree[id * 2 + 1] < x) return find(id * 2 + 2, mid + 1, r, x - tree[id * 2 + 1]);
else return find(id * 2 + 1, l, mid, x);
}
void pre() {
while((*s.rbegin()).ff.ss + r <= n / 2) {
for(int i = 0; i < (*s.rbegin()).ff.ss; i++) {
fix[n - r - (*s.rbegin()).ff.ss + i] = p[(*s.rbegin()).ss + i];
}
r += (*s.rbegin()).ff.ss;
s.erase(*s.rbegin());
}
}
void go() {
if(r == n / 2) return;
pair<pair<int, int>, int> x = *s.rbegin();
s.erase(*s.rbegin());
s.insert({{x.ff.ff, x.ff.ss + r - n / 2}, x.ss});
update(0, 1, n, x.ff.ff, r - n / 2);
int ind = x.ss + x.ff.ss + r - n / 2;
while(ind < x.ss + x.ff.ss) {
s.insert({{p[ind], min(nxt[ind], x.ss + x.ff.ss) - ind}, ind});
update(0, 1, n, p[ind], min(nxt[ind], x.ss + x.ff.ss) - ind);
ind = nxt[ind];
}
pre();
}
int main() {
ios::sync_with_stdio(0);cin.tie(0);
memset(fix, -1, sizeof(fix));
cin >> n >> q;
stack<pair<int, int > > st;
for(int i = 0; i <= n; i++) {
if (i != n) {
cin >> p[i];
}
else {
p[i] = INT_MAX;
}
while(!st.empty() && st.top().ff < p[i]) {
nxt[st.top().ss] = i;
st.pop();
}
st.push({p[i], i});
}
while(l < n) {
s.insert({{p[l], nxt[l] - l}, l});
update(0, 1, n, p[l], nxt[l] - l);
l = nxt[l];
}
pre();
for(int i = 0; i < q; i++) {
cin >> t[i].ff.ff >> t[i].ff.ss;
t[i].ss = i;
if (t[i].ff.ff > n) t[i].ff.ff = n;
}
sort(t, t + q);
for(int i = 0; i <= n; i++) {
if(t[cur].ff.ff > i) {
go();
continue;
}
while(cur < q && t[cur].ff.ff == i) {
if(t[cur].ff.ss + r <= n) {
int k = find(0, 1, n, t[cur].ff.ss) + 1;
auto itr = s.lower_bound({{k, 0}, 0});
int cnt = sum(0, 1, n, 1, k - 1);
ans[t[cur].ss] = p[(*itr).ss + t[cur].ff.ss - cnt - 1];
cur++;
}
else {
ans[t[cur].ss] = fix[t[cur].ff.ss - 1];
cur++;
}
}
go();
}
for(int i = 0; i < q; i++) {
cout << ans[i] << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
287 ms |
31472 KB |
Output is correct |
2 |
Correct |
306 ms |
31180 KB |
Output is correct |
3 |
Correct |
319 ms |
30408 KB |
Output is correct |
4 |
Correct |
280 ms |
29008 KB |
Output is correct |
5 |
Correct |
298 ms |
30768 KB |
Output is correct |
6 |
Correct |
274 ms |
29316 KB |
Output is correct |
7 |
Correct |
310 ms |
30964 KB |
Output is correct |
8 |
Correct |
280 ms |
29452 KB |
Output is correct |
9 |
Correct |
283 ms |
29084 KB |
Output is correct |
10 |
Correct |
281 ms |
29612 KB |
Output is correct |
11 |
Correct |
279 ms |
29580 KB |
Output is correct |
12 |
Correct |
279 ms |
28396 KB |
Output is correct |
13 |
Correct |
308 ms |
29272 KB |
Output is correct |
14 |
Correct |
291 ms |
30200 KB |
Output is correct |
15 |
Correct |
313 ms |
30056 KB |
Output is correct |
16 |
Correct |
2 ms |
4180 KB |
Output is correct |
17 |
Correct |
317 ms |
28844 KB |
Output is correct |
18 |
Correct |
287 ms |
28520 KB |
Output is correct |
19 |
Correct |
2 ms |
4180 KB |
Output is correct |
20 |
Correct |
2 ms |
4248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
397 ms |
50644 KB |
Output is correct |
2 |
Correct |
376 ms |
52752 KB |
Output is correct |
3 |
Correct |
397 ms |
41764 KB |
Output is correct |
4 |
Correct |
302 ms |
38972 KB |
Output is correct |
5 |
Correct |
311 ms |
39884 KB |
Output is correct |
6 |
Correct |
317 ms |
38540 KB |
Output is correct |
7 |
Correct |
327 ms |
44844 KB |
Output is correct |
8 |
Correct |
308 ms |
43980 KB |
Output is correct |
9 |
Correct |
299 ms |
39048 KB |
Output is correct |
10 |
Correct |
291 ms |
41040 KB |
Output is correct |
11 |
Correct |
269 ms |
34516 KB |
Output is correct |
12 |
Correct |
258 ms |
35096 KB |
Output is correct |
13 |
Correct |
284 ms |
40192 KB |
Output is correct |
14 |
Correct |
279 ms |
35836 KB |
Output is correct |
15 |
Correct |
307 ms |
41840 KB |
Output is correct |
16 |
Correct |
23 ms |
9020 KB |
Output is correct |
17 |
Correct |
378 ms |
50128 KB |
Output is correct |
18 |
Correct |
235 ms |
32032 KB |
Output is correct |
19 |
Correct |
69 ms |
14660 KB |
Output is correct |
20 |
Correct |
75 ms |
16716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
81 ms |
12992 KB |
Output is correct |
2 |
Correct |
85 ms |
14664 KB |
Output is correct |
3 |
Correct |
101 ms |
12728 KB |
Output is correct |
4 |
Correct |
45 ms |
10356 KB |
Output is correct |
5 |
Correct |
72 ms |
12692 KB |
Output is correct |
6 |
Correct |
48 ms |
10512 KB |
Output is correct |
7 |
Correct |
67 ms |
11860 KB |
Output is correct |
8 |
Correct |
56 ms |
11208 KB |
Output is correct |
9 |
Correct |
65 ms |
11872 KB |
Output is correct |
10 |
Correct |
44 ms |
9744 KB |
Output is correct |
11 |
Correct |
43 ms |
9868 KB |
Output is correct |
12 |
Correct |
41 ms |
9632 KB |
Output is correct |
13 |
Correct |
49 ms |
9820 KB |
Output is correct |
14 |
Correct |
53 ms |
9884 KB |
Output is correct |
15 |
Correct |
41 ms |
9408 KB |
Output is correct |
16 |
Correct |
10 ms |
6740 KB |
Output is correct |
17 |
Correct |
68 ms |
15352 KB |
Output is correct |
18 |
Correct |
38 ms |
9184 KB |
Output is correct |
19 |
Correct |
2 ms |
4180 KB |
Output is correct |
20 |
Correct |
2 ms |
4180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
287 ms |
31472 KB |
Output is correct |
2 |
Correct |
306 ms |
31180 KB |
Output is correct |
3 |
Correct |
319 ms |
30408 KB |
Output is correct |
4 |
Correct |
280 ms |
29008 KB |
Output is correct |
5 |
Correct |
298 ms |
30768 KB |
Output is correct |
6 |
Correct |
274 ms |
29316 KB |
Output is correct |
7 |
Correct |
310 ms |
30964 KB |
Output is correct |
8 |
Correct |
280 ms |
29452 KB |
Output is correct |
9 |
Correct |
283 ms |
29084 KB |
Output is correct |
10 |
Correct |
281 ms |
29612 KB |
Output is correct |
11 |
Correct |
279 ms |
29580 KB |
Output is correct |
12 |
Correct |
279 ms |
28396 KB |
Output is correct |
13 |
Correct |
308 ms |
29272 KB |
Output is correct |
14 |
Correct |
291 ms |
30200 KB |
Output is correct |
15 |
Correct |
313 ms |
30056 KB |
Output is correct |
16 |
Correct |
2 ms |
4180 KB |
Output is correct |
17 |
Correct |
317 ms |
28844 KB |
Output is correct |
18 |
Correct |
287 ms |
28520 KB |
Output is correct |
19 |
Correct |
2 ms |
4180 KB |
Output is correct |
20 |
Correct |
2 ms |
4248 KB |
Output is correct |
21 |
Correct |
397 ms |
50644 KB |
Output is correct |
22 |
Correct |
376 ms |
52752 KB |
Output is correct |
23 |
Correct |
397 ms |
41764 KB |
Output is correct |
24 |
Correct |
302 ms |
38972 KB |
Output is correct |
25 |
Correct |
311 ms |
39884 KB |
Output is correct |
26 |
Correct |
317 ms |
38540 KB |
Output is correct |
27 |
Correct |
327 ms |
44844 KB |
Output is correct |
28 |
Correct |
308 ms |
43980 KB |
Output is correct |
29 |
Correct |
299 ms |
39048 KB |
Output is correct |
30 |
Correct |
291 ms |
41040 KB |
Output is correct |
31 |
Correct |
269 ms |
34516 KB |
Output is correct |
32 |
Correct |
258 ms |
35096 KB |
Output is correct |
33 |
Correct |
284 ms |
40192 KB |
Output is correct |
34 |
Correct |
279 ms |
35836 KB |
Output is correct |
35 |
Correct |
307 ms |
41840 KB |
Output is correct |
36 |
Correct |
23 ms |
9020 KB |
Output is correct |
37 |
Correct |
378 ms |
50128 KB |
Output is correct |
38 |
Correct |
235 ms |
32032 KB |
Output is correct |
39 |
Correct |
69 ms |
14660 KB |
Output is correct |
40 |
Correct |
75 ms |
16716 KB |
Output is correct |
41 |
Correct |
81 ms |
12992 KB |
Output is correct |
42 |
Correct |
85 ms |
14664 KB |
Output is correct |
43 |
Correct |
101 ms |
12728 KB |
Output is correct |
44 |
Correct |
45 ms |
10356 KB |
Output is correct |
45 |
Correct |
72 ms |
12692 KB |
Output is correct |
46 |
Correct |
48 ms |
10512 KB |
Output is correct |
47 |
Correct |
67 ms |
11860 KB |
Output is correct |
48 |
Correct |
56 ms |
11208 KB |
Output is correct |
49 |
Correct |
65 ms |
11872 KB |
Output is correct |
50 |
Correct |
44 ms |
9744 KB |
Output is correct |
51 |
Correct |
43 ms |
9868 KB |
Output is correct |
52 |
Correct |
41 ms |
9632 KB |
Output is correct |
53 |
Correct |
49 ms |
9820 KB |
Output is correct |
54 |
Correct |
53 ms |
9884 KB |
Output is correct |
55 |
Correct |
41 ms |
9408 KB |
Output is correct |
56 |
Correct |
10 ms |
6740 KB |
Output is correct |
57 |
Correct |
68 ms |
15352 KB |
Output is correct |
58 |
Correct |
38 ms |
9184 KB |
Output is correct |
59 |
Correct |
2 ms |
4180 KB |
Output is correct |
60 |
Correct |
2 ms |
4180 KB |
Output is correct |
61 |
Correct |
500 ms |
50088 KB |
Output is correct |
62 |
Correct |
577 ms |
52580 KB |
Output is correct |
63 |
Correct |
718 ms |
47932 KB |
Output is correct |
64 |
Correct |
472 ms |
44680 KB |
Output is correct |
65 |
Correct |
473 ms |
46468 KB |
Output is correct |
66 |
Correct |
426 ms |
44724 KB |
Output is correct |
67 |
Correct |
420 ms |
44340 KB |
Output is correct |
68 |
Correct |
443 ms |
43640 KB |
Output is correct |
69 |
Correct |
466 ms |
44036 KB |
Output is correct |
70 |
Correct |
422 ms |
41432 KB |
Output is correct |
71 |
Correct |
367 ms |
40032 KB |
Output is correct |
72 |
Correct |
425 ms |
41180 KB |
Output is correct |
73 |
Correct |
377 ms |
40764 KB |
Output is correct |
74 |
Correct |
396 ms |
41948 KB |
Output is correct |
75 |
Correct |
394 ms |
42032 KB |
Output is correct |
76 |
Correct |
18 ms |
8956 KB |
Output is correct |
77 |
Correct |
414 ms |
50344 KB |
Output is correct |
78 |
Correct |
369 ms |
37724 KB |
Output is correct |
79 |
Correct |
2 ms |
4180 KB |
Output is correct |
80 |
Correct |
2 ms |
4180 KB |
Output is correct |