#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define xx first
#define yy second
#define ff(i,s,f) for(int i=s;i<f;i++)
#define fb(i,s,f) for(int i=(s)-1;i>=f;i--)
#define ffi(i,s,f) for(int i=s;i<=f;i++)
#define fbi(i,s,f) for(int i=s;i>=f;i--)
#define srt(a) sort(a.begin(),a.end());
#define srtg(a,int) sort(a.begin(),a.end(),greater<int>())
#define lb(a,x) lower_bound(a.begin(),a.end(),x)
#define ub(a,x) upper_bound(a.begin(),a.end(),x)
#define fnd(a,x) find(a.begin(),a.end(),x)
#define ios ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef string str;
using ti = tuple<int,int,int>;
const int maxn=1e6+10;
int n,q;
int a[maxn];
int inv[maxn];
int veci[maxn];
int segt[4*maxn];
ti upit[maxn];
int res[maxn];
void update(int t,int v,int ind=1,int l=1,int r=n){
if(l==r){
segt[ind]=v;
return;
}
int mid=l+(r-l)/2;
if(t<=mid) update(t,v,2*ind,l,mid);
else update(t,v,2*ind+1,mid+1,r);
segt[ind]=segt[2*ind]+segt[2*ind+1];
}
int qry(int tl,int tr,int ind=1,int l=1,int r=n){
if(tl>tr) return 0;
if(tl<=l && r<=tr) return segt[ind];
int mid=l+(r-l)/2;
int ret=0;
if(tl<=mid) ret+=qry(tl,tr,2*ind,l,mid);
if(tr>mid) ret+=qry(tl,tr,2*ind+1,mid+1,r);
return ret;
}
int pronadji(int x,int ind=1,int l=1,int r=n){
if(l==r) return l;
int mid=l+(r-l)/2;
if(segt[2*ind]>=x) return pronadji(x,2*ind,l,mid);
else return pronadji(x-segt[2*ind],2*ind+1,mid+1,r);
}
bool pomeri(){
int lik=pronadji(n/2+1);
int kraj=inv[lik]+qry(lik,lik)-1;
int prosli=inv[lik]+n/2+1-qry(1,lik-1)-1;
if(qry(a[prosli],a[prosli])!=0) return true;
update(lik,n/2+1-qry(1,lik-1)-1);
int tren=veci[prosli];
while(tren<=kraj){
update(a[prosli],tren-prosli);
prosli=tren;
tren=veci[tren];
}
update(a[prosli],kraj+1-prosli);
return false;
}
void solve(){
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
inv[a[i]]=i;
}
for(int i=1;i<=q;i++){
int t,ind;
cin>>t>>ind;
upit[i]={t,ind,i};
}
sort(upit+1,upit+q+1);
stack<int> stek;
for(int i=n;i>0;i--){
while(!stek.empty() && a[stek.top()]<a[i]) stek.pop();
if(stek.empty()) veci[i]=n+1;
else veci[i]=stek.top();
stek.push(i);
}
int tren=1;
int prosli=-1;
while(tren<=n/2){
if(prosli!=-1){
update(a[prosli],tren-prosli);
}
prosli=tren;
tren=veci[tren];
}
update(a[prosli],n/2+1-prosli);
tren=n/2+1;
prosli=-1;
while(tren<=n){
if(prosli!=-1) update(a[prosli],tren-prosli);
prosli=tren;
tren=veci[tren];
}
update(a[prosli],n+1-prosli);
int tu=0;
while(get<0>(upit[tu])==0){
res[get<2>(upit[tu])]=a[get<1>(upit[tu])];
tu++;
}
for(int t=1;tu<=q;t++){
while(tu<=q && t>=get<0>(upit[tu])){
auto [t,x,ind]=upit[tu];
int gde=pronadji(x);
int klk=x-qry(1,gde-1)-1;
res[ind]=a[inv[gde]+klk];
tu++;
}
if(pomeri()) t=1e9+10;
}
for(int i=1;i<=q;i++){
cout<<res[i]<<"\n";
}
}
int main(){
ios;
int t=1;
//cin>>t;
while(t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
308 ms |
20004 KB |
Output is correct |
2 |
Correct |
323 ms |
27240 KB |
Output is correct |
3 |
Correct |
323 ms |
26280 KB |
Output is correct |
4 |
Correct |
296 ms |
25064 KB |
Output is correct |
5 |
Correct |
315 ms |
26992 KB |
Output is correct |
6 |
Correct |
292 ms |
25372 KB |
Output is correct |
7 |
Correct |
319 ms |
27096 KB |
Output is correct |
8 |
Correct |
309 ms |
25556 KB |
Output is correct |
9 |
Correct |
294 ms |
25180 KB |
Output is correct |
10 |
Correct |
306 ms |
25724 KB |
Output is correct |
11 |
Correct |
300 ms |
25660 KB |
Output is correct |
12 |
Correct |
280 ms |
24532 KB |
Output is correct |
13 |
Correct |
301 ms |
25452 KB |
Output is correct |
14 |
Correct |
304 ms |
26176 KB |
Output is correct |
15 |
Correct |
314 ms |
26252 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
235 ms |
24908 KB |
Output is correct |
18 |
Correct |
265 ms |
24660 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
417 ms |
26444 KB |
Output is correct |
2 |
Correct |
368 ms |
27428 KB |
Output is correct |
3 |
Correct |
377 ms |
22056 KB |
Output is correct |
4 |
Correct |
312 ms |
22220 KB |
Output is correct |
5 |
Correct |
353 ms |
22652 KB |
Output is correct |
6 |
Correct |
313 ms |
22328 KB |
Output is correct |
7 |
Correct |
371 ms |
27672 KB |
Output is correct |
8 |
Correct |
351 ms |
26716 KB |
Output is correct |
9 |
Correct |
328 ms |
23356 KB |
Output is correct |
10 |
Correct |
344 ms |
27132 KB |
Output is correct |
11 |
Correct |
300 ms |
22684 KB |
Output is correct |
12 |
Correct |
301 ms |
22940 KB |
Output is correct |
13 |
Correct |
331 ms |
27260 KB |
Output is correct |
14 |
Correct |
310 ms |
23592 KB |
Output is correct |
15 |
Correct |
333 ms |
27728 KB |
Output is correct |
16 |
Correct |
18 ms |
4812 KB |
Output is correct |
17 |
Correct |
234 ms |
27340 KB |
Output is correct |
18 |
Correct |
312 ms |
20684 KB |
Output is correct |
19 |
Correct |
54 ms |
8100 KB |
Output is correct |
20 |
Correct |
78 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
5540 KB |
Output is correct |
2 |
Correct |
67 ms |
6708 KB |
Output is correct |
3 |
Correct |
79 ms |
6400 KB |
Output is correct |
4 |
Correct |
47 ms |
6180 KB |
Output is correct |
5 |
Correct |
70 ms |
6344 KB |
Output is correct |
6 |
Correct |
59 ms |
6064 KB |
Output is correct |
7 |
Correct |
63 ms |
6296 KB |
Output is correct |
8 |
Correct |
70 ms |
6112 KB |
Output is correct |
9 |
Correct |
61 ms |
6200 KB |
Output is correct |
10 |
Correct |
45 ms |
5956 KB |
Output is correct |
11 |
Correct |
46 ms |
6044 KB |
Output is correct |
12 |
Correct |
43 ms |
5972 KB |
Output is correct |
13 |
Correct |
48 ms |
6076 KB |
Output is correct |
14 |
Correct |
49 ms |
6100 KB |
Output is correct |
15 |
Correct |
43 ms |
5964 KB |
Output is correct |
16 |
Correct |
14 ms |
3028 KB |
Output is correct |
17 |
Correct |
50 ms |
6284 KB |
Output is correct |
18 |
Correct |
36 ms |
4864 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
308 ms |
20004 KB |
Output is correct |
2 |
Correct |
323 ms |
27240 KB |
Output is correct |
3 |
Correct |
323 ms |
26280 KB |
Output is correct |
4 |
Correct |
296 ms |
25064 KB |
Output is correct |
5 |
Correct |
315 ms |
26992 KB |
Output is correct |
6 |
Correct |
292 ms |
25372 KB |
Output is correct |
7 |
Correct |
319 ms |
27096 KB |
Output is correct |
8 |
Correct |
309 ms |
25556 KB |
Output is correct |
9 |
Correct |
294 ms |
25180 KB |
Output is correct |
10 |
Correct |
306 ms |
25724 KB |
Output is correct |
11 |
Correct |
300 ms |
25660 KB |
Output is correct |
12 |
Correct |
280 ms |
24532 KB |
Output is correct |
13 |
Correct |
301 ms |
25452 KB |
Output is correct |
14 |
Correct |
304 ms |
26176 KB |
Output is correct |
15 |
Correct |
314 ms |
26252 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
235 ms |
24908 KB |
Output is correct |
18 |
Correct |
265 ms |
24660 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
336 KB |
Output is correct |
21 |
Correct |
417 ms |
26444 KB |
Output is correct |
22 |
Correct |
368 ms |
27428 KB |
Output is correct |
23 |
Correct |
377 ms |
22056 KB |
Output is correct |
24 |
Correct |
312 ms |
22220 KB |
Output is correct |
25 |
Correct |
353 ms |
22652 KB |
Output is correct |
26 |
Correct |
313 ms |
22328 KB |
Output is correct |
27 |
Correct |
371 ms |
27672 KB |
Output is correct |
28 |
Correct |
351 ms |
26716 KB |
Output is correct |
29 |
Correct |
328 ms |
23356 KB |
Output is correct |
30 |
Correct |
344 ms |
27132 KB |
Output is correct |
31 |
Correct |
300 ms |
22684 KB |
Output is correct |
32 |
Correct |
301 ms |
22940 KB |
Output is correct |
33 |
Correct |
331 ms |
27260 KB |
Output is correct |
34 |
Correct |
310 ms |
23592 KB |
Output is correct |
35 |
Correct |
333 ms |
27728 KB |
Output is correct |
36 |
Correct |
18 ms |
4812 KB |
Output is correct |
37 |
Correct |
234 ms |
27340 KB |
Output is correct |
38 |
Correct |
312 ms |
20684 KB |
Output is correct |
39 |
Correct |
54 ms |
8100 KB |
Output is correct |
40 |
Correct |
78 ms |
9728 KB |
Output is correct |
41 |
Correct |
91 ms |
5540 KB |
Output is correct |
42 |
Correct |
67 ms |
6708 KB |
Output is correct |
43 |
Correct |
79 ms |
6400 KB |
Output is correct |
44 |
Correct |
47 ms |
6180 KB |
Output is correct |
45 |
Correct |
70 ms |
6344 KB |
Output is correct |
46 |
Correct |
59 ms |
6064 KB |
Output is correct |
47 |
Correct |
63 ms |
6296 KB |
Output is correct |
48 |
Correct |
70 ms |
6112 KB |
Output is correct |
49 |
Correct |
61 ms |
6200 KB |
Output is correct |
50 |
Correct |
45 ms |
5956 KB |
Output is correct |
51 |
Correct |
46 ms |
6044 KB |
Output is correct |
52 |
Correct |
43 ms |
5972 KB |
Output is correct |
53 |
Correct |
48 ms |
6076 KB |
Output is correct |
54 |
Correct |
49 ms |
6100 KB |
Output is correct |
55 |
Correct |
43 ms |
5964 KB |
Output is correct |
56 |
Correct |
14 ms |
3028 KB |
Output is correct |
57 |
Correct |
50 ms |
6284 KB |
Output is correct |
58 |
Correct |
36 ms |
4864 KB |
Output is correct |
59 |
Correct |
0 ms |
340 KB |
Output is correct |
60 |
Correct |
0 ms |
340 KB |
Output is correct |
61 |
Correct |
543 ms |
40724 KB |
Output is correct |
62 |
Correct |
568 ms |
40628 KB |
Output is correct |
63 |
Correct |
545 ms |
39000 KB |
Output is correct |
64 |
Correct |
439 ms |
38364 KB |
Output is correct |
65 |
Correct |
473 ms |
39920 KB |
Output is correct |
66 |
Correct |
437 ms |
38360 KB |
Output is correct |
67 |
Correct |
440 ms |
39552 KB |
Output is correct |
68 |
Correct |
442 ms |
38156 KB |
Output is correct |
69 |
Correct |
444 ms |
38188 KB |
Output is correct |
70 |
Correct |
434 ms |
37804 KB |
Output is correct |
71 |
Correct |
392 ms |
36808 KB |
Output is correct |
72 |
Correct |
405 ms |
37444 KB |
Output is correct |
73 |
Correct |
409 ms |
37356 KB |
Output is correct |
74 |
Correct |
422 ms |
38460 KB |
Output is correct |
75 |
Correct |
418 ms |
38472 KB |
Output is correct |
76 |
Correct |
16 ms |
5204 KB |
Output is correct |
77 |
Correct |
291 ms |
35956 KB |
Output is correct |
78 |
Correct |
329 ms |
33252 KB |
Output is correct |
79 |
Correct |
1 ms |
340 KB |
Output is correct |
80 |
Correct |
0 ms |
340 KB |
Output is correct |