#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+5;
#define int long long
vector<int> ar, pos;
struct segtree{
int n, type, tl, tr, val, pos, el;
vector<int> st, a;
int base=0;
int merge(int x, int y){
if(type) return max(x, y);
return x + y;
}
segtree(int _n){
n = _n; type = 0;
st.resize(4*n);
}
segtree(int _n, vector<int> _a){
n = _n; a = _a; type = 1;
st.resize(4*n);
build(1, 0, n-1);
}
void build(int v, int l, int r){
if(l==r) st[v] = a[l];
else{
int mid = (l+r) / 2;
build(v*2, l, mid);
build(v*2+1, mid+1, r);
st[v] = merge(st[v*2], st[v*2+1]);
}
}
void upd(int v, int l, int r){
if(l==r) st[v] = val;
else{
int mid = (l+r) /2;
if(pos <= mid) upd(v*2, l, mid);
else upd(v*2+1, mid+1, r);
st[v] = merge(st[v*2], st[v*2+1]);
}
}
void update(int _pos, int va){
pos = _pos; val=va;
upd(1, 0, n-1);
}
array<int, 2> get1(int v, int l, int r){
if(l==r){
return {val-el-1, l};
}
else{
int mid = (l+r)/2;
if(el+st[v*2] >=val&&st[v*2]>0) return get1(v*2, l, mid);
el+=st[v*2];
return get1(v*2+1, mid+1, r);
}
}
array<int, 2> get(int h){
val = h, el = 0;
return get1(1, 0, n-1);
}
int get0(int v, int l, int r){
if(l>tr || r < tl) return 0;
else if(l>= tl && r<= tr) return st[v];
else{
int mid = (l+r) /2;
return merge(get0(v*2, l, mid), get0(v*2+1, mid+1, r));
}
}
int get(int l, int r){
tl = l; tr = r;
return get0(1, 0, n-1);
}
};
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, m; cin >> n >> m;
vector<int> a(n);
for (auto &i: a) cin >> i, i--;
segtree st_sizes(n);
segtree st_max(n, a);
vector<array<int, 2>> val(n, {-1, -1});
int l=0;
for(int i=1; i<n; i++){
if(a[l]<a[i]){
val[a[l]]={l, i-1};
l=i;
}
}
ar = a;
pos.resize(n);
for(int i=0; i<n; i++) pos[a[i]]=i;
val[a[l]] = {l, n-1};
for(int i=0; i<n; i++){
if(val[i][0]!=-1){
st_sizes.update(i, val[i][1]-val[i][0]+1);
}
}
vector<array<int, 2>> q(m);
vector<int> ans(m);
for(auto &[i, l]: q) cin >> i >> l, i=min(i, n);
vector<int> id(m);
iota(id.begin(), id.end(), 0);
sort(id.begin(), id.end(), [&](int a, int b){
return q[a][0] < q[b][0];
});
int it = 0;
for(auto i: id){
while(it < q[i][0]){
auto [value, g]=st_sizes.get(n/2);
int po = pos[g];
if(value==val[g][1]-val[g][0]+1){
it++;
continue;
}
int k =po+value+1;
int r = val[g][1];
while(k<=r){
int v = st_max.get(k, r);
val[v]={pos[v], r};
st_sizes.update(v, r-pos[v]+1);
r = pos[v]-1;
}
st_sizes.update(g, k-po);
val[g]={po, k-1};
it++;
}
auto [value, g]=st_sizes.get(q[i][1]);
ans[i]=a[pos[g]+value];
}
for(auto i: ans) cout<<i+1<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
317 ms |
43348 KB |
Output is correct |
2 |
Correct |
351 ms |
43224 KB |
Output is correct |
3 |
Correct |
325 ms |
41556 KB |
Output is correct |
4 |
Correct |
243 ms |
40272 KB |
Output is correct |
5 |
Correct |
281 ms |
42936 KB |
Output is correct |
6 |
Correct |
253 ms |
40532 KB |
Output is correct |
7 |
Correct |
297 ms |
43040 KB |
Output is correct |
8 |
Correct |
262 ms |
40780 KB |
Output is correct |
9 |
Correct |
255 ms |
40284 KB |
Output is correct |
10 |
Correct |
281 ms |
40952 KB |
Output is correct |
11 |
Correct |
258 ms |
40748 KB |
Output is correct |
12 |
Correct |
208 ms |
39568 KB |
Output is correct |
13 |
Correct |
245 ms |
40936 KB |
Output is correct |
14 |
Correct |
280 ms |
41592 KB |
Output is correct |
15 |
Correct |
267 ms |
41812 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
213 ms |
40016 KB |
Output is correct |
18 |
Correct |
182 ms |
39760 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
311 ms |
74536 KB |
Output is correct |
2 |
Correct |
283 ms |
73528 KB |
Output is correct |
3 |
Correct |
224 ms |
61740 KB |
Output is correct |
4 |
Correct |
220 ms |
62552 KB |
Output is correct |
5 |
Correct |
234 ms |
63356 KB |
Output is correct |
6 |
Correct |
220 ms |
61028 KB |
Output is correct |
7 |
Correct |
264 ms |
73020 KB |
Output is correct |
8 |
Correct |
259 ms |
70012 KB |
Output is correct |
9 |
Correct |
224 ms |
63108 KB |
Output is correct |
10 |
Correct |
235 ms |
68820 KB |
Output is correct |
11 |
Correct |
193 ms |
61180 KB |
Output is correct |
12 |
Correct |
200 ms |
60020 KB |
Output is correct |
13 |
Correct |
232 ms |
68476 KB |
Output is correct |
14 |
Correct |
200 ms |
62068 KB |
Output is correct |
15 |
Correct |
241 ms |
70744 KB |
Output is correct |
16 |
Correct |
20 ms |
23368 KB |
Output is correct |
17 |
Correct |
253 ms |
66964 KB |
Output is correct |
18 |
Correct |
184 ms |
58224 KB |
Output is correct |
19 |
Correct |
59 ms |
32232 KB |
Output is correct |
20 |
Correct |
85 ms |
33024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
69 ms |
16796 KB |
Output is correct |
2 |
Correct |
62 ms |
16700 KB |
Output is correct |
3 |
Correct |
65 ms |
16216 KB |
Output is correct |
4 |
Correct |
46 ms |
16280 KB |
Output is correct |
5 |
Correct |
58 ms |
16708 KB |
Output is correct |
6 |
Correct |
49 ms |
16080 KB |
Output is correct |
7 |
Correct |
63 ms |
16696 KB |
Output is correct |
8 |
Correct |
66 ms |
15972 KB |
Output is correct |
9 |
Correct |
56 ms |
16452 KB |
Output is correct |
10 |
Correct |
43 ms |
15720 KB |
Output is correct |
11 |
Correct |
48 ms |
16200 KB |
Output is correct |
12 |
Correct |
43 ms |
15972 KB |
Output is correct |
13 |
Correct |
44 ms |
15932 KB |
Output is correct |
14 |
Correct |
49 ms |
16460 KB |
Output is correct |
15 |
Correct |
43 ms |
16248 KB |
Output is correct |
16 |
Correct |
11 ms |
11860 KB |
Output is correct |
17 |
Correct |
58 ms |
15804 KB |
Output is correct |
18 |
Correct |
35 ms |
15708 KB |
Output is correct |
19 |
Correct |
0 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
317 ms |
43348 KB |
Output is correct |
2 |
Correct |
351 ms |
43224 KB |
Output is correct |
3 |
Correct |
325 ms |
41556 KB |
Output is correct |
4 |
Correct |
243 ms |
40272 KB |
Output is correct |
5 |
Correct |
281 ms |
42936 KB |
Output is correct |
6 |
Correct |
253 ms |
40532 KB |
Output is correct |
7 |
Correct |
297 ms |
43040 KB |
Output is correct |
8 |
Correct |
262 ms |
40780 KB |
Output is correct |
9 |
Correct |
255 ms |
40284 KB |
Output is correct |
10 |
Correct |
281 ms |
40952 KB |
Output is correct |
11 |
Correct |
258 ms |
40748 KB |
Output is correct |
12 |
Correct |
208 ms |
39568 KB |
Output is correct |
13 |
Correct |
245 ms |
40936 KB |
Output is correct |
14 |
Correct |
280 ms |
41592 KB |
Output is correct |
15 |
Correct |
267 ms |
41812 KB |
Output is correct |
16 |
Correct |
0 ms |
344 KB |
Output is correct |
17 |
Correct |
213 ms |
40016 KB |
Output is correct |
18 |
Correct |
182 ms |
39760 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
596 KB |
Output is correct |
21 |
Correct |
311 ms |
74536 KB |
Output is correct |
22 |
Correct |
283 ms |
73528 KB |
Output is correct |
23 |
Correct |
224 ms |
61740 KB |
Output is correct |
24 |
Correct |
220 ms |
62552 KB |
Output is correct |
25 |
Correct |
234 ms |
63356 KB |
Output is correct |
26 |
Correct |
220 ms |
61028 KB |
Output is correct |
27 |
Correct |
264 ms |
73020 KB |
Output is correct |
28 |
Correct |
259 ms |
70012 KB |
Output is correct |
29 |
Correct |
224 ms |
63108 KB |
Output is correct |
30 |
Correct |
235 ms |
68820 KB |
Output is correct |
31 |
Correct |
193 ms |
61180 KB |
Output is correct |
32 |
Correct |
200 ms |
60020 KB |
Output is correct |
33 |
Correct |
232 ms |
68476 KB |
Output is correct |
34 |
Correct |
200 ms |
62068 KB |
Output is correct |
35 |
Correct |
241 ms |
70744 KB |
Output is correct |
36 |
Correct |
20 ms |
23368 KB |
Output is correct |
37 |
Correct |
253 ms |
66964 KB |
Output is correct |
38 |
Correct |
184 ms |
58224 KB |
Output is correct |
39 |
Correct |
59 ms |
32232 KB |
Output is correct |
40 |
Correct |
85 ms |
33024 KB |
Output is correct |
41 |
Correct |
69 ms |
16796 KB |
Output is correct |
42 |
Correct |
62 ms |
16700 KB |
Output is correct |
43 |
Correct |
65 ms |
16216 KB |
Output is correct |
44 |
Correct |
46 ms |
16280 KB |
Output is correct |
45 |
Correct |
58 ms |
16708 KB |
Output is correct |
46 |
Correct |
49 ms |
16080 KB |
Output is correct |
47 |
Correct |
63 ms |
16696 KB |
Output is correct |
48 |
Correct |
66 ms |
15972 KB |
Output is correct |
49 |
Correct |
56 ms |
16452 KB |
Output is correct |
50 |
Correct |
43 ms |
15720 KB |
Output is correct |
51 |
Correct |
48 ms |
16200 KB |
Output is correct |
52 |
Correct |
43 ms |
15972 KB |
Output is correct |
53 |
Correct |
44 ms |
15932 KB |
Output is correct |
54 |
Correct |
49 ms |
16460 KB |
Output is correct |
55 |
Correct |
43 ms |
16248 KB |
Output is correct |
56 |
Correct |
11 ms |
11860 KB |
Output is correct |
57 |
Correct |
58 ms |
15804 KB |
Output is correct |
58 |
Correct |
35 ms |
15708 KB |
Output is correct |
59 |
Correct |
0 ms |
344 KB |
Output is correct |
60 |
Correct |
0 ms |
460 KB |
Output is correct |
61 |
Correct |
584 ms |
73892 KB |
Output is correct |
62 |
Correct |
580 ms |
73416 KB |
Output is correct |
63 |
Correct |
538 ms |
70772 KB |
Output is correct |
64 |
Correct |
474 ms |
70488 KB |
Output is correct |
65 |
Correct |
481 ms |
73020 KB |
Output is correct |
66 |
Correct |
474 ms |
70028 KB |
Output is correct |
67 |
Correct |
463 ms |
72732 KB |
Output is correct |
68 |
Correct |
456 ms |
69808 KB |
Output is correct |
69 |
Correct |
475 ms |
70612 KB |
Output is correct |
70 |
Correct |
426 ms |
69504 KB |
Output is correct |
71 |
Correct |
409 ms |
69428 KB |
Output is correct |
72 |
Correct |
467 ms |
69268 KB |
Output is correct |
73 |
Correct |
431 ms |
69380 KB |
Output is correct |
74 |
Correct |
448 ms |
71264 KB |
Output is correct |
75 |
Correct |
448 ms |
71208 KB |
Output is correct |
76 |
Correct |
21 ms |
23360 KB |
Output is correct |
77 |
Correct |
352 ms |
67188 KB |
Output is correct |
78 |
Correct |
262 ms |
66920 KB |
Output is correct |
79 |
Correct |
0 ms |
348 KB |
Output is correct |
80 |
Correct |
0 ms |
348 KB |
Output is correct |