二分模板题。

#include<bits/stdc++.h>
#define int long long
#define double long double
#define x first
#define y second
using namespace std;
typedef long long LL;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 3e5 + 10;
const int M = 1e3 + 10;
int mod = 1e9 + 7;
int a[N];

void solve() {
    int n, q;
    cin >> n >> q;
    for (int i = 0; i < n; i++)cin >> a[i];
    while (q--) {
        int op, l, r, x;
        cin >> op >> l >> r >> x;
        if (op == 1) {
            int k = lower_bound(a + l, a + r , x) - a;
            if (k < r&&k>=l) cout << a[k] << "\n";
            else cout << -1 << "\n";
        } else if (op == 2) {
            int k = upper_bound(a + l, a + r , x) - a;
            if (k < r&&k>=l) cout << a[k] << "\n";
            else cout << -1 << "\n";
        } else if (op == 3) {
            int k = lower_bound(a + l, a + r , x) - a - 1;
            if (k < r&&k>=l) cout << a[k] << "\n";
            else cout << -1 << "\n";
        } else {
            int k = upper_bound(a + l, a + r , x) - a - 1;
            if (k < r&&k>=l) cout << a[k] << "\n";
            else cout << -1 << "\n";
        }
    }
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    int _;
    _ = 1;
    //cin>>_;
    while (_--) {
        solve();
    }
}