笔记
if (check(mid)) L = mid ,则 mid = L + R + 1 >> 1
if (check(mid)) R = mid ,则 mid = L + R >> 1
题目
#include<bits/stdc++.h>
using namespace std;
int n, q;
int a[100010];
int b[10010];
typedef pair<int, int> PII;
void search_binary(int x)
{
int mid = n >> 1;
int l = 0, r = n-1;
while(l < r)
{
if(a[mid] >= x) r = mid;
else l = mid + 1;
mid = l + r >> 1;
}
if(a[r] == x)
cout<<r<<' ';
else
{
cout<<-1<<' '<<-1<<'\n';
return;
}
l = 0;
r = n-1;
while(l < r)
{
mid = l + r + 1 >> 1;
if(a[mid] <= x) l = mid;
else r = mid - 1;
}
cout<<l<<'\n';
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>q;
for(int i=0;i<n;++i) cin>>a[i];
for(int i=0;i<q;++i) cin>>b[i];
for(int i=0;i<q;++i) search_binary(b[i]);
return 0;
}