Advertisement
ivnikkk

Untitled

Sep 23rd, 2022
951
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.19 KB | None | 0 0
  1. // clang-format off
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include "bits/stdc++.h"
  4. using namespace std;
  5. #define all(a) a.begin(), a.end()
  6. #define int long long
  7. mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
  8. typedef long double ld;
  9.  
  10. const int c = 2200;
  11.  
  12. struct Query {
  13.     int l, r, t;
  14.     Query(int _l, int _r, int _t) : l(_l), r(_r), t(_t) {}
  15.    
  16.     bool operator<(const Query& other) {
  17.         if (l / c == other.l / c) {
  18.             if (r / c == other.r / c) {
  19.                 if ((l / c + r / c) & 1) {
  20.                     return t > other.t;
  21.                 }
  22.                 return t < other.t;
  23.             }
  24.             if ((l / c) & 1) {
  25.                 return r > other.r;
  26.             }
  27.             return r < other.r;
  28.         }
  29.         return l < other.l;
  30.     }
  31. };
  32.  
  33. struct Condition {
  34.     int ind = -1, last = -1, now = -1;
  35.     bool upd = false;
  36.     Condition(int _i, int _l, int _n, bool u) : ind(_i), last(_l), now(_n), upd(u) { }
  37. };
  38.  
  39. struct Set_sq {
  40.     const int sq_2 = 600, sq_4 = 50;
  41.     int n;
  42.     vector<int> cnt, mark_st, st, st_sq;
  43.     Set_sq(int _n) : n(_n) {
  44.         cnt.resize(n);
  45.         mark_st.resize(sq_2, 0); mark_st[0] = 1;
  46.         st.resize(sq_2, 0); st[0] = 1;
  47.         st_sq.resize(sq_4, 0); st_sq[0] = 1;
  48.     }
  49.     void erase(int x) {
  50.         if(cnt[x] && cnt[x] < sq_2) {
  51.             mark_st[cnt[x]]--;
  52.             if(!mark_st[cnt[x]]){
  53.                 st[cnt[x]] = 0;
  54.                 st_sq[cnt[x] / sq_4]--;
  55.             }
  56.         }
  57.         cnt[x]--;
  58.         if (cnt[x] && cnt[x] < sq_2) {
  59.             mark_st[cnt[x]]++;
  60.             if(mark_st[cnt[x]] == 1) {
  61.                 st[cnt[x]] = 1;
  62.                 st_sq[cnt[x] / sq_4]++;
  63.             }
  64.         }
  65.     }
  66.     void insert(int x) {
  67.         if (cnt[x] && cnt[x] < sq_2) {
  68.             mark_st[cnt[x]]--;
  69.             if (!mark_st[cnt[x]]) {
  70.                 st[cnt[x]] = 0;
  71.                 st_sq[cnt[x] / sq_4]--;
  72.             }
  73.         }
  74.         cnt[x]++;
  75.         if (cnt[x] && cnt[x] < sq_2) {
  76.             mark_st[cnt[x]]++;
  77.             if (mark_st[cnt[x]] == 1) {
  78.                 st[cnt[x]] = 1;
  79.                 st_sq[cnt[x] / sq_4]++;
  80.             }
  81.         }
  82.     }
  83.     int get_fst() {
  84.         int it = 0;
  85.         while(st_sq[it] == sq_4) { it++; }
  86.         int res = it * sq_4;
  87.         while(st[res] == 1) { res++; }
  88.         return res;
  89.     }
  90. };
  91. signed main() {
  92. #ifdef _DEBUG
  93.     freopen("input.txt", "r", stdin);
  94.     freopen("output.txt", "w", stdout);
  95. #endif
  96.     ios_base::sync_with_stdio(false);
  97.     cin.tie(nullptr);
  98.     cout.tie(nullptr);
  99.     int n, q;
  100.     cin >> n >> q;
  101.     vector<int> a(n), init_sub, compress;
  102.     for (int i = 0; i < n; i++) {
  103.         cin >> a[i];
  104.     }
  105.  
  106.     compress = init_sub = a;
  107.     vector<Condition> up; up.push_back(Condition(-1, -1, -1, false));
  108.     vector<Query> mo;
  109.  
  110.     for (int i = 0; i < q; i++) {
  111.         int type; cin >> type;
  112.         if (type == 1) {
  113.             int l, r; cin >> l >> r;
  114.             l--, r--;
  115.             mo.push_back(Query(l, r, i + 1));
  116.             up.push_back(Condition(-1, -1, -1, false));
  117.         }
  118.         if (type == 2) {
  119.             int p, x; cin >> p >> x;
  120.             p--;
  121.             up.push_back(Condition(p, init_sub[p], x, true));
  122.             init_sub[p] = x;
  123.             compress.push_back(x);
  124.         }
  125.     }
  126.  
  127.  
  128.     {
  129.         sort(all(compress)); compress.resize(unique(all(compress)) - compress.begin());
  130.         for (int i = 0; i < n; i++) {
  131.             a[i] = lower_bound(all(compress), a[i]) - compress.begin();
  132.         }
  133.         for (int i = 0; i < (int)up.size(); i++) {
  134.             if (!up[i].upd) { continue; }
  135.             up[i].last = lower_bound(all(compress), up[i].last) - compress.begin();
  136.             up[i].now = lower_bound(all(compress), up[i].now) - compress.begin();
  137.         }
  138.     }
  139.  
  140.     init_sub.clear();
  141.     Set_sq U((int)compress.size() + 1);
  142.     sort(all(mo));
  143.    
  144.     auto add = [&](int x) {
  145.         U.insert(x);
  146.     };
  147.     auto del = [&](int x) {
  148.         U.erase(x);
  149.     };
  150.  
  151.     vector<int> ans(q, -1);
  152.     int l = 0, r = -1, time = -1;
  153.     for (const Query& i : mo) {
  154.         while (time < i.t) {
  155.             Condition& recalc = up[++time];
  156.             if (recalc.upd) {
  157.                 if(recalc.ind >= l && recalc.ind <= r){
  158.                     del(recalc.last);
  159.                     add(recalc.now);
  160.                 }
  161.                 a[recalc.ind] = recalc.now;
  162.             }
  163.         }
  164.         while (time > i.t) {
  165.             Condition& recalc = up[time--];
  166.             if (recalc.upd) {
  167.                 if (recalc.ind >= l && recalc.ind <= r) {
  168.                     del(recalc.now);
  169.                     add(recalc.last);
  170.                 }
  171.                 a[recalc.ind] = recalc.last;
  172.             }
  173.         }
  174.  
  175.         while (l > i.l) {
  176.             add(a[--l]);
  177.         }
  178.         while (r < i.r) {
  179.             add(a[++r]);
  180.         }
  181.         while (l < i.l) {
  182.             del(a[l++]);
  183.         }
  184.         while (r > i.r) {
  185.             del(a[r--]);
  186.         }
  187.  
  188.         ans[i.t - 1] = U.get_fst();
  189.     }
  190.  
  191.     for (int i = 0; i < q; i++) {
  192.         if(ans[i] == -1) { continue; }
  193.         cout << ans[i] << '\n';
  194.     }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement