#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<limits.h>
#include<math.h>
#include<map>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<iomanip>
#include<cstring>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
const int TREE_SIZE=1<<18;
int n;
int l[200001];
int r[200001];
int tree[2*TREE_SIZE];
vector<int>init;
void update(int i,int val){
i+=TREE_SIZE;
tree[i]=val;
i/=2;
while(i){
tree[i]=tree[2*i]+tree[2*i+1];
i/=2;
}
}
pair<int,int>get(int node,int rl,int rr,int sum){
if(tree[node]<=sum)
return{rr,tree[node]};
if(rl==rr-1)
return{rl,0};
int mid=(rl+rr)/2;
if(sum<tree[2*node])
return get(2*node,rl,mid,sum);
else{
auto res=get(2*node+1,mid,rr,sum-tree[2*node]);
res.second+=tree[2*node];
return res;
}
}
void insert(int a,int b){
l[init[a]]=a;
r[init[a]]=b;
update(init[a],b-a);
}
pair<int,int>get(int sum){
auto res=get(1,0,TREE_SIZE,sum);
return{res.first,sum-res.second};
}
void solve(){
int q;
cin>>n>>q;
init.resize(n);
vector<int>nxt(n);
for(int&i:init)
cin>>i;
stack<pair<int,int>>st;
st.push({n+1,n});
for(int i=n-1;i>=0;i--){
while(st.top().first<init[i])
st.pop();
nxt[i]=st.top().second;
st.push({init[i],i});
}
vector<vector<pair<int,int>>>qrs(n+1);
vector<int>ans(q,-1);
for(int i=0;i<q;i++){
int t,qi;
cin>>t>>qi;
t=min(t,n);
qrs[t].push_back({qi-1,i});
}
for(int curr=0;curr<n;){
insert(curr,nxt[curr]);
curr=nxt[curr];
}
for(int round=0;round<=n;round++){
for(auto i:qrs[round]){
auto res=get(i.first);
ans[i.second]=init[l[res.first]+res.second];
}
auto res=get(n/2);
if(res.second==0)
continue;
int nl=l[res.first]+res.second;
int nr=r[res.first];
insert(l[res.first],nl);
for(int curr=nl;curr<nr;){
int nx=min(nxt[curr],nr);
insert(curr,nx);
curr=nx;
}
}
for(int i:ans){
if(i==-1)
abort();
cout<<i<<"\n";
}
}
int main(){
ios::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);
//freopen("input.in","r",stdin);freopen("output.out","w",stdout);
int t=1;//cin>>t;
while(t--)solve();
return 0;
}
/*
6 3
1 5 6 2 3 4
1 2
0 4
1 5
2
2
5
6 6
2 1 5 4 6 3
0 1
1 1
0 3
1 3
0 6
10 6
2
2
5
4
3
3
10 10
7 5 2 9 10 8 4 3 6 1
3 1
3 2
3 3
3 4
3 5
3 6
3 7
3 8
3 9
3 10
2
3
6
1
7
5
8
4
9
10
10 10
5 4 3 10 9 8 7 6 2 1
4 1
4 2
4 3
4 4
4 5
4 6
4 7
4 8
4 9
4 10
1
2
5
4
3
6
8
7
10
9
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
230 ms |
25180 KB |
Output is correct |
2 |
Correct |
226 ms |
23628 KB |
Output is correct |
3 |
Correct |
211 ms |
23120 KB |
Output is correct |
4 |
Correct |
191 ms |
21408 KB |
Output is correct |
5 |
Correct |
199 ms |
24912 KB |
Output is correct |
6 |
Correct |
181 ms |
25696 KB |
Output is correct |
7 |
Correct |
203 ms |
26588 KB |
Output is correct |
8 |
Correct |
186 ms |
24104 KB |
Output is correct |
9 |
Correct |
185 ms |
23400 KB |
Output is correct |
10 |
Correct |
191 ms |
22644 KB |
Output is correct |
11 |
Correct |
183 ms |
22988 KB |
Output is correct |
12 |
Correct |
183 ms |
20972 KB |
Output is correct |
13 |
Correct |
193 ms |
22132 KB |
Output is correct |
14 |
Correct |
193 ms |
24760 KB |
Output is correct |
15 |
Correct |
196 ms |
22900 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
218 ms |
21420 KB |
Output is correct |
18 |
Correct |
173 ms |
20912 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
291 ms |
36596 KB |
Output is correct |
2 |
Correct |
293 ms |
43692 KB |
Output is correct |
3 |
Correct |
222 ms |
37164 KB |
Output is correct |
4 |
Correct |
218 ms |
35840 KB |
Output is correct |
5 |
Correct |
219 ms |
37304 KB |
Output is correct |
6 |
Correct |
217 ms |
35252 KB |
Output is correct |
7 |
Correct |
251 ms |
42012 KB |
Output is correct |
8 |
Correct |
239 ms |
40120 KB |
Output is correct |
9 |
Correct |
215 ms |
37336 KB |
Output is correct |
10 |
Correct |
252 ms |
39484 KB |
Output is correct |
11 |
Correct |
234 ms |
35896 KB |
Output is correct |
12 |
Correct |
202 ms |
35184 KB |
Output is correct |
13 |
Correct |
237 ms |
38528 KB |
Output is correct |
14 |
Correct |
212 ms |
36660 KB |
Output is correct |
15 |
Correct |
244 ms |
40164 KB |
Output is correct |
16 |
Correct |
27 ms |
11344 KB |
Output is correct |
17 |
Correct |
253 ms |
38312 KB |
Output is correct |
18 |
Correct |
176 ms |
32184 KB |
Output is correct |
19 |
Correct |
69 ms |
16604 KB |
Output is correct |
20 |
Correct |
74 ms |
17800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
10596 KB |
Output is correct |
2 |
Correct |
54 ms |
11088 KB |
Output is correct |
3 |
Correct |
67 ms |
10904 KB |
Output is correct |
4 |
Correct |
41 ms |
10020 KB |
Output is correct |
5 |
Correct |
49 ms |
10580 KB |
Output is correct |
6 |
Correct |
44 ms |
10064 KB |
Output is correct |
7 |
Correct |
46 ms |
10588 KB |
Output is correct |
8 |
Correct |
43 ms |
10064 KB |
Output is correct |
9 |
Correct |
44 ms |
10320 KB |
Output is correct |
10 |
Correct |
41 ms |
10068 KB |
Output is correct |
11 |
Correct |
37 ms |
10068 KB |
Output is correct |
12 |
Correct |
39 ms |
9808 KB |
Output is correct |
13 |
Correct |
40 ms |
9808 KB |
Output is correct |
14 |
Correct |
37 ms |
10064 KB |
Output is correct |
15 |
Correct |
37 ms |
9808 KB |
Output is correct |
16 |
Correct |
14 ms |
6744 KB |
Output is correct |
17 |
Correct |
46 ms |
9932 KB |
Output is correct |
18 |
Correct |
31 ms |
8656 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
230 ms |
25180 KB |
Output is correct |
2 |
Correct |
226 ms |
23628 KB |
Output is correct |
3 |
Correct |
211 ms |
23120 KB |
Output is correct |
4 |
Correct |
191 ms |
21408 KB |
Output is correct |
5 |
Correct |
199 ms |
24912 KB |
Output is correct |
6 |
Correct |
181 ms |
25696 KB |
Output is correct |
7 |
Correct |
203 ms |
26588 KB |
Output is correct |
8 |
Correct |
186 ms |
24104 KB |
Output is correct |
9 |
Correct |
185 ms |
23400 KB |
Output is correct |
10 |
Correct |
191 ms |
22644 KB |
Output is correct |
11 |
Correct |
183 ms |
22988 KB |
Output is correct |
12 |
Correct |
183 ms |
20972 KB |
Output is correct |
13 |
Correct |
193 ms |
22132 KB |
Output is correct |
14 |
Correct |
193 ms |
24760 KB |
Output is correct |
15 |
Correct |
196 ms |
22900 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
218 ms |
21420 KB |
Output is correct |
18 |
Correct |
173 ms |
20912 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2392 KB |
Output is correct |
21 |
Correct |
291 ms |
36596 KB |
Output is correct |
22 |
Correct |
293 ms |
43692 KB |
Output is correct |
23 |
Correct |
222 ms |
37164 KB |
Output is correct |
24 |
Correct |
218 ms |
35840 KB |
Output is correct |
25 |
Correct |
219 ms |
37304 KB |
Output is correct |
26 |
Correct |
217 ms |
35252 KB |
Output is correct |
27 |
Correct |
251 ms |
42012 KB |
Output is correct |
28 |
Correct |
239 ms |
40120 KB |
Output is correct |
29 |
Correct |
215 ms |
37336 KB |
Output is correct |
30 |
Correct |
252 ms |
39484 KB |
Output is correct |
31 |
Correct |
234 ms |
35896 KB |
Output is correct |
32 |
Correct |
202 ms |
35184 KB |
Output is correct |
33 |
Correct |
237 ms |
38528 KB |
Output is correct |
34 |
Correct |
212 ms |
36660 KB |
Output is correct |
35 |
Correct |
244 ms |
40164 KB |
Output is correct |
36 |
Correct |
27 ms |
11344 KB |
Output is correct |
37 |
Correct |
253 ms |
38312 KB |
Output is correct |
38 |
Correct |
176 ms |
32184 KB |
Output is correct |
39 |
Correct |
69 ms |
16604 KB |
Output is correct |
40 |
Correct |
74 ms |
17800 KB |
Output is correct |
41 |
Correct |
55 ms |
10596 KB |
Output is correct |
42 |
Correct |
54 ms |
11088 KB |
Output is correct |
43 |
Correct |
67 ms |
10904 KB |
Output is correct |
44 |
Correct |
41 ms |
10020 KB |
Output is correct |
45 |
Correct |
49 ms |
10580 KB |
Output is correct |
46 |
Correct |
44 ms |
10064 KB |
Output is correct |
47 |
Correct |
46 ms |
10588 KB |
Output is correct |
48 |
Correct |
43 ms |
10064 KB |
Output is correct |
49 |
Correct |
44 ms |
10320 KB |
Output is correct |
50 |
Correct |
41 ms |
10068 KB |
Output is correct |
51 |
Correct |
37 ms |
10068 KB |
Output is correct |
52 |
Correct |
39 ms |
9808 KB |
Output is correct |
53 |
Correct |
40 ms |
9808 KB |
Output is correct |
54 |
Correct |
37 ms |
10064 KB |
Output is correct |
55 |
Correct |
37 ms |
9808 KB |
Output is correct |
56 |
Correct |
14 ms |
6744 KB |
Output is correct |
57 |
Correct |
46 ms |
9932 KB |
Output is correct |
58 |
Correct |
31 ms |
8656 KB |
Output is correct |
59 |
Correct |
1 ms |
2392 KB |
Output is correct |
60 |
Correct |
1 ms |
2396 KB |
Output is correct |
61 |
Correct |
507 ms |
49488 KB |
Output is correct |
62 |
Correct |
422 ms |
48768 KB |
Output is correct |
63 |
Correct |
444 ms |
46952 KB |
Output is correct |
64 |
Correct |
337 ms |
45840 KB |
Output is correct |
65 |
Correct |
380 ms |
47792 KB |
Output is correct |
66 |
Correct |
375 ms |
45648 KB |
Output is correct |
67 |
Correct |
313 ms |
45452 KB |
Output is correct |
68 |
Correct |
325 ms |
43600 KB |
Output is correct |
69 |
Correct |
329 ms |
45888 KB |
Output is correct |
70 |
Correct |
292 ms |
42008 KB |
Output is correct |
71 |
Correct |
272 ms |
43852 KB |
Output is correct |
72 |
Correct |
289 ms |
42352 KB |
Output is correct |
73 |
Correct |
304 ms |
42576 KB |
Output is correct |
74 |
Correct |
286 ms |
45032 KB |
Output is correct |
75 |
Correct |
310 ms |
43600 KB |
Output is correct |
76 |
Correct |
27 ms |
11344 KB |
Output is correct |
77 |
Correct |
321 ms |
38328 KB |
Output is correct |
78 |
Correct |
234 ms |
35352 KB |
Output is correct |
79 |
Correct |
1 ms |
2392 KB |
Output is correct |
80 |
Correct |
1 ms |
2392 KB |
Output is correct |