整数二分
重点
1. 二分的时候,注意是求>=,还是<=,如果是<=, 则求mid的时候需要加1
2. if中的判断可以改成check,用来做局部性判断,这要求二分的序列需要有局部性质,一部分check=1,另一份check一定=0,
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
int q[N];
int main() {
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> q[i];
for (int i = 0; i < k; i++) {
int a;
cin >> a;
int l = 0, r = n - 1;
while (l < r) {
int mid = (l + r) >> 1;
if (q[mid] >= a) r = mid;
else l = mid + 1;
}
int t = l;
if (q[t] == a) {
cout << t << " ";
int l = 0, r = n - 1;
while (l < r) {
int mid = (l + r + 1) >> 1;
if (q[mid] <= a) l = mid;
else r = mid - 1;
}
cout << l << endl;
} else cout << "-1 -1" << endl;
}
return 0;
}