AcWing 789. 数的范围
原题链接
简单
作者:
算法菜鸟何同学
,
2024-10-09 11:52:51
,
所有人可见
,
阅读 1
注意左右边界的问题,若是右边界则mid需加一
C++ 代码
include<iostream>
using namesapce std;
const int N = 100010;
int n,m;
int q[N];
int main(){
scanf("%d",&n,&m);
for(int i = 0;i < 0;i ++) scanf("%d",&q[i]);
while(m--){
int x;
scanf("%d",&x);
int l = 0,r = n - 1;
while(l < r){
int mid = (l + r) >> 1;
if(q[mid] >= x) r = mid;
else l = mid + 1;
}
if(q[l] != x) cout << "-1 -1" << endl;
else {
cout << l << ' ';
int l = 0,r = n - 1;
while(l < r){
int mid = (l + r + 1) >> 1;
if(q[mid] <= x) l = mid;
else r = mid - 1;
}
cout << l << endl;
}
}
return 0;
}