Salvens

3D MO

Aug 14th, 2023
910
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <cmath>
  4. #include <string>
  5. #include <algorithm>
  6. #include <iomanip>
  7. #include <map>
  8. #include <set>
  9. #include <bitset>
  10. #include <fstream>
  11. #include <unordered_set>
  12. #include <unordered_map>
  13. #include <ext/pb_ds/assoc_container.hpp>
  14. #include <stack>
  15. #include <queue>
  16. #include <complex>
  17.  
  18.  
  19. using namespace std;
  20. using namespace __gnu_pbds;
  21.  
  22.  
  23.  
  24. /*#pragma GCC optimize("Ofast")
  25. #pragma GCC optimize("no-stack-protector")
  26. #pragma GCC optimize("unroll-loops")
  27. #pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
  28. #pragma GCC optimize("fast-math")*/
  29.  
  30. /*
  31. #pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,tune=native")
  32. #pragma GCC target("avx2")
  33.  #pragma GCC optimize("Ofast")
  34. #pragma GCC optimization("unroll-loops")
  35.  #pragma GCC optimization("O3")
  36. */
  37.  
  38. //#define int long long
  39. #define ll long long
  40. //#define int short int
  41. #define eb emplace_back
  42. #define pb push_back
  43. #define ld long double
  44. //#define ld double
  45. #define mp make_pair
  46. #define f first
  47. #define s second
  48. #define gleg(x) return cout << x, 0
  49. #define pii pair <int, int>
  50. #define deb(a) cerr << #a << " = " << a << '\n';
  51. //#define sz(x) (int)s.size();
  52. #define fast() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout << fixed << setprecision(9);     cerr << fixed << setprecision(12); }
  53.  
  54. template < typename firstType, typename secondType = null_type, class compare = less < firstType > >
  55. struct sett {
  56.     typedef tree <
  57.             firstType,
  58.             secondType,
  59.             compare,
  60.             rb_tree_tag,
  61.             tree_order_statistics_node_update
  62.     > _ ;
  63. };
  64.  
  65.  
  66. const int INF = 1e9 + 7;
  67. const ld EPS = 1e-9;
  68. const int MAXI = 350;
  69. const int MOD = 998244353;
  70. const int MOD1 = 1e9 + 7;
  71. const int MAXST = 2000000;
  72. const int P = 31;
  73. const int P1 = 37;
  74. const int N = 5100000;
  75. const ld PI = 3.14159265358979323;
  76. const int bp = 23;
  77. //const int sz = 1 << bp;
  78.  
  79.  
  80. ostream &operator<<(ostream &stream, const pair <int, int> &p) {
  81.     stream << p.first << ' ' << p.second << ' ';
  82.     return stream;
  83. }
  84.  
  85. struct rq{
  86.     int l, r, tm;
  87.  
  88.     rq(int _l, int _r, int _tm){
  89.         l = _l;
  90.         r = _r;
  91.         tm = _tm;
  92.     }
  93.  
  94. };
  95.  
  96. struct u {
  97.     bool f;
  98.     int pos, prx, x;
  99.  
  100.     u(){
  101.         f = false;
  102.     }
  103.  
  104.     u(int _pos, int _prx, int _x){
  105.         f = true;
  106.         pos  = _pos;
  107.         prx = _prx;
  108.         x = _x;
  109.     }
  110. };
  111.  
  112. int k;
  113.  
  114. bool cmp(rq &a, rq &b){
  115.     if (a.l / k == b.l / k){
  116.         if (a.r / k == b.r / k)
  117.             return a.tm < b.tm;
  118.         return ((a.r * (a.l / k % 2 == 1 ? -1 : 1)) < (b.r * (a.l / k % 2 == 1 ? -1 : 1)));
  119.     }
  120.     return a.l < b.l;
  121. }
  122.  
  123. int cnt[310000], mx[310000];
  124.  
  125. void del(int x){
  126.     if (cnt[x] == 1){
  127.         mx[cnt[x]]--;
  128.         cnt[x]--;
  129.     }
  130.     else{
  131.         mx[cnt[x]]--;
  132.         cnt[x]--;
  133.         mx[cnt[x]]++;
  134.     }
  135. }
  136.  
  137. void add(int x){
  138.     if (cnt[x] == 0){
  139.         cnt[x]++;
  140.         mx[cnt[x]]++;
  141.     }
  142.     else{
  143.         mx[cnt[x]]--;
  144.         cnt[x]++;
  145.         mx[cnt[x]]++;
  146.     }
  147. }
  148.  
  149. signed main(){
  150.     fast();
  151.     srand(time(0));
  152.     /*#ifdef _LOCAL
  153.         freopen("input.txt", "r", stdin);
  154.         freopen("output.txt", "w", stdout);
  155.     #endif*/
  156.     int n, m;
  157.     cin >> n >> m;
  158.     vector <int> a(n);
  159.     unordered_map <int, int> p;
  160.  
  161.     for (int i = 0; i < n; i++){
  162.         cin >> a[i];
  163.         if (p.find(a[i]) == p.end())
  164.             p[a[i]] = p.size();
  165.         a[i] = p[a[i]];
  166.     }
  167.     vector <int> a1 = a;
  168.  
  169.     vector <rq> req;
  170.     vector <u> upd(m + 10, u());
  171.     for (int q = 0; q < m; q++){
  172.         int c, l, r;
  173.         cin >> c >> l >> r;
  174.  
  175.         if (c == 1){
  176.             l--, r--;
  177.             req.eb(rq(l, r, q));
  178.         } else {
  179.             l--;
  180.             if (p.find(r) == p.end())
  181.                 p[r] = p.size();
  182.  
  183.             r = p[r];
  184.             upd[q] = u(l, a1[l], r);
  185.             a1[l] = r;
  186.         }
  187.     }
  188.  
  189.     k = 3500;
  190.  
  191.     sort(req.begin(), req.end(), cmp);
  192.  
  193.     int l = 0, r = -1, t = -1;
  194.  
  195.     vector <pair <int, int>> ans;
  196.     //sqmex st = sqmex(n + m);
  197.  
  198.  
  199.     for (int q = 0; q < (int)req.size(); q++){
  200.         int l1 = req[q].l, r1 = req[q].r, tm = req[q].tm;
  201.  
  202.         while (r < r1){
  203.             r++;
  204.             add(a[r]);
  205.         }
  206.  
  207.         while (l > l1){
  208.             l--;
  209.             add(a[l]);
  210.         }
  211.  
  212.         while (r > r1){
  213.             del(a[r]);
  214.             r--;
  215.         }
  216.  
  217.         while (l < l1){
  218.             del(a[l]);
  219.             l++;
  220.         }
  221.  
  222.         while (t < tm) {
  223.             t++;
  224.             if (upd[t].f) {
  225.                 if (l <= upd[t].pos && r >= upd[t].pos) {
  226.                     del(a[upd[t].pos]);
  227.                     add(upd[t].x);
  228.                 }
  229.  
  230.                 a[upd[t].pos] = upd[t].x;
  231.             }
  232.         }
  233.  
  234.         while (t > tm) {
  235.             if (upd[t].f) {
  236.                 if (l <= upd[t].pos && r >= upd[t].pos){
  237.                     del(a[upd[t].pos]);
  238.                     add(upd[t].prx);
  239.                 }
  240.  
  241.                 a[upd[t].pos] = upd[t].prx;
  242.             }
  243.  
  244.             t--;
  245.         }
  246.  
  247.         int cur;
  248.         for (int i = 1; i < (int)3e5; i++){
  249.             if (mx[i] == 0) {
  250.                 cur = i;
  251.                 break;
  252.             }
  253.         }
  254.         ans.pb({req[q].tm, cur});
  255.     }
  256.  
  257.     sort(ans.begin(), ans.end());
  258.  
  259.     for (auto i: ans)
  260.         cout << i.s << '\n';
  261.  
  262.     return 0;
  263. }
Advertisement
Add Comment
Please, Sign In to add comment