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=2e5+15;
int L[N],R[N];
int a[N];
vector<int>ans;
vector<int>st[N*4];
void merge(int x){
st[x].clear();
int i=0,j=0;
while(1){
if(i==st[x*2].size() and j==st[x*2+1].size()){
break;
}
if(i==st[x*2].size()){
st[x].pb(st[x*2+1][j]);
j++;
}
else if(j==st[x*2+1].size()){
st[x].pb(st[x*2][i]);
i++;
}
else if(st[x*2][i]<st[x*2+1][j]){
st[x].pb(st[x*2][i]);
i++;
}
else {
st[x].pb(st[x*2+1][j]);
j++;
}
}
}
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));
}
map<pair<int,int>,int>mp;
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n,q;
cin>>n>>q;
int g=0;
set<int>st;
for(int i=1;i<=n;i++){
cin>>a[i];
st.insert(a[i]);
g=max(a[i],g);
}
build(1,1,n);
while(q--){
int l,r;
cin>>l>>r;
if(mp[{l,r}]){
ans.pb(mp[{l,r}]);
continue;
}
int lo=0,hi=min(r-l+1,g);
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;
}
}
mp[{l,r}]=hi;
ans.pb(hi);
}
for(auto p:ans)cout<<p<<'\n';
}
Compilation message (stderr)
index.cpp: In function 'void merge(long long int)':
index.cpp:16:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | if(i==st[x*2].size() and j==st[x*2+1].size()){
| ~^~~~~~~~~~~~~~~~
index.cpp:16:34: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
16 | if(i==st[x*2].size() and j==st[x*2+1].size()){
| ~^~~~~~~~~~~~~~~~~~
index.cpp:19:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | if(i==st[x*2].size()){
| ~^~~~~~~~~~~~~~~~
index.cpp:23:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | else if(j==st[x*2+1].size()){
| ~^~~~~~~~~~~~~~~~~~
index.cpp: In function 'long long int check(long long int, long long int)':
index.cpp:50:9: warning: unused variable 'p' [-Wunused-variable]
50 | 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... |