AcWing 789. 数的范围
#include <bits/stdc++.h>
using namespace std;
const int N=1e6+10;
int q[N],mid;
int check_min(int ans[],int x,int l,int r){
while(l<r){
mid=l+r >> 1;
if(q[mid]>=x) r=mid;
else l=mid+1;
}
if(q[l]!=x) return -1;
return l;
}
int check_max(int ans[],int x,int l,int r){
while(l<r){
mid=l+r+1 >> 1;
if(q[mid]<=x) l=mid;
else r=mid-1;
}
if(q[l]!=x) return -1;
return l;
return l;
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
scanf("%d",&q[i]);
while(m--){
int x;
scanf("%d",&x);
cout<<check_min(q,x,0,n-1)<<" "<<check_max(q,x,0,n-1);
printf("\n");
}
return 0;
}
AcWing 789. 数的范围