AcWing 789. 数的范围
原题链接
简单
作者:
wangyj
,
2020-10-30 20:19:37
,
所有人可见
,
阅读 394
#include<bits/stdc++.h>
using namespace std;
int a[100005],ans[10005][2];
int main()
{
//6 4 1 2 2 3 3 4 3 4 5 0
int n,t,question,i;
scanf("%d%d",&n,&t);
for(i=0;i<n;i++)scanf("%d",&a[i]);
for(i=0;i<t;i++){
scanf("%d",&question);
int up=n,down=0,mid,j;
while(down<up){
mid=(up+down)/2;
if(a[mid]==question)break;
else if(a[mid]>question)up=mid;
else down=mid+1;
}
if(a[mid]==question){
for(j=mid;j>=0;j--)if(a[j]!=question)break;
ans[i][0]=j+1;
for(j=mid;j<n;j++)if(a[j]!=question)break;
ans[i][1]=j-1;
}
else ans[i][0]=ans[i][1]=-1;
}
for(i=0;i<t;i++)printf("%d %d\n",ans[i][0],ans[i][1]);
return 0;
}