AcWing 789. 数的范围
原题链接
简单
作者:
__43
,
2020-09-23 22:27:38
,
所有人可见
,
阅读 376
#include <iostream>
using namespace std;
const int N = 1e5 + 10;
int q[N];
int main(void)
{
int n,k;
scanf("%d%d",&n,&k);
for (int i = 0;i < n;++i)
scanf("%d",&q[i]);
while (k--)
{
int x, l = 0,r = n - 1;
scanf("%d",&x);
while (l < r)
{
int m = l + (r - l) / 2;
if (q[m] >= x) r = m;
else l = m + 1;
}
if (q[l] != x)
{
printf("-1 -1\n");
continue;
}
printf("%d ",l);
l = 0,r = n - 1;
while (l < r)
{
int m = l + (r - l + 1) / 2;
if (q[m] <= x) l = m;
else r = m - 1;
}
printf("%d\n",l);
}
return 0;
}