二分模板(l = mid.分界线有加1)
作者:
Oct_9
,
2024-03-28 01:08:15
,
所有人可见
,
阅读 8
#include <iostream>
using namespace std;
const int N = 100010;
int n, m;
int q[N];
int main()
{
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i ++) scanf("%d", &q[i]);
while (m --)
{
int x;
scanf("%d", &x);
int l = 0, r = n - 1;
while (l < r)
{
int mid = r + l >> 1;
if (q[mid] >= x) r = mid; // 前面mid 不加1
else l = mid + 1;
}
if (q[l] != x) cout << "-1 -1" << endl;
else
{
cout << l << " ";
int l = 0, r = n - 1;
while (l < r)
{
int mid =( r + l + 1) >> 1;
if (q[mid] <= x) l = mid; // l = mid, 前面mid = l + r + 1
else r = mid - 1;
}
cout << l << endl;
}
}
return 0;
}