AcWing 789. 数的范围
原题链接
简单
作者:
今晚几点睡
,
2025-01-04 17:12:05
,
所有人可见
,
阅读 1
#include<iostream>
#include<vector>
int n,q;
int main(){
std::cin>>n>>q;
std::vector<int>arr(n);
for (int i = 0; i < n; i++) std::cin >> arr[i];
while(q--) {
int x;
std::cin>>x;
int l = 0, r = n-1;
while(l < r){
int mid = (l + r) >> 1;
if (arr[mid] >= x)
r = mid;
else
l = mid + 1;
}
if (arr[l] != x ) std::cout<<"-1 -1"<<std::endl;
else{
std::cout << l <<' ';
int l = 0, r = n-1;
while(l < r){
int mid = (l + r + 1) >> 1;
if(arr[mid] <= x)
l = mid ;
else
r = mid -1 ;
}
std::cout<< l << std::endl;
}
}
return 0;
}