AcWing 789. 数的范围
题目描述:
代码:
#include<iostream>
using namespace std;
const int MAX=1e5+5;
int main(){
int n,q;
cin>>n>>q;
int a[MAX];
for(int i=0;i<n;i++)cin>>a[i];
while(q--){
int k;
cin>>k;
int l=0,r=n-1;
while(l<r){
int mid=l+r>>1;
if(a[mid]>=k)r=mid;
else l=mid+1;
}
if(a[l]!=k)cout<<"-1 -1"<<endl;
else {
cout<<l<<" ";
int l=0,r=n-1;
while(l<r){
int mid=l+r+1>>1;
if(a[mid]<=k)l=mid;
else r=mid-1;
}
cout<<r<<endl;
}
}
return 0;
}
原题链接:活动 - AcWing