二分出要求的数的右边界,在从右边界确定左边界
#include <iostream>
using namespace std;
const int N = 1e5+10;
int a[N];
int main()
{
int n, q, num;
scanf("%d%d", &n, &q);
for(int i = 0; i < n; i++) scanf("%d", &a[i]);
while(q--)
{
scanf("%d", &num);
int l = 0, r = n-1;
//二分num的右边界
while(l < r)
{
int mid = l+r+1>>1;
if(a[mid] <= num) l = mid;//若l == mid区间没缩小,由此看出满足条件后会向右边收拢
else r = mid-1;
}
//求左边界
int start, end;
if(a[l] == num)
{
start = end = l;
while(a[--l] == num) start = l;
printf("%d %d\n", start, end);
}
else printf("-1 -1\n");
}
return 0;
}