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;
int a[200005];
vector<int>seg[800005];
void build(int no,int l,int r)
{
//cout<<"B "<<no<<" "<<l<<" "<<r<<endl;
if(l==r){seg[no].push_back(a[l]);return;}
build(2*no,l,(l+r)/2);
build(2*no+1,(l+r)/2+1,r);
seg[no].resize(seg[no*2].size()+seg[no*2+1].size());
merge(seg[2*no+1].begin(), seg[2*no+1].end(), seg[2*no].begin(), seg[2*no].end(), seg[no].begin());
}
int query(int n,int l,int r,int ql,int qr,int h)
{
//cout<<"A "<<n<<endl;
if(l>r || ql>qr || r<ql || qr<l)return 0;
if(ql<=l && r<=qr)
{
if(lower_bound(seg[n].begin(),seg[n].end(),h)==seg[n].end())return 0;
int index=lower_bound(seg[n].begin(),seg[n].end(),h)-seg[n].begin();
return seg[n].size()-index;
}
return query(2*n,l,(l+r)/2,ql,qr,h)+query(2*n+1,(l+r)/2+1,r,ql,qr,h);
}
int ans(int a,int b,int n)
{
int answer=0;
int l=1,r=200000;
int mid;
while(l<=r)
{mid=(l+r)/2;
//cout<<l<<" "<<r<<" "<<mid<<endl;
if(query(1,1,n,a,b,mid)>=mid)
answer=max(answer,mid),l=mid+1;
else r=mid-1;
}
return answer;
}
int main()
{
int n,q;
cin>>n>>q;
for(int i=1;i<=n;i++)
cin>>a[i];
build(1,1,n);
//cout<<"C"<<endl;
while(q--)
{
int a,b;
cin>>a>>b;
cout<<ans(a,b,n)<<endl;
}
return 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... |