题解 | 【模板】二分
#include <algorithm>
#include <iostream>
using namespace std;
using ll = long long;
const ll N = 1e5 + 5;
int main() {
int n, q, op;
cin >> n >> q;
ll l, r, x, idx, a[N] = {0};
for (int i = 0; i < n; i++) {
cin >> a[i];
}
while (q--) {
cin >> op >> l >> r >> x;
if(op%2){// 1 or 3
idx = lower_bound(a + l, a + r, x) - a;
}
else {
idx = upper_bound(a + l, a + r, x) - a;
}
if((op-1)/2)// 3 or 4
idx --;
if(idx >= l && idx < r)//左闭右开
cout << a[idx] << endl;
else
cout << -1 << endl;
}
return 0;
}

