AcWing 789. 数的范围
原题链接
简单
作者:
川@木子
,
2020-09-05 12:51:38
,
所有人可见
,
阅读 2
//先找左边,再找右边
#include<iostream>
using namespace std;
const int N=100010;
int n,q;
int num[N];
//找左边
void binary_search_left(int a,int l,int r){
while(l<r){
int mid=(l+r)>>1;
if(num[mid]<a){
l=mid+1;
}else{
r=mid;
}
}
cout<<l<<" ";
}
//找右边
void binary_search_right(int a,int l,int r){
while(l<r){
int mid=(l+r+1)>>1;
if(num[mid]>a){
r=mid-1;
}else{
l=mid;
}
}
cout<<l<<endl;
}
int main(){
cin>>n>>q;
for(int i=0;i<n;i++){
cin>>num[i];
}
int a;
for(int k=0;k<q;k++){
cin>>a;
int l=0,r=n-1;
//找左边
while(l<r){
int mid=(l+r)>>1;
if(num[mid]<a){
l=mid+1;
}else{
r=mid;
}
}
if(num[l]!=a){
cout<<"-1 -1"<<endl;
}else{
cout<<l<<" ";
//找右边
l=0,r=n-1;
while(l<r){
int mid=(l+r+1)>>1;
if(num[mid]>a){
r=mid-1;
}else{
l=mid;
}
}
cout<<l<<endl;
}
}
}