#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
292 ms |
25100 KB |
Output is correct |
2 |
Correct |
300 ms |
25148 KB |
Output is correct |
3 |
Correct |
326 ms |
24332 KB |
Output is correct |
4 |
Correct |
280 ms |
24172 KB |
Output is correct |
5 |
Correct |
296 ms |
24976 KB |
Output is correct |
6 |
Correct |
294 ms |
24328 KB |
Output is correct |
7 |
Correct |
305 ms |
25388 KB |
Output is correct |
8 |
Correct |
304 ms |
24672 KB |
Output is correct |
9 |
Correct |
301 ms |
24528 KB |
Output is correct |
10 |
Correct |
294 ms |
24792 KB |
Output is correct |
11 |
Correct |
307 ms |
24768 KB |
Output is correct |
12 |
Correct |
306 ms |
24568 KB |
Output is correct |
13 |
Correct |
294 ms |
24848 KB |
Output is correct |
14 |
Correct |
297 ms |
25116 KB |
Output is correct |
15 |
Correct |
300 ms |
25364 KB |
Output is correct |
16 |
Correct |
2 ms |
4180 KB |
Output is correct |
17 |
Correct |
280 ms |
24984 KB |
Output is correct |
18 |
Correct |
293 ms |
24624 KB |
Output is correct |
19 |
Correct |
2 ms |
4180 KB |
Output is correct |
20 |
Correct |
2 ms |
4180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
390 ms |
37896 KB |
Output is correct |
2 |
Correct |
380 ms |
41024 KB |
Output is correct |
3 |
Correct |
418 ms |
32748 KB |
Output is correct |
4 |
Correct |
303 ms |
29908 KB |
Output is correct |
5 |
Correct |
319 ms |
30468 KB |
Output is correct |
6 |
Correct |
284 ms |
29504 KB |
Output is correct |
7 |
Correct |
315 ms |
32972 KB |
Output is correct |
8 |
Correct |
311 ms |
32828 KB |
Output is correct |
9 |
Correct |
316 ms |
29644 KB |
Output is correct |
10 |
Correct |
282 ms |
30644 KB |
Output is correct |
11 |
Correct |
263 ms |
26624 KB |
Output is correct |
12 |
Correct |
282 ms |
26708 KB |
Output is correct |
13 |
Correct |
283 ms |
30528 KB |
Output is correct |
14 |
Correct |
264 ms |
27168 KB |
Output is correct |
15 |
Correct |
293 ms |
30972 KB |
Output is correct |
16 |
Correct |
21 ms |
8908 KB |
Output is correct |
17 |
Correct |
352 ms |
42296 KB |
Output is correct |
18 |
Correct |
229 ms |
25764 KB |
Output is correct |
19 |
Correct |
69 ms |
13088 KB |
Output is correct |
20 |
Correct |
71 ms |
13556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
12636 KB |
Output is correct |
2 |
Correct |
87 ms |
14172 KB |
Output is correct |
3 |
Correct |
109 ms |
12336 KB |
Output is correct |
4 |
Correct |
50 ms |
10056 KB |
Output is correct |
5 |
Correct |
71 ms |
12300 KB |
Output is correct |
6 |
Correct |
47 ms |
10196 KB |
Output is correct |
7 |
Correct |
61 ms |
11344 KB |
Output is correct |
8 |
Correct |
56 ms |
10868 KB |
Output is correct |
9 |
Correct |
66 ms |
11404 KB |
Output is correct |
10 |
Correct |
43 ms |
9364 KB |
Output is correct |
11 |
Correct |
45 ms |
9576 KB |
Output is correct |
12 |
Correct |
41 ms |
9276 KB |
Output is correct |
13 |
Correct |
49 ms |
9508 KB |
Output is correct |
14 |
Correct |
43 ms |
9400 KB |
Output is correct |
15 |
Correct |
39 ms |
9156 KB |
Output is correct |
16 |
Correct |
12 ms |
6684 KB |
Output is correct |
17 |
Correct |
67 ms |
15228 KB |
Output is correct |
18 |
Correct |
39 ms |
9024 KB |
Output is correct |
19 |
Correct |
2 ms |
4252 KB |
Output is correct |
20 |
Correct |
2 ms |
4180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
292 ms |
25100 KB |
Output is correct |
2 |
Correct |
300 ms |
25148 KB |
Output is correct |
3 |
Correct |
326 ms |
24332 KB |
Output is correct |
4 |
Correct |
280 ms |
24172 KB |
Output is correct |
5 |
Correct |
296 ms |
24976 KB |
Output is correct |
6 |
Correct |
294 ms |
24328 KB |
Output is correct |
7 |
Correct |
305 ms |
25388 KB |
Output is correct |
8 |
Correct |
304 ms |
24672 KB |
Output is correct |
9 |
Correct |
301 ms |
24528 KB |
Output is correct |
10 |
Correct |
294 ms |
24792 KB |
Output is correct |
11 |
Correct |
307 ms |
24768 KB |
Output is correct |
12 |
Correct |
306 ms |
24568 KB |
Output is correct |
13 |
Correct |
294 ms |
24848 KB |
Output is correct |
14 |
Correct |
297 ms |
25116 KB |
Output is correct |
15 |
Correct |
300 ms |
25364 KB |
Output is correct |
16 |
Correct |
2 ms |
4180 KB |
Output is correct |
17 |
Correct |
280 ms |
24984 KB |
Output is correct |
18 |
Correct |
293 ms |
24624 KB |
Output is correct |
19 |
Correct |
2 ms |
4180 KB |
Output is correct |
20 |
Correct |
2 ms |
4180 KB |
Output is correct |
21 |
Correct |
390 ms |
37896 KB |
Output is correct |
22 |
Correct |
380 ms |
41024 KB |
Output is correct |
23 |
Correct |
418 ms |
32748 KB |
Output is correct |
24 |
Correct |
303 ms |
29908 KB |
Output is correct |
25 |
Correct |
319 ms |
30468 KB |
Output is correct |
26 |
Correct |
284 ms |
29504 KB |
Output is correct |
27 |
Correct |
315 ms |
32972 KB |
Output is correct |
28 |
Correct |
311 ms |
32828 KB |
Output is correct |
29 |
Correct |
316 ms |
29644 KB |
Output is correct |
30 |
Correct |
282 ms |
30644 KB |
Output is correct |
31 |
Correct |
263 ms |
26624 KB |
Output is correct |
32 |
Correct |
282 ms |
26708 KB |
Output is correct |
33 |
Correct |
283 ms |
30528 KB |
Output is correct |
34 |
Correct |
264 ms |
27168 KB |
Output is correct |
35 |
Correct |
293 ms |
30972 KB |
Output is correct |
36 |
Correct |
21 ms |
8908 KB |
Output is correct |
37 |
Correct |
352 ms |
42296 KB |
Output is correct |
38 |
Correct |
229 ms |
25764 KB |
Output is correct |
39 |
Correct |
69 ms |
13088 KB |
Output is correct |
40 |
Correct |
71 ms |
13556 KB |
Output is correct |
41 |
Correct |
83 ms |
12636 KB |
Output is correct |
42 |
Correct |
87 ms |
14172 KB |
Output is correct |
43 |
Correct |
109 ms |
12336 KB |
Output is correct |
44 |
Correct |
50 ms |
10056 KB |
Output is correct |
45 |
Correct |
71 ms |
12300 KB |
Output is correct |
46 |
Correct |
47 ms |
10196 KB |
Output is correct |
47 |
Correct |
61 ms |
11344 KB |
Output is correct |
48 |
Correct |
56 ms |
10868 KB |
Output is correct |
49 |
Correct |
66 ms |
11404 KB |
Output is correct |
50 |
Correct |
43 ms |
9364 KB |
Output is correct |
51 |
Correct |
45 ms |
9576 KB |
Output is correct |
52 |
Correct |
41 ms |
9276 KB |
Output is correct |
53 |
Correct |
49 ms |
9508 KB |
Output is correct |
54 |
Correct |
43 ms |
9400 KB |
Output is correct |
55 |
Correct |
39 ms |
9156 KB |
Output is correct |
56 |
Correct |
12 ms |
6684 KB |
Output is correct |
57 |
Correct |
67 ms |
15228 KB |
Output is correct |
58 |
Correct |
39 ms |
9024 KB |
Output is correct |
59 |
Correct |
2 ms |
4252 KB |
Output is correct |
60 |
Correct |
2 ms |
4180 KB |
Output is correct |
61 |
Correct |
504 ms |
37368 KB |
Output is correct |
62 |
Correct |
566 ms |
40348 KB |
Output is correct |
63 |
Correct |
708 ms |
36076 KB |
Output is correct |
64 |
Correct |
434 ms |
33080 KB |
Output is correct |
65 |
Correct |
448 ms |
34360 KB |
Output is correct |
66 |
Correct |
424 ms |
32996 KB |
Output is correct |
67 |
Correct |
414 ms |
32436 KB |
Output is correct |
68 |
Correct |
452 ms |
32296 KB |
Output is correct |
69 |
Correct |
460 ms |
32508 KB |
Output is correct |
70 |
Correct |
413 ms |
30512 KB |
Output is correct |
71 |
Correct |
367 ms |
29844 KB |
Output is correct |
72 |
Correct |
375 ms |
30388 KB |
Output is correct |
73 |
Correct |
373 ms |
30288 KB |
Output is correct |
74 |
Correct |
371 ms |
30688 KB |
Output is correct |
75 |
Correct |
401 ms |
30948 KB |
Output is correct |
76 |
Correct |
19 ms |
8524 KB |
Output is correct |
77 |
Correct |
422 ms |
41748 KB |
Output is correct |
78 |
Correct |
349 ms |
29188 KB |
Output is correct |
79 |
Correct |
2 ms |
4252 KB |
Output is correct |
80 |
Correct |
2 ms |
4180 KB |
Output is correct |