AcWing 789. 数的范围
原题链接
简单
作者:
minux
,
2020-04-18 21:57:39
,
所有人可见
,
阅读 395
#include <bits/stdc++.h>
using namespace std;
int n;
int k;
int main(){
cin>>n>>k;
int a[n];
for(int i=0; i<n; ++i) cin>>a[i];
int v;
while(cin>>v){
int l=0;
int r=n;
int mid=-1;
while(l<r){
mid = l+(r-l)/2;
if(a[mid]==v) break;
else if(a[mid]<v) l=mid+1;
else r=mid;
}
if(l<r){
int start = mid;
int end = mid;
while(a[start]==a[mid]) start--;
while(a[end]==a[mid]) end++;
cout<<start+1<<" "<<end-1<<endl;
}
else
cout<<-1<<" "<<-1<<endl;
}
return 0;
}