AcWing 789. 数的范围
原题链接
简单
作者:
uchar
,
2024-12-08 11:24:33
,
所有人可见
,
阅读 14
131ms
#include <iostream>
using namespace std;
const int N = 1e5 + 10;
int a[N];
int mid, n, q;
void search(int x) // 先找起点,后找终点
{
int l = 0, r = n - 1, ans1, ans2;
while (l < r)
{
mid = l + r >> 1;
if (a[mid] >= x) r = mid;
else l = mid + 1;
}
ans1 = l;
l = 0, r = n - 1;
while (l < r)
{
mid = (l + r + 1) >> 1;
if (a[mid] <= x) l = mid;
else r = mid - 1;
}
ans2 = r;
if (ans1 > ans2 || a[ans1] != x || a[ans2] != x)
printf("%d %d\n", -1, -1);
else printf("%d %d\n", ans1, ans2);
}
int main()
{
int k;
scanf("%d%d", &n, &q);
for (int i = 0; i < n; i ++ )
scanf("%d", &a[i]);
while (q-- )
{
cin >> k;
search(k);
}
return 0;
}