题目描述
https://www.acwing.com/problem/content/791/
C++ 代码
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int n,m;
int q[100010];
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&q[i]);
while(m--)
{
int x;
scanf("%d",&x);
int l=1,r=n;
while(l<r)
{
int mid=(l+r)>>1;
if(q[mid]>=x) r=mid;
else l=mid+1;
}
if(q[l]!=x) cout<<"-1 -1"<<endl;
else
{
cout<<l-1<<' ';
int l=1,r=n;
while(l<r)
{
int mid=(l+r+1)>>1;
if(q[mid]<=x) l=mid;
else r=mid-1;
}
cout<<l-1<<endl;
}
}
return 0;
}