Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ll long long
- #define ull unsigned long long
- using namespace std;
- int main() {
- ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
- freopen("mex.in", "r", stdin);
- ll t;
- cin >> t;
- while (t--) {
- ll n,q;
- cin >> n >> q;
- vector<ll>v(n);
- for (auto &i:v) cin >> i;
- vector<ll>vv;
- while (q--) {
- ll l,r;
- cin >> l >> r;
- vv.resize(r-l+1);
- for (int i=l-1; i<r; i++) {
- vv[i-l+1]=v[i];
- }
- sort (vv.begin(), vv.end());
- l=0; r=vv.size();
- if (vv[0]>1) cout << 1 << endl;
- else if (vv[r-1]==r) cout << r+1 << endl;
- else {
- r--;
- ll mid;
- while (r>=l) {
- mid=(l+r)/2;
- if (vv[mid]>mid+1) r=mid-1;
- else if (vv[mid]==mid+1) l=mid+1;
- }
- cout << vv[mid-1]+1 << endl;
- }
- }
- }
- return 0;
- }
- /*
- * 3 -2 2 -7 16 -5 17 -13 22 -25 36 -40
- */
Advertisement
Add Comment
Please, Sign In to add comment