数的范围
acwing789
STL
const int N=1e5+10;
int a[N];
int n,m;
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++) cin>>a[i];
while(m--)
{
int x;
cin>>x;
int st=lower_bound(a,a+n,x)-a;
if(st == n || a[st] != x)
{
cout<<-1<<' '<<-1<<endl;
continue;
}
int ed=upper_bound(a,a+n,x)-a;
cout<<st<<' '<<ed-1<<endl;
}
//system("pause");
}
手写二分
const int N=1e5+10;
int a[N];
int n,m;
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++) cin>>a[i];
while(m--)
{
int x;
cin>>x;
int l=0,r=n;
while(l<r)
{
int mid=l+r>>1;
if(a[mid] >= x) r=mid;
else l=mid+1;
}
if(r == n || a[r] != x)
{
cout<<-1<<' '<<-1<<endl;
continue;
}
cout<<l<<' ';
l=0,r=n-1;
while(l<r)
{
int mid=l+r+1>>1;
if(a[mid] <= x) l=mid;
else r=mid-1;
}
cout<<l<<endl;
}
//system("pause");
}