AcWing 789. 数的范围
原题链接
简单
作者:
啊呀呀呀呀呀
,
2021-03-04 09:28:57
,
所有人可见
,
阅读 251
C++ 代码
#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int main()
{
int n,k;
int q[N];
cin >> n >> k;
for(int i = 0; i < n; i ++ ) cin >> q[i];
while(k -- )
{
int t;
cin >> t;
int l = 0, r = n- 1;
while(l < r)
{
int mid = (l+r) >> 1;
if(q[mid] >= t) r = mid;
else l = mid + 1;
}
if(q[l] != t)
{
cout<<"-1 -1"<<endl;
continue;
}
cout<<l<<" ";
l = 0, r = n - 1;
while(l < r)
{
int mid = (l + r + 1) >> 1;
if(q[mid] <= t) l = mid;
else r = mid - 1;
}
cout<<l<<endl;
}
return 0;
}