AcWing 789. 数的范围
原题链接
简单
作者:
freshman
,
2024-07-22 21:09:05
,
所有人可见
,
阅读 2
C++ 代码
#include <iostream>
using namespace std;
const int N = 1e6 + 10;
int q[N];
int n, m;
int main()
{
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i ++) scanf("%d", &q[i]);
while(m --)
{
int k;
scanf("%d", &k);
int l = 0, r = n - 1;
while(l < r)
{ //找左边界
int mid = l + r >> 1;
if(q[mid] <= k) r = mid;
else l = mid + 1;
}
if(q[l] != k) printf("-1 -1\n");
else
{
printf("%d ", l);
l = 0, r = n - 1;
while(l < r)
{ //找右边界
int mid = l + r + 1 >> 1;
if(q[mid] <= k) l = mid;
else r = mid - 1;
}
printf("%d\n", r);
}
}
return 0;
}