可能比一些题解稍微清楚些欢迎交流。
#include<iostream>
using namespace std;
int a[100005],b[100005];
int len=0,count=0;
bool check(int goal,int mid)
{
if(goal<=mid) return 1;
else return 0;
}
int find_half(int q[],int l,int r,int g)
{
while(l<r)
{
int mid = l+r>>1;
if(check(g,q[mid])) r=mid;
else l=mid+1;
}
if (q[l]==g)
return l;
else
return 10006;
}
int find_max(int q[],int g,int now)
{
int you = now;
while (q[++you]==g); return you-1;
}
int find_min(int q[],int g,int now)
{
int zuo = now;
while (q[--zuo]==g) ; return zuo+1;
}
int main()
{
cin>>len>>count;
for(int i=0;i<len;i++)
cin>>a[i];
for(int i=0;i<count;i++)
cin>>b[i];
for(int i=0;i<count;i++)
{
if(find_half(a,0,len-1,b[i])==10006) cout<<"-1"<<' '<<"-1"<<endl;
else
cout<<find_min(a,b[i],find_half(a,0,len-1,b[i]))<<' '<<find_max(a,b[i],find_half(a,0,len-1,b[i]))<<endl;
}
}