0%

789. 数的范围

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// https://www.acwing.com/problem/content/791/
#include <iostream>
using namespace std;
const int maxn=100007;
int n,q,k,a[maxn];

int main()
{
cin>>n>>q;
for(int i=1;i<=n;i++) {
cin>>a[i];
}
for(int _=1;_<=q;_++) {
cin>>k;
int l=1,r=n;
while (l<r) {
int mid=l+r>>1;
if(a[mid]>=k) r=mid;
else l=mid+1;
}
if(a[l]!=k) {
cout<<"-1 -1"<<endl;
continue;
}
int l2=1,r2=n;
while (l2<r2) {
int mid=l2+r2+1>>1;
if(a[mid]<=k) l2=mid;
else r2=mid-1;
}
cout<<l-1<<" "<<l2-1<<endl;
}
return 0;
}
// AC https://www.acwing.com/problem/content/submission/code_detail/37312161/