This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ff first
#define ss second
#define pb push_back
const int N=3e5+5;
int L[N],R[N];
int a[N];
vector<int>ans;
vector<int>st[N*4];
void merge(int x){
st[x].clear();
for(auto p:st[x*2])st[x].pb(p);
for(auto p:st[x*2+1])st[x].pb(p);
sort(st[x].begin(),st[x].end());
}
void build(int node, int L, int R){
if(L==R){
st[node].pb(a[L]);
return;
}
int mid=(L+R)/2;
build(node*2,L,mid);
build(node*2+1,mid+1,R);
merge(node);
}
int check(int val, int x){
val--;
auto it=upper_bound(st[x].begin(),st[x].end(),val);
int p=0;
return st[x].size()-(it-st[x].begin());
}
int find(int node, int L, int R, int l, int r, int val){
if(r<L or R<l)return 0;
if(l<=L and R<=r){
return check(val,node);
}
int mid=(L+R)/2;
return(find(node*2,L,mid,l,r,val)+find(node*2+1,mid+1,R,l,r,val));
}
signed main(){
int n,q;
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
}
build(1,1,n);
while(q--){
int l,r;
cin>>l>>r;
int lo=0,hi=1e9;
while(hi>=lo){
int mid=(hi+lo)/2;
int k=find(1,1,n,l,r,mid);
if(k>=mid){
lo=mid+1;
}
else{
hi=mid-1;
}
}
ans.pb(hi);
}
for(auto p:ans)cout<<p<<endl;
}
Compilation message (stderr)
index.cpp: In function 'long long int check(long long int, long long int)':
index.cpp:31:9: warning: unused variable 'p' [-Wunused-variable]
31 | int p=0;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |