模板,按照题意模拟即可,记得讨论清楚边界
#include <bits/stdc++.h> using namespace std; #define int long long const int N = 2e5 + 5; int __t = 1, n, q, op, l, r, x, k; void solve() { vector<int> a; cin >> n >> q; for (int i = 0; i < n; i++) cin >> x, a.push_back(x); while (q--) { cin >> op >> l >> r >> x; if (op == 1) k = lower_bound(a.begin() + l, a.begin() + r, x) - a.begin(); if (op == 2) k = upper_bound(a.begin() + l, a.begin() + r, x) - a.begin(); if (op == 3) k = lower_bound(a.begin() + l, a.begin() + r, x) - a.begin() - 1; if (op == 4) k = upper_bound(a.begin() + l, a.begin() + r, x) - a.begin() - 1; cout << ((k < r && k >= l) ? a[k] : -1) << "\n"; } } int32_t main() { #ifdef ONLINE_JUDGE ios::sync_with_stdio(false); cin.tie(0); #endif // cin >> __t; while (__t--) solve(); return 0; }