AcWing 789. 数的范围
原题链接
简单
作者:
Value
,
2020-07-05 08:49:07
,
所有人可见
,
阅读 473
#include <iostream>
using namespace std;
const int N = 1e5 + 10;
int a[N];
int main(){
int n, m; cin >> n >> m;
for(int i = 0; i < n; i ++ ) cin >> a[i];
while(m -- ){
int f; cin >> f;
int l = 0, r = n;
/*
找到左边界,在从左边界开始循环一次找到右边界
*/
while(l < r){
int mid = l + r >> 1;
if(a[mid] >= f) r = mid;
else l = mid + 1;
}
if(l == n || a[l] != f){
cout << "-1 -1" << endl;
continue;
}
r = l;
while(a[r + 1] == f && r + 1 < n) r ++ ;
cout << l << " " << r << endl;
}
return 0;
}