AcWing 789. 数的范围
原题链接
简单
作者:
yoruichi
,
2021-03-08 22:24:48
,
所有人可见
,
阅读 300
C++ 代码
#include <iostream>
using namespace std;
const int N = 1e6+10;
int q[N];
int lower_bound(int a[],int left,int right,int x)
{
int mid;
while(left < right)
{
mid = left+right >> 1;
if(a[mid] >= x) right = mid;
else left = mid+1;
}
return left;
}
int upper_bound(int a[],int left,int right,int x)
{
int mid;
while(left < right)
{
mid = left + right >> 1;
if(a[mid] > x) right = mid;
else left = mid + 1;
}
return left-1;
}
int main()
{
int n,x,que,LB,RB;
cin >> n >>x;
for(int i=0;i<n;i++)
cin >> q[i];
while(x--)
{
cin >> que;
LB = lower_bound(q,0,n,que);
RB = upper_bound(q,0,n,que);
if(q[LB] != que) LB = -1;
if(q[RB] != que ) RB = -1;
cout << LB << ' '<< RB << endl;
}
return 0;
}