789 数的范围
作者:
jy9
,
2024-06-07 23:28:28
,
所有人可见
,
阅读 5
在最后写了个判断,a[r]是查找的最终结果,如果这个
结果不等于目标,说明没找到
#include <iostream>
using namespace std;
#define int long long
const int N = 1e6+10;
signed main(){
int n, q;
int a[N];
int k;
cin >> n >> q;
for (int i = 0; i < n; i ++ ) cin >> a[i];
while (q -- ){
cin >> k;
int l = -1, r = n;
while (l+1<r){
int mid = l + r >> 1;
if (a[mid]<k)l = mid;
else r = mid;
}
int l2 = -1, r2 = n;
while (l2+1!=r2){
int mid = l2 + r2 >> 1;
if(a[mid] <= k)l2 = mid;
else r2 = mid;
}
if (a[r] != k){
printf("-1 -1\n");
continue;
}
printf("%d %d\n", r, l2);
}
return 0;
}