lina_os

Untitled

Apr 25th, 2025
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define ull unsigned long long
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  9. freopen("mex.in", "r", stdin);
  10. ll t;
  11. cin >> t;
  12. while (t--) {
  13. ll n,q;
  14. cin >> n >> q;
  15. vector<ll>v(n);
  16. for (auto &i:v) cin >> i;
  17. vector<ll>vv;
  18. while (q--) {
  19. ll l,r;
  20. cin >> l >> r;
  21. vv.resize(r-l+1);
  22. for (int i=l-1; i<r; i++) {
  23. vv[i-l+1]=v[i];
  24. }
  25. sort (vv.begin(), vv.end());
  26. l=0; r=vv.size();
  27. if (vv[0]>1) cout << 1 << endl;
  28. else if (vv[r-1]==r) cout << r+1 << endl;
  29. else {
  30. r--;
  31. ll mid;
  32. while (r>=l) {
  33. mid=(l+r)/2;
  34. if (vv[mid]>mid+1) r=mid-1;
  35. else if (vv[mid]==mid+1) l=mid+1;
  36. }
  37. cout << vv[mid-1]+1 << endl;
  38. }
  39. }
  40. }
  41. return 0;
  42. }
  43. /*
  44. * 3 -2 2 -7 16 -5 17 -13 22 -25 36 -40
  45. */
  46.  
  47.  
Advertisement
Add Comment
Please, Sign In to add comment