#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << " " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << " " << j << " " << #i << " " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<int,int>pii;
typedef pair<int,pii>pi2;
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
inline int combine(int a, int b){
return a+b;
}
struct node{
int s,e,m;
node *l,*r;
int v;
node(int ss, int ee):s(ss),e(ee),m((s+e)>>1),v(0){
if(s!=e){
l=new node(s,m);
r=new node(m+1,e);
}
}
void upd(int x, int y){
if(s==e){
v=y;
return;
}
if(x<=m) l->upd(x,y);
else r->upd(x,y);
v=combine(l->v,r->v);
}
int query(int x, int y){
if(x<=s&&y>=e){
return v;
}
if(y<=m) return l->query(x,y);
if(x>m) return r->query(x,y);
return combine(l->query(x,m),r->query(m+1,y));
}
int bs(int x){
if(s==e){
return s;
}
if(l->v>=x) return l->bs(x);
else return r->bs(x-l->v);
}
};
void solve(){
int n,q;
cin >> n >> q;
int arr[n+5];
for(int x=1;x<=n;x++){
cin >> arr[x];
}
vector<pii>que[n+5];
int temp,temp2;
int ans[q];
for(int x=0;x<q;x++){
cin >> temp >> temp2;
temp=min(temp,n+1);
if(temp==0){
ans[x]=arr[temp2];
continue;
}
que[temp].push_back({temp2,x});
}
//shuffle once
int arr2[n+5];
memset(arr2,-1,sizeof(arr2));
int a=1;
int b=n/2+1;
int ptr=1;
while(a<=n/2||b<=n){
//show2(a,a,b,b);
if(a>n/2){
arr2[ptr]=arr[b];
b++;
}
else if(b>n){
arr2[ptr]=arr[a];
a++;
}
else if(arr[a]<arr[b]){
arr2[ptr]=arr[a];
a++;
}
else{
arr2[ptr]=arr[b];
b++;
}
ptr++;
}
int nxt[n+5];
memset(nxt,-1,sizeof(nxt));
deque<int>d;
for(int x=n;x>=1;x--){
while(!d.empty()&&arr[d.back()]<arr[x]) d.pop_back();
if(!d.empty()) nxt[x]=d.back();
else nxt[x]=n+1;
d.push_back(x);
}
pii storage[n+5];
node st(0,n+5);
int maxi=arr2[1];
int l=1;
for(int x=2;x<=n;x++){
if(arr2[x]>maxi){
st.upd(maxi,x-l);
storage[maxi]={l,x-1};
//show(maxi,maxi);
//show2(l,l,x-1,x-1);
maxi=arr2[x];
l=x;
}
}
//show4(arr2,arr2);
st.upd(maxi,n+1-l);
storage[maxi]={l,n};
for(int x=1;x<=n+1;x++){
for(auto it:que[x]){
int index=st.bs(it.first);
int hold=st.query(0,index-1);
//show2(index,index,hold,hold);
ans[it.second]=arr2[storage[index].first+it.first-hold-1];
}
int index=st.bs(n/2);
int hold=st.query(0,index);
int cur=st.query(index,index);
if(hold>n/2){
int diff=hold-n/2;
int r=storage[index].second;
storage[index].second-=diff;
st.upd(index,cur-diff);
int lft=storage[index].second+1;
while(lft<=r){
int a=min(nxt[lft],r+1);
storage[arr2[lft]]={lft,a-1};
st.upd(arr2[lft],a-lft);
lft=a;
}
}
}
for(int x=0;x<q;x++){
cout << ans[x] << "\n";
}
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
//freopen("in.txt","r",stdin);
//freopen("in.txt","w",stdout);
int t=1;
//cin >> t;
while(t--){
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |