#include<bits/stdc++.h>
using namespace std;
#define ll long long
struct qry{
ll t,i,qid;
bool operator<(const qry& qr)const{
if(t!=qr.t)return t<qr.t;
if(i!=qr.i)return i<qr.i;
return qid<qr.qid;
}
};
struct SEG{
vector<pair<ll,ll> >seg;
vector<ll>cnt;
ll _n;
void init(ll n){
_n=n;
seg.resize(4*n+5);
cnt.resize(4*n+5,0);
}
void upd(ll ul, ll l, ll r, pair<ll,ll>val, ll id){
if(l==r){
seg[id]=val;
cnt[id]=val.second-val.first+1;
return;
}
ll mid=(l+r)>>1;
if(ul<=mid) upd(ul,l,mid,val,id*2);
else upd(ul,mid+1,r,val,id*2+1);
cnt[id]=cnt[id*2]+cnt[id*2+1];
}
ll bs(ll l, ll r, ll k, ll id){
if(l==r) return l;
ll mid=(l+r)>>1;
if(cnt[id*2]>=k) return bs(l,mid,k,id*2);
else return bs(mid+1,r,k-cnt[id*2],id*2+1);
}
pair<ll,ll> pairfinding(ll ul, ll l, ll r, ll id){
if(l==r){
return seg[id];
}
else{
ll mid=(l+r)>>1;
if(ul<=mid){
return pairfinding(ul,l,mid,id*2);
}
else return pairfinding(ul,mid+1,r,id*2+1);
}
}
ll sum(ll ql, ll qr, ll l, ll r, ll id){
if(l>qr||r<ql)return 0;
if(l>=ql&&r<=qr)return cnt[id];
ll mid=(l+r)>>1;
return sum(ql,qr,l,mid,id*2)+sum(ql,qr,mid+1,r,id*2+1);
}
ll find(){
return bs(1,_n,_n/2+1,1);
}
}st;
ll n;
ll nxt[200005];
ll a[200005];
void start_shuffle(){
ll cur=st.find();
ll tot=st.sum(1,cur,1,n,1);
pair<ll,ll>rng=st.pairfinding(cur,1,n,1);
ll rngsum=rng.second-rng.first+1;
if(tot-rngsum==n/2) return;
tot-=rngsum;
ll diff=n/2-tot;
pair<ll,ll>bruh={rng.first,rng.first+diff-1};
st.upd(cur,1,n,bruh,1);
ll curid=bruh.second+1;
while(1){
if(nxt[curid]>rng.second){
st.upd(a[curid],1,n,{curid,rng.second},1);break;
}
else{
st.upd(a[curid],1,n,{curid,nxt[curid]-1},1);
curid=nxt[curid];
}
}
}
ll find_id(ll idd){
ll cur=st.bs(1,n,idd,1);
pair<ll,ll>rng=st.pairfinding(cur,1,n,1);
ll tot=st.sum(1,cur,1,n,1);
ll rngsum=rng.second-rng.first+1;
tot-=rngsum;
return a[rng.first+idd-tot-1];
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll q;
cin>>n>>q;
st.init(n);
for(int i=1;i<=n;i++)cin>>a[i];
vector<qry>qrs;
int cnt=1;
while(q--){
ll t,i;
cin>>t>>i;
t=min(t,n);
qrs.push_back({t,i,cnt});
cnt++;
}
sort(qrs.begin(),qrs.end());
ll shuffles=0;
ll ans[qrs.size()+5];
ll newarr[n+5];
newarr[0]=0;
for(int i=1;i<=n;i++){
newarr[i]=max(newarr[i-1],a[i]);
}
ll ptr=1;
for(int i=1;i<=n;i++){
if(newarr[i]!=newarr[i-1]){
st.upd(newarr[i-1],1,n,{ptr,i-1},1);
ptr=i;
}
}
st.upd(newarr[n],1,n,{ptr,n},1);
stack<pair<ll,ll> >s;
for(int i=1;i<=n;i++){
while(s.size()){
if(a[i]>s.top().second){
nxt[s.top().first]=i; s.pop();
}
else break;
}
s.push({i,a[i]});
}
while(s.size()){
nxt[s.top().first]=300000;
s.pop();
}
for(int i=0;i<qrs.size();i++){
while(shuffles<qrs[i].t){
shuffles++;
start_shuffle();
}
ans[qrs[i].qid]=find_id(qrs[i].i);
}
for(int i=1;i<=cnt-1;i++){
cout<<ans[i]<<'\n';
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:141:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<qry>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
141 | for(int i=0;i<qrs.size();i++){
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
342 ms |
35716 KB |
Output is correct |
2 |
Correct |
311 ms |
35716 KB |
Output is correct |
3 |
Correct |
313 ms |
34440 KB |
Output is correct |
4 |
Correct |
287 ms |
34180 KB |
Output is correct |
5 |
Correct |
292 ms |
35716 KB |
Output is correct |
6 |
Correct |
291 ms |
34308 KB |
Output is correct |
7 |
Correct |
295 ms |
35656 KB |
Output is correct |
8 |
Correct |
304 ms |
34440 KB |
Output is correct |
9 |
Correct |
279 ms |
33960 KB |
Output is correct |
10 |
Correct |
327 ms |
34636 KB |
Output is correct |
11 |
Correct |
290 ms |
34460 KB |
Output is correct |
12 |
Correct |
296 ms |
34128 KB |
Output is correct |
13 |
Correct |
296 ms |
34948 KB |
Output is correct |
14 |
Correct |
297 ms |
35208 KB |
Output is correct |
15 |
Correct |
315 ms |
35728 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
309 ms |
34696 KB |
Output is correct |
18 |
Correct |
263 ms |
34280 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
375 ms |
63372 KB |
Output is correct |
2 |
Correct |
361 ms |
75864 KB |
Output is correct |
3 |
Correct |
299 ms |
64904 KB |
Output is correct |
4 |
Correct |
304 ms |
63628 KB |
Output is correct |
5 |
Correct |
309 ms |
65160 KB |
Output is correct |
6 |
Correct |
287 ms |
62740 KB |
Output is correct |
7 |
Correct |
330 ms |
74848 KB |
Output is correct |
8 |
Correct |
301 ms |
71584 KB |
Output is correct |
9 |
Correct |
290 ms |
64648 KB |
Output is correct |
10 |
Correct |
293 ms |
70540 KB |
Output is correct |
11 |
Correct |
267 ms |
62868 KB |
Output is correct |
12 |
Correct |
323 ms |
61836 KB |
Output is correct |
13 |
Correct |
389 ms |
70096 KB |
Output is correct |
14 |
Correct |
301 ms |
63880 KB |
Output is correct |
15 |
Correct |
356 ms |
72328 KB |
Output is correct |
16 |
Correct |
24 ms |
24920 KB |
Output is correct |
17 |
Correct |
360 ms |
68384 KB |
Output is correct |
18 |
Correct |
253 ms |
62808 KB |
Output is correct |
19 |
Correct |
79 ms |
33708 KB |
Output is correct |
20 |
Correct |
98 ms |
34484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
16572 KB |
Output is correct |
2 |
Correct |
77 ms |
17856 KB |
Output is correct |
3 |
Correct |
75 ms |
17784 KB |
Output is correct |
4 |
Correct |
55 ms |
17088 KB |
Output is correct |
5 |
Correct |
71 ms |
17712 KB |
Output is correct |
6 |
Correct |
55 ms |
16828 KB |
Output is correct |
7 |
Correct |
68 ms |
17600 KB |
Output is correct |
8 |
Correct |
59 ms |
16928 KB |
Output is correct |
9 |
Correct |
61 ms |
17400 KB |
Output is correct |
10 |
Correct |
52 ms |
16828 KB |
Output is correct |
11 |
Correct |
69 ms |
17256 KB |
Output is correct |
12 |
Correct |
56 ms |
16836 KB |
Output is correct |
13 |
Correct |
55 ms |
16936 KB |
Output is correct |
14 |
Correct |
53 ms |
17348 KB |
Output is correct |
15 |
Correct |
53 ms |
17052 KB |
Output is correct |
16 |
Correct |
12 ms |
12636 KB |
Output is correct |
17 |
Correct |
55 ms |
16724 KB |
Output is correct |
18 |
Correct |
41 ms |
18144 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
342 ms |
35716 KB |
Output is correct |
2 |
Correct |
311 ms |
35716 KB |
Output is correct |
3 |
Correct |
313 ms |
34440 KB |
Output is correct |
4 |
Correct |
287 ms |
34180 KB |
Output is correct |
5 |
Correct |
292 ms |
35716 KB |
Output is correct |
6 |
Correct |
291 ms |
34308 KB |
Output is correct |
7 |
Correct |
295 ms |
35656 KB |
Output is correct |
8 |
Correct |
304 ms |
34440 KB |
Output is correct |
9 |
Correct |
279 ms |
33960 KB |
Output is correct |
10 |
Correct |
327 ms |
34636 KB |
Output is correct |
11 |
Correct |
290 ms |
34460 KB |
Output is correct |
12 |
Correct |
296 ms |
34128 KB |
Output is correct |
13 |
Correct |
296 ms |
34948 KB |
Output is correct |
14 |
Correct |
297 ms |
35208 KB |
Output is correct |
15 |
Correct |
315 ms |
35728 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
309 ms |
34696 KB |
Output is correct |
18 |
Correct |
263 ms |
34280 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
600 KB |
Output is correct |
21 |
Correct |
375 ms |
63372 KB |
Output is correct |
22 |
Correct |
361 ms |
75864 KB |
Output is correct |
23 |
Correct |
299 ms |
64904 KB |
Output is correct |
24 |
Correct |
304 ms |
63628 KB |
Output is correct |
25 |
Correct |
309 ms |
65160 KB |
Output is correct |
26 |
Correct |
287 ms |
62740 KB |
Output is correct |
27 |
Correct |
330 ms |
74848 KB |
Output is correct |
28 |
Correct |
301 ms |
71584 KB |
Output is correct |
29 |
Correct |
290 ms |
64648 KB |
Output is correct |
30 |
Correct |
293 ms |
70540 KB |
Output is correct |
31 |
Correct |
267 ms |
62868 KB |
Output is correct |
32 |
Correct |
323 ms |
61836 KB |
Output is correct |
33 |
Correct |
389 ms |
70096 KB |
Output is correct |
34 |
Correct |
301 ms |
63880 KB |
Output is correct |
35 |
Correct |
356 ms |
72328 KB |
Output is correct |
36 |
Correct |
24 ms |
24920 KB |
Output is correct |
37 |
Correct |
360 ms |
68384 KB |
Output is correct |
38 |
Correct |
253 ms |
62808 KB |
Output is correct |
39 |
Correct |
79 ms |
33708 KB |
Output is correct |
40 |
Correct |
98 ms |
34484 KB |
Output is correct |
41 |
Correct |
75 ms |
16572 KB |
Output is correct |
42 |
Correct |
77 ms |
17856 KB |
Output is correct |
43 |
Correct |
75 ms |
17784 KB |
Output is correct |
44 |
Correct |
55 ms |
17088 KB |
Output is correct |
45 |
Correct |
71 ms |
17712 KB |
Output is correct |
46 |
Correct |
55 ms |
16828 KB |
Output is correct |
47 |
Correct |
68 ms |
17600 KB |
Output is correct |
48 |
Correct |
59 ms |
16928 KB |
Output is correct |
49 |
Correct |
61 ms |
17400 KB |
Output is correct |
50 |
Correct |
52 ms |
16828 KB |
Output is correct |
51 |
Correct |
69 ms |
17256 KB |
Output is correct |
52 |
Correct |
56 ms |
16836 KB |
Output is correct |
53 |
Correct |
55 ms |
16936 KB |
Output is correct |
54 |
Correct |
53 ms |
17348 KB |
Output is correct |
55 |
Correct |
53 ms |
17052 KB |
Output is correct |
56 |
Correct |
12 ms |
12636 KB |
Output is correct |
57 |
Correct |
55 ms |
16724 KB |
Output is correct |
58 |
Correct |
41 ms |
18144 KB |
Output is correct |
59 |
Correct |
0 ms |
348 KB |
Output is correct |
60 |
Correct |
0 ms |
348 KB |
Output is correct |
61 |
Correct |
598 ms |
77216 KB |
Output is correct |
62 |
Correct |
702 ms |
75960 KB |
Output is correct |
63 |
Correct |
589 ms |
73900 KB |
Output is correct |
64 |
Correct |
450 ms |
72224 KB |
Output is correct |
65 |
Correct |
488 ms |
74844 KB |
Output is correct |
66 |
Correct |
449 ms |
71720 KB |
Output is correct |
67 |
Correct |
444 ms |
74304 KB |
Output is correct |
68 |
Correct |
453 ms |
71580 KB |
Output is correct |
69 |
Correct |
525 ms |
72312 KB |
Output is correct |
70 |
Correct |
434 ms |
71264 KB |
Output is correct |
71 |
Correct |
406 ms |
70996 KB |
Output is correct |
72 |
Correct |
430 ms |
70660 KB |
Output is correct |
73 |
Correct |
445 ms |
70980 KB |
Output is correct |
74 |
Correct |
451 ms |
72744 KB |
Output is correct |
75 |
Correct |
464 ms |
72840 KB |
Output is correct |
76 |
Correct |
24 ms |
24912 KB |
Output is correct |
77 |
Correct |
409 ms |
68752 KB |
Output is correct |
78 |
Correct |
325 ms |
71668 KB |
Output is correct |
79 |
Correct |
0 ms |
464 KB |
Output is correct |
80 |
Correct |
0 ms |
348 KB |
Output is correct |