最左边的x ----- >= x
最右边的x ------ <= x
x的前一个 ------- <x
x的后一个 ---------> x
#include<iostream> #include<cstring> #include<algorithm> using namespace std ; const int N = 100010 ; int a[N] ; int n , q ; int main(){ cin >> n >> q ; for(int i = 0 ; i < n ; i ++) cin >> a[i] ; while(q --){ int k ; cin >> k ; int l = 0 , r= n -1; 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 ; } else { cout << l << " " ; int l2 = 0 , r2 = n-1 ; while(l2 < r2) { int mid = l2 + r2 + 1 >> 1 ; if(a[mid] <= k) l2 = mid ; else r2 = mid - 1 ; } cout << l2 << endl ; } } return 0 ; }