AcWing 789. 数的范围
原题链接
简单
作者:
Aleia
,
2024-10-08 15:23:49
,
所有人可见
,
阅读 1
#include <iostream>
using namespace std;
const int N = 1e6+10;
int a[N];
int main()
{
int n,q;
cin>>n>>q;
for(int i=0;i<n;i++) cin>>a[i];
while(q--)
{
int x;
cin>>x;
int l=0,r=n-1;
while(l<r)
{
int mid = (l+r)>>1;
if(a[mid]>=x) r = mid;//如果a[mid]>=x,则a[mid]右边的数也必定>=x,所以更新r
else l = mid + 1;
}
if(a[l] == x)
{
cout<<l;
l=0,r=n-1;
while(l<r)
{
int mid = (l+r+1) >>1;
if(a[mid]<=x) l = mid;
else r = mid -1;
}
cout<<' '<<l<<endl;
}
else cout<<"-1 -1"<<endl;
}
return 0;
}