#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ff first
#define ss second
ll ttt;
const ll INF=1e18;
const ll MOD=1e9+7;
const ll N=200007;
ll n,m,k;
int a[N];
int d[N];
int ind[N];
bool met[N];
set<int>s;
int curlen=0;
pair<int,int> t[4*N];
int tt[4*N];
vector<pair<pair<int,int>,int>>q;
int qi=0;
void build(int v, int tl, int tr){
if(tl==tr){
t[v]={a[tl],tl};
}
else{
int tm=(tl+tr)/2;
build(2*v,tl,tm);
build(2*v+1,tm+1,tr);
t[v]=max(t[2*v],t[2*v+1]);
}
}
void build2(int v, int tl, int tr){
if(tl==tr){
tt[v]=d[tl];
}
else{
int tm=(tl+tr)/2;
build2(2*v,tl,tm);
build2(2*v+1,tm+1,tr);
tt[v]=tt[2*v]+tt[2*v+1];
}
}
void update(int v, int tl, int tr, int ind, int val){
if(tl==tr){
tt[v]=val;
}
else{
int tm=(tl+tr)/2;
if(ind<=tm){
update(2*v,tl,tm,ind,val);
}
else{
update(2*v+1,tm+1,tr,ind,val);
}
tt[v]=tt[2*v]+tt[2*v+1];
}
}
int getsum(int v, int tl, int tr, int l, int r){
if(l>r)return 0;
if(tl==l&&tr==r){
return tt[v];
}
else{
int tm=(tl+tr)/2;
return getsum(2*v,tl,tm,l,min(r,tm))+
getsum(2*v+1,tm+1,tr,max(tm+1,l),r);
}
}
int binsearch(int indd){
int l=1,r=n+1;
while(l<r){
int mid=(l+r)/2;
if(getsum(1,1,n,mid,n)>indd){
l=mid+1;
}
else{
r=mid;
}
}
return l-1;
}
pair<int,int> getmax(int v, int tl, int tr, int l, int r){
if(l>r)return {0,0};
if(tl==l&&tr==r){
return t[v];
}
else{
int tm=(tl+tr)/2;
return max(getmax(2*v,tl,tm,l,min(r,tm)),
getmax(2*v+1,tm+1,tr,max(tm+1,l),r));
}
}
int stepcnt=0;
void step(vector<int>&ans){
while(qi!=m&&q[qi].ff.ff==stepcnt){
int indd=n-q[qi].ff.ss-1;
int cmp=binsearch(indd);
int cmpind=getsum(1,1,n,cmp,n)-1;
ans[q[qi].ss]=a[ind[cmp]-(cmpind-indd)];
qi++;
}
stepcnt++;
// cout<<"step"<<endl;
while(curlen+d[-(*s.begin())]<=n/2){
curlen+=d[-(*s.begin())];
s.erase(s.begin());
}
int v=-(*s.begin());
int i=ind[v];
int len=d[v];
// cout<<v<<" "<<i<<" "<<len<<endl;
int l=ind[v]-d[v]+1;
int r=n/2-curlen+l-1;
d[v]=i-r;
update(1,1,n,v,d[v]);
// cout<<l<<" "<<r<<endl;
while(l<=r){
pair<int,int>e=getmax(1,0,n-1,l,r);
int mx=e.ff;
int mxi=e.ss;
int newlen=mxi-l+1;
d[mx]=newlen;
update(1,1,n,mx,d[mx]);
ind[mx]=mxi;
s.insert(-mx);
l=mxi+1;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen("Ainput.txt","r",stdin);
// freopen("Aoutput.txt","w",stdout);
cin>>n>>m;
for(int i=0;i<n;i++){
cin>>a[n-i-1];
}
build(1,0,n-1);
int lsti=-1;
int mx=n;
int i=0;
while(i!=n){
while(i!=n&&a[i]<mx){
met[a[i]]=true;
i++;
}
d[mx]=i-lsti;
ind[mx]=i;
s.insert(-mx);
met[mx]=true;
while(met[mx]){
mx--;
}
lsti=i;
i++;
}
build2(1,1,n);
int t=0;
for(int i=0;i<m;i++){
int x,y;
cin>>x>>y;
y--;
q.push_back({{x,y},i});
// t=x;
}
sort(q.begin(),q.end());
vector<int>ans(m,0);
for(int i=0;i<n*5;i++){
step(ans);
}
while(qi!=m){
int indd=n-q[qi].ff.ss-1;
int cmp=binsearch(indd);
int cmpind=getsum(1,1,n,cmp,n)-1;
ans[q[qi].ss]=a[ind[cmp]-(cmpind-indd)];
qi++;
}
for(int i=0;i<m;i++){
cout<<ans[i]<<endl;
}
}
Compilation message
Main.cpp: In function 'void step(std::vector<int>&)':
Main.cpp:124:9: warning: unused variable 'len' [-Wunused-variable]
124 | int len=d[v];
| ^~~
Main.cpp: In function 'int main()':
Main.cpp:174:9: warning: unused variable 't' [-Wunused-variable]
174 | int t=0;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1307 ms |
30832 KB |
Output is correct |
2 |
Correct |
1311 ms |
31300 KB |
Output is correct |
3 |
Correct |
1229 ms |
30460 KB |
Output is correct |
4 |
Correct |
1220 ms |
29316 KB |
Output is correct |
5 |
Correct |
1204 ms |
31120 KB |
Output is correct |
6 |
Correct |
1128 ms |
29852 KB |
Output is correct |
7 |
Correct |
1161 ms |
31252 KB |
Output is correct |
8 |
Correct |
1126 ms |
30384 KB |
Output is correct |
9 |
Correct |
1134 ms |
30384 KB |
Output is correct |
10 |
Correct |
1163 ms |
29880 KB |
Output is correct |
11 |
Correct |
1184 ms |
30644 KB |
Output is correct |
12 |
Correct |
1107 ms |
29096 KB |
Output is correct |
13 |
Correct |
1158 ms |
29900 KB |
Output is correct |
14 |
Correct |
1150 ms |
30344 KB |
Output is correct |
15 |
Correct |
1199 ms |
30360 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1173 ms |
29164 KB |
Output is correct |
18 |
Correct |
1130 ms |
28844 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
0 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1795 ms |
50424 KB |
Output is correct |
2 |
Correct |
1631 ms |
52008 KB |
Output is correct |
3 |
Correct |
1311 ms |
42804 KB |
Output is correct |
4 |
Correct |
1324 ms |
40560 KB |
Output is correct |
5 |
Correct |
1344 ms |
42836 KB |
Output is correct |
6 |
Correct |
1309 ms |
40876 KB |
Output is correct |
7 |
Correct |
1543 ms |
46252 KB |
Output is correct |
8 |
Correct |
1500 ms |
45500 KB |
Output is correct |
9 |
Correct |
1321 ms |
41392 KB |
Output is correct |
10 |
Correct |
1476 ms |
43508 KB |
Output is correct |
11 |
Correct |
1255 ms |
38320 KB |
Output is correct |
12 |
Correct |
1317 ms |
37652 KB |
Output is correct |
13 |
Correct |
1594 ms |
42492 KB |
Output is correct |
14 |
Correct |
1385 ms |
38884 KB |
Output is correct |
15 |
Correct |
1591 ms |
43604 KB |
Output is correct |
16 |
Correct |
69 ms |
13644 KB |
Output is correct |
17 |
Correct |
1656 ms |
49572 KB |
Output is correct |
18 |
Correct |
1223 ms |
36580 KB |
Output is correct |
19 |
Correct |
345 ms |
19136 KB |
Output is correct |
20 |
Correct |
365 ms |
21092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
233 ms |
15448 KB |
Output is correct |
2 |
Correct |
221 ms |
16836 KB |
Output is correct |
3 |
Correct |
227 ms |
15324 KB |
Output is correct |
4 |
Correct |
185 ms |
13512 KB |
Output is correct |
5 |
Correct |
201 ms |
15312 KB |
Output is correct |
6 |
Correct |
177 ms |
13752 KB |
Output is correct |
7 |
Correct |
197 ms |
14816 KB |
Output is correct |
8 |
Correct |
183 ms |
14136 KB |
Output is correct |
9 |
Correct |
199 ms |
14788 KB |
Output is correct |
10 |
Correct |
177 ms |
13060 KB |
Output is correct |
11 |
Correct |
186 ms |
13332 KB |
Output is correct |
12 |
Correct |
169 ms |
12980 KB |
Output is correct |
13 |
Correct |
174 ms |
13256 KB |
Output is correct |
14 |
Correct |
175 ms |
13256 KB |
Output is correct |
15 |
Correct |
185 ms |
13044 KB |
Output is correct |
16 |
Correct |
38 ms |
10076 KB |
Output is correct |
17 |
Correct |
189 ms |
17376 KB |
Output is correct |
18 |
Correct |
163 ms |
12484 KB |
Output is correct |
19 |
Correct |
0 ms |
4444 KB |
Output is correct |
20 |
Correct |
0 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1307 ms |
30832 KB |
Output is correct |
2 |
Correct |
1311 ms |
31300 KB |
Output is correct |
3 |
Correct |
1229 ms |
30460 KB |
Output is correct |
4 |
Correct |
1220 ms |
29316 KB |
Output is correct |
5 |
Correct |
1204 ms |
31120 KB |
Output is correct |
6 |
Correct |
1128 ms |
29852 KB |
Output is correct |
7 |
Correct |
1161 ms |
31252 KB |
Output is correct |
8 |
Correct |
1126 ms |
30384 KB |
Output is correct |
9 |
Correct |
1134 ms |
30384 KB |
Output is correct |
10 |
Correct |
1163 ms |
29880 KB |
Output is correct |
11 |
Correct |
1184 ms |
30644 KB |
Output is correct |
12 |
Correct |
1107 ms |
29096 KB |
Output is correct |
13 |
Correct |
1158 ms |
29900 KB |
Output is correct |
14 |
Correct |
1150 ms |
30344 KB |
Output is correct |
15 |
Correct |
1199 ms |
30360 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1173 ms |
29164 KB |
Output is correct |
18 |
Correct |
1130 ms |
28844 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
0 ms |
4444 KB |
Output is correct |
21 |
Correct |
1795 ms |
50424 KB |
Output is correct |
22 |
Correct |
1631 ms |
52008 KB |
Output is correct |
23 |
Correct |
1311 ms |
42804 KB |
Output is correct |
24 |
Correct |
1324 ms |
40560 KB |
Output is correct |
25 |
Correct |
1344 ms |
42836 KB |
Output is correct |
26 |
Correct |
1309 ms |
40876 KB |
Output is correct |
27 |
Correct |
1543 ms |
46252 KB |
Output is correct |
28 |
Correct |
1500 ms |
45500 KB |
Output is correct |
29 |
Correct |
1321 ms |
41392 KB |
Output is correct |
30 |
Correct |
1476 ms |
43508 KB |
Output is correct |
31 |
Correct |
1255 ms |
38320 KB |
Output is correct |
32 |
Correct |
1317 ms |
37652 KB |
Output is correct |
33 |
Correct |
1594 ms |
42492 KB |
Output is correct |
34 |
Correct |
1385 ms |
38884 KB |
Output is correct |
35 |
Correct |
1591 ms |
43604 KB |
Output is correct |
36 |
Correct |
69 ms |
13644 KB |
Output is correct |
37 |
Correct |
1656 ms |
49572 KB |
Output is correct |
38 |
Correct |
1223 ms |
36580 KB |
Output is correct |
39 |
Correct |
345 ms |
19136 KB |
Output is correct |
40 |
Correct |
365 ms |
21092 KB |
Output is correct |
41 |
Correct |
233 ms |
15448 KB |
Output is correct |
42 |
Correct |
221 ms |
16836 KB |
Output is correct |
43 |
Correct |
227 ms |
15324 KB |
Output is correct |
44 |
Correct |
185 ms |
13512 KB |
Output is correct |
45 |
Correct |
201 ms |
15312 KB |
Output is correct |
46 |
Correct |
177 ms |
13752 KB |
Output is correct |
47 |
Correct |
197 ms |
14816 KB |
Output is correct |
48 |
Correct |
183 ms |
14136 KB |
Output is correct |
49 |
Correct |
199 ms |
14788 KB |
Output is correct |
50 |
Correct |
177 ms |
13060 KB |
Output is correct |
51 |
Correct |
186 ms |
13332 KB |
Output is correct |
52 |
Correct |
169 ms |
12980 KB |
Output is correct |
53 |
Correct |
174 ms |
13256 KB |
Output is correct |
54 |
Correct |
175 ms |
13256 KB |
Output is correct |
55 |
Correct |
185 ms |
13044 KB |
Output is correct |
56 |
Correct |
38 ms |
10076 KB |
Output is correct |
57 |
Correct |
189 ms |
17376 KB |
Output is correct |
58 |
Correct |
163 ms |
12484 KB |
Output is correct |
59 |
Correct |
0 ms |
4444 KB |
Output is correct |
60 |
Correct |
0 ms |
4444 KB |
Output is correct |
61 |
Correct |
1916 ms |
53492 KB |
Output is correct |
62 |
Correct |
2079 ms |
54756 KB |
Output is correct |
63 |
Correct |
1909 ms |
50832 KB |
Output is correct |
64 |
Correct |
1618 ms |
48444 KB |
Output is correct |
65 |
Correct |
1712 ms |
50140 KB |
Output is correct |
66 |
Correct |
1657 ms |
48308 KB |
Output is correct |
67 |
Correct |
1657 ms |
48508 KB |
Output is correct |
68 |
Correct |
1622 ms |
47532 KB |
Output is correct |
69 |
Correct |
1637 ms |
47796 KB |
Output is correct |
70 |
Correct |
1598 ms |
46784 KB |
Output is correct |
71 |
Correct |
1553 ms |
44716 KB |
Output is correct |
72 |
Correct |
1570 ms |
46204 KB |
Output is correct |
73 |
Correct |
1606 ms |
45276 KB |
Output is correct |
74 |
Correct |
1642 ms |
46548 KB |
Output is correct |
75 |
Correct |
1627 ms |
46512 KB |
Output is correct |
76 |
Correct |
67 ms |
13912 KB |
Output is correct |
77 |
Correct |
1711 ms |
51884 KB |
Output is correct |
78 |
Correct |
1550 ms |
41904 KB |
Output is correct |
79 |
Correct |
1 ms |
4444 KB |
Output is correct |
80 |
Correct |
0 ms |
4444 KB |
Output is correct |