AcWing 789. 数的范围--简单明了 C++
原题链接
简单
作者:
I+III
,
2021-05-28 13:57:51
,
所有人可见
,
阅读 322
#include<bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, q;
int a[N];
int getLeft(int x){
int l = 0, r = n - 1;
while(l < r){
int mid = l + (r - l) / 2;
if(a[mid] >= x) r = mid;
else if(a[mid] < x) l = mid + 1;
}
return a[l] == x ? l : -1;
}
int getRight(int st, int x){
int r = st;
do{
r++;
}
while(a[r] == x);
return st == -1 ? -1 : r - 1;
}
int main(){
cin>>n>>q;
for(int i = 0; i < n; i++) cin>>a[i];
while(q--){
int x, st, ed;
cin>>x;
st = getLeft(x), ed = getRight(st, x);
cout<<st<<" "<<ed<<endl;
}
return 0;
}