#include<bits/stdc++.h>
using namespace std;
#define pb push_back
using pii=pair<int,int>;
struct query{
int t,i,ind;
};
struct Node{
int val,maxi,sz,pri;
Node*l,*r;
Node(int val):val(val),maxi(val),sz(1),pri(rand()),l(0),r(0){}
};
using pnode=Node*;
int getcnt(pnode p){
if(p)return p->sz;
return 0;
}
int getval(pnode p){
if(p)return p->maxi;
return 0;
}
void upd(pnode p){
if(p){
p->sz=getcnt(p->l)+getcnt(p->r)+1;
p->maxi=p->val;
if(p->l)p->maxi=max(p->maxi,p->l->maxi);
if(p->r)p->maxi=max(p->maxi,p->r->maxi);
}
}
void merge(pnode&p,pnode l,pnode r){
if(!l){
p=r;
return;
}
if(!r){
p=l;
return;
}
if(l->pri<r->pri){
merge(r->l,l,r->l);
p=r;
}else{
merge(l->r,l->r,r);
p=l;
}
upd(p);
}
void splitind(pnode p,pnode&l,pnode&r,int ind){
if(!p){
l=r=0;
return;
}
if(getcnt(p->l)+1<ind){
splitind(p->r,p->r,r,ind-getcnt(p->l)-1);
l=p;
}else{
splitind(p->l,l,p->l,ind);
r=p;
}
upd(p);
}
void split(pnode p,pnode&l,pnode&r,int val){
if(!p){
l=r=0;
return;
}
if(max(p->val,getval(p->l))<=val){
split(p->r,p->r,r,val);
l=p;
}else{
split(p->l,l,p->l,val);
r=p;
}
upd(p);
}
int find(pnode p,int ind){
if(ind==getcnt(p->l)+1)return p->val;
if(ind<getcnt(p->l)+1)return find(p->l,ind);
return find(p->r,ind-getcnt(p->l)-1);
}
int beg(pnode p){
if(p->l)return beg(p->l);
return p->val;
}
pnode last(pnode p){
if(p->r)return last(p->r);
return p;
}
void walk(pnode p){
if(!p)return;
walk(p->l);
//cerr<<p->val<<' ';
walk(p->r);
}
int main(){
#ifdef Local
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
int n,m;
cin>>n>>m;
int a[n];
pnode t1=0,t2=0;
for(int i=0;i<n;i++){
cin>>a[i];
if(i<n/2)merge(t1,t1,new Node(a[i]));
else merge(t2,t2,new Node(a[i]));
}
query q[m];
for(int i=0;i<m;i++){
cin>>q[i].t>>q[i].i;
q[i].ind=i;
}
sort(q,q+m,[&](query x,query y)->bool {
return x.t<y.t;
});
bool done=0;
int turn=0;
int ans[m];
for(int i=0;i<m;i++){
while(turn<q[i].t&&!done){
done=1;
turn++;
bool cont;
//cerr<<"begin\n";
do{
cont=0;
//cerr<<"begin again\n";
int val=beg(t2);
//cerr<<"found\n";
pnode p1=0,p2=0;
split(t1,p1,p2,val);
if(p2!=0){
cont=1;
done=0;
pnode p3=0;
//cerr<<"sp beg\n";
split(t2,p3,t2,val);
merge(t1,p1,p3);
merge(t1,t1,p2);
//cerr<<"sp end\n";
}
//cerr<<"here\n";
}while(cont&&getcnt(t2));
//cerr<<"end\n";
while(n/2<getcnt(t1)){
pnode p=0;
splitind(t1,t1,p,n/2+1);
merge(t2,p,t2);
}
//cerr<<"check\n";
}
//walk(t1);cerr<<'\n';walk(t2);
//cerr<<'\n';
if(q[i].i<=n/2){
ans[q[i].ind]=find(t1,q[i].i);
}else{
//cerr<<q[i].i-n/2<<" huh\n";
ans[q[i].ind]=find(t2,q[i].i-n/2);
}
//cerr<<"ok\n";
//cerr<<ans[q[i].ind]<<'\n';
}
for(int i:ans)cout<<i<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
430 ms |
27720 KB |
Output is correct |
2 |
Correct |
396 ms |
27564 KB |
Output is correct |
3 |
Correct |
389 ms |
26440 KB |
Output is correct |
4 |
Correct |
349 ms |
25160 KB |
Output is correct |
5 |
Correct |
380 ms |
26952 KB |
Output is correct |
6 |
Correct |
352 ms |
25416 KB |
Output is correct |
7 |
Correct |
401 ms |
27208 KB |
Output is correct |
8 |
Correct |
377 ms |
25672 KB |
Output is correct |
9 |
Correct |
377 ms |
25416 KB |
Output is correct |
10 |
Correct |
352 ms |
25780 KB |
Output is correct |
11 |
Correct |
380 ms |
25672 KB |
Output is correct |
12 |
Correct |
331 ms |
24648 KB |
Output is correct |
13 |
Correct |
350 ms |
25416 KB |
Output is correct |
14 |
Correct |
386 ms |
26160 KB |
Output is correct |
15 |
Correct |
381 ms |
26184 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
332 ms |
24988 KB |
Output is correct |
18 |
Correct |
333 ms |
24776 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
670 ms |
47028 KB |
Output is correct |
2 |
Correct |
564 ms |
46316 KB |
Output is correct |
3 |
Correct |
465 ms |
38040 KB |
Output is correct |
4 |
Correct |
455 ms |
38196 KB |
Output is correct |
5 |
Correct |
482 ms |
39240 KB |
Output is correct |
6 |
Correct |
464 ms |
37664 KB |
Output is correct |
7 |
Correct |
589 ms |
45764 KB |
Output is correct |
8 |
Correct |
536 ms |
43848 KB |
Output is correct |
9 |
Correct |
485 ms |
38812 KB |
Output is correct |
10 |
Correct |
543 ms |
42824 KB |
Output is correct |
11 |
Correct |
394 ms |
37192 KB |
Output is correct |
12 |
Correct |
412 ms |
36860 KB |
Output is correct |
13 |
Correct |
515 ms |
42148 KB |
Output is correct |
14 |
Correct |
459 ms |
38076 KB |
Output is correct |
15 |
Correct |
500 ms |
43848 KB |
Output is correct |
16 |
Correct |
61 ms |
11568 KB |
Output is correct |
17 |
Correct |
435 ms |
40432 KB |
Output is correct |
18 |
Correct |
353 ms |
34632 KB |
Output is correct |
19 |
Correct |
152 ms |
17388 KB |
Output is correct |
20 |
Correct |
189 ms |
18760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
187 ms |
9288 KB |
Output is correct |
2 |
Correct |
119 ms |
9232 KB |
Output is correct |
3 |
Correct |
147 ms |
9032 KB |
Output is correct |
4 |
Correct |
129 ms |
9032 KB |
Output is correct |
5 |
Correct |
142 ms |
9288 KB |
Output is correct |
6 |
Correct |
125 ms |
8776 KB |
Output is correct |
7 |
Correct |
126 ms |
9128 KB |
Output is correct |
8 |
Correct |
128 ms |
8940 KB |
Output is correct |
9 |
Correct |
136 ms |
9032 KB |
Output is correct |
10 |
Correct |
96 ms |
8716 KB |
Output is correct |
11 |
Correct |
93 ms |
8864 KB |
Output is correct |
12 |
Correct |
92 ms |
8644 KB |
Output is correct |
13 |
Correct |
94 ms |
8892 KB |
Output is correct |
14 |
Correct |
103 ms |
9032 KB |
Output is correct |
15 |
Correct |
91 ms |
8776 KB |
Output is correct |
16 |
Correct |
30 ms |
5804 KB |
Output is correct |
17 |
Correct |
84 ms |
8520 KB |
Output is correct |
18 |
Correct |
91 ms |
8492 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
430 ms |
27720 KB |
Output is correct |
2 |
Correct |
396 ms |
27564 KB |
Output is correct |
3 |
Correct |
389 ms |
26440 KB |
Output is correct |
4 |
Correct |
349 ms |
25160 KB |
Output is correct |
5 |
Correct |
380 ms |
26952 KB |
Output is correct |
6 |
Correct |
352 ms |
25416 KB |
Output is correct |
7 |
Correct |
401 ms |
27208 KB |
Output is correct |
8 |
Correct |
377 ms |
25672 KB |
Output is correct |
9 |
Correct |
377 ms |
25416 KB |
Output is correct |
10 |
Correct |
352 ms |
25780 KB |
Output is correct |
11 |
Correct |
380 ms |
25672 KB |
Output is correct |
12 |
Correct |
331 ms |
24648 KB |
Output is correct |
13 |
Correct |
350 ms |
25416 KB |
Output is correct |
14 |
Correct |
386 ms |
26160 KB |
Output is correct |
15 |
Correct |
381 ms |
26184 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
332 ms |
24988 KB |
Output is correct |
18 |
Correct |
333 ms |
24776 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
670 ms |
47028 KB |
Output is correct |
22 |
Correct |
564 ms |
46316 KB |
Output is correct |
23 |
Correct |
465 ms |
38040 KB |
Output is correct |
24 |
Correct |
455 ms |
38196 KB |
Output is correct |
25 |
Correct |
482 ms |
39240 KB |
Output is correct |
26 |
Correct |
464 ms |
37664 KB |
Output is correct |
27 |
Correct |
589 ms |
45764 KB |
Output is correct |
28 |
Correct |
536 ms |
43848 KB |
Output is correct |
29 |
Correct |
485 ms |
38812 KB |
Output is correct |
30 |
Correct |
543 ms |
42824 KB |
Output is correct |
31 |
Correct |
394 ms |
37192 KB |
Output is correct |
32 |
Correct |
412 ms |
36860 KB |
Output is correct |
33 |
Correct |
515 ms |
42148 KB |
Output is correct |
34 |
Correct |
459 ms |
38076 KB |
Output is correct |
35 |
Correct |
500 ms |
43848 KB |
Output is correct |
36 |
Correct |
61 ms |
11568 KB |
Output is correct |
37 |
Correct |
435 ms |
40432 KB |
Output is correct |
38 |
Correct |
353 ms |
34632 KB |
Output is correct |
39 |
Correct |
152 ms |
17388 KB |
Output is correct |
40 |
Correct |
189 ms |
18760 KB |
Output is correct |
41 |
Correct |
187 ms |
9288 KB |
Output is correct |
42 |
Correct |
119 ms |
9232 KB |
Output is correct |
43 |
Correct |
147 ms |
9032 KB |
Output is correct |
44 |
Correct |
129 ms |
9032 KB |
Output is correct |
45 |
Correct |
142 ms |
9288 KB |
Output is correct |
46 |
Correct |
125 ms |
8776 KB |
Output is correct |
47 |
Correct |
126 ms |
9128 KB |
Output is correct |
48 |
Correct |
128 ms |
8940 KB |
Output is correct |
49 |
Correct |
136 ms |
9032 KB |
Output is correct |
50 |
Correct |
96 ms |
8716 KB |
Output is correct |
51 |
Correct |
93 ms |
8864 KB |
Output is correct |
52 |
Correct |
92 ms |
8644 KB |
Output is correct |
53 |
Correct |
94 ms |
8892 KB |
Output is correct |
54 |
Correct |
103 ms |
9032 KB |
Output is correct |
55 |
Correct |
91 ms |
8776 KB |
Output is correct |
56 |
Correct |
30 ms |
5804 KB |
Output is correct |
57 |
Correct |
84 ms |
8520 KB |
Output is correct |
58 |
Correct |
91 ms |
8492 KB |
Output is correct |
59 |
Correct |
1 ms |
336 KB |
Output is correct |
60 |
Correct |
1 ms |
336 KB |
Output is correct |
61 |
Correct |
1034 ms |
46408 KB |
Output is correct |
62 |
Correct |
924 ms |
45912 KB |
Output is correct |
63 |
Correct |
944 ms |
44104 KB |
Output is correct |
64 |
Correct |
841 ms |
44092 KB |
Output is correct |
65 |
Correct |
1018 ms |
45688 KB |
Output is correct |
66 |
Correct |
891 ms |
43852 KB |
Output is correct |
67 |
Correct |
880 ms |
45384 KB |
Output is correct |
68 |
Correct |
885 ms |
43480 KB |
Output is correct |
69 |
Correct |
909 ms |
43848 KB |
Output is correct |
70 |
Correct |
805 ms |
43276 KB |
Output is correct |
71 |
Correct |
768 ms |
42732 KB |
Output is correct |
72 |
Correct |
831 ms |
42864 KB |
Output is correct |
73 |
Correct |
842 ms |
42824 KB |
Output is correct |
74 |
Correct |
848 ms |
44144 KB |
Output is correct |
75 |
Correct |
826 ms |
44228 KB |
Output is correct |
76 |
Correct |
68 ms |
11592 KB |
Output is correct |
77 |
Correct |
682 ms |
40612 KB |
Output is correct |
78 |
Correct |
673 ms |
40592 KB |
Output is correct |
79 |
Correct |
1 ms |
336 KB |
Output is correct |
80 |
Correct |
1 ms |
336 KB |
Output is correct |