二分 时间复杂度Ologn
代码
include [HTML_REMOVED]
using namespace std;
const int maxn=1e5+5;
int n,m,q[maxn];
int bsearchr(int l,int r,int k){
while(l[HTML_REMOVED]>1;
if(q[mid]>=k)r=mid;
else l=mid+1;
}
return r;
}
int bsearchl(int l,int r,int k){
while(l[HTML_REMOVED]>1;
if(q[mid]<=k)l=mid;
else r=mid-1;
}
return l;
}
int main()
{
int k,l,r;
scanf(“%d%d”,&n,&m);
for(int i=0;i<n;i)scanf(“%d”,&q[i]);
for(int i=0;i<m;i){
scanf(“%d”,&k);
r=bsearchl(0,n-1,k);
if(q[r]!=k)printf(“-1 -1\n”);
else {
l=bsearchr(0,n-1,k);
printf(“%d %d\n”,l,r);
}
}
return 0;
}
就一个找左端点一个右端点就完了