AcWing 789. 数的范围
原题链接
简单
作者:
千禧
,
2020-04-04 12:23:19
,
所有人可见
,
阅读 530
C++ 代码
#include <iostream>
using namespace std;
const int N = 1e6 +10;
int x[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
int n,t,m;
cin >> n >> t;
for(int i = 0;i < n;i ++) cin >> x[i];
while(t--)
{
cin >> m;
int l = 0,r = n-1;
while(l < r)
{
int mid = l + r >> 1;
if(x[mid] >= m) r = mid;
else l = mid + 1;
}
int begin = r;
l = 0, r = n -1;
while(l < r)
{
int mid = r + l +1 >> 1;
if(x[mid] <= m) l = mid;
else r = mid -1;
}
int end = r;
if(x[r] != m) cout << "-1 -1" << endl;
else
cout << begin << " " << end << endl;
}
return 0;
}