AcWing 789. 数的范围
原题链接
简单
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+10;
int n,q;
int w[N];
void check(int a)
{
int l = 1, r = n;
while(l < r)
{
int mid = l + r >> 1;
if(w[mid] >= a) r = mid;
else l = mid+1;
}
if(w[l] != a) printf("-1 -1\n");
else
{
printf("%d ",l-1);
l = 1, r = n;
while(l < r)
{
int mid = l + r + 1 >> 1;
if(w[mid] <= a) l = mid;
else r = mid-1;
}
printf("%d\n",r-1);
}
}
int main()
{
cin >> n >> q;
for(int i = 1; i <= n; ++i) scanf("%d",&w[i]);
while(q--)
{
int a; cin >> a;
check(a);
}
}