AcWing 789. 数的范围
原题链接
简单
作者:
hxj
,
2021-04-17 20:57:20
,
所有人可见
,
阅读 205
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
int n, a[N], q;
int main()
{
scanf("%d%d", &n, &q);
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
while(q--) {
int num;
scanf("%d", &num);
// 找右边界,保证只使用 > 进行比较会比较清晰
int l = 0, r = n-1;
while(l < r) {
int mid = l+r>>1;
if(num > a[mid]) l = mid+1;
else r = mid;
}
if(a[l] != num) {
printf("-1 -1\n");
continue;
}
printf("%d ", l);
l = 0, r = n-1;
while(l < r) {
int mid = l+r+1>>1;
if(a[mid] > num) r = mid-1;
else l = mid;
}
printf("%d\n", l);
}
return 0;
}