EntropyIncreaser

[CF] Machine Learning

Mar 14th, 2018
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.57 KB | None | 0 0
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <cstring>
  4.  
  5. #include <algorithm>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. struct Query {
  11.     int l, r, t, id;
  12.  
  13.     Query() {}
  14.  
  15.     Query(int l, int r, int t, int id) : l(l), r(r), t(t), id(id) {}
  16.  
  17.     bool operator<(const Query& rhs) const {
  18.         return t < rhs.t;
  19.     }
  20. };
  21.  
  22. const int N = 100010, N13 = 50;
  23.  
  24. int n, q, opCnt, bSize, bCnt;
  25. int a[N], ans[N];
  26. int opt[N], x[N], y[N];
  27. int opX[N], opS[N], opT[N];
  28. int c[N * 2], cc[N];
  29. int uniq[N * 2];
  30. vector<Query> qr[N13][N13];
  31.  
  32. void rmv(int x);
  33. void add(int x);
  34.  
  35. int main() {
  36.     scanf("%d%d", &n, &q);
  37.     for (int i = 1; i <= n; ++i)
  38.         scanf("%d", &a[i]);
  39.     for (int i = 1; i <= q; ++i)
  40.         scanf("%d%d%d", &opt[i], &x[i], &y[i]);
  41.  
  42.     bSize = pow(n, 2. / 3);
  43.     bCnt = (n + bSize - 1) / bSize;
  44.     memcpy(uniq, a, sizeof(a));
  45.     for (int i = 1; i <= q; ++i)
  46.         if (opt[i] == 1) {
  47.             qr[(x[i] - 1) / bSize + 1][(y[i] - 1) / bSize + 1].emplace_back(x[i], y[i], opCnt, i - opCnt);
  48.         } else {
  49.             ++opCnt;
  50.             uniq[n + opCnt] = y[i];
  51.         }
  52.     sort(uniq + 1, uniq + n + opCnt + 1);
  53.     int* lst = unique(uniq + 1, uniq + n + opCnt + 1);
  54.     for (int i = 1; i <= n; ++i)
  55.         a[i] = lower_bound(uniq + 1, lst, a[i]) - uniq;
  56.     opCnt = 0;
  57.     for (int i = 1; i <= q; ++i)
  58.         if (opt[i] == 2) {
  59.             ++opCnt;
  60.             y[i] = lower_bound(uniq + 1, lst, y[i]) - uniq;
  61.             opX[opCnt] = x[i];
  62.             opS[opCnt] = a[x[i]];
  63.             opT[opCnt] = y[i];
  64.             a[x[i]] = y[i];
  65.         }
  66.     for (int i = q; i; --i)
  67.         if (opt[i] == 2) {
  68.             a[x[i]] = opS[opCnt];
  69.             --opCnt;
  70.         }
  71.     opCnt = count(opt + 1, opt + q + 1, 2);
  72.     int lP = 1, rP = 0, tP = 0;
  73.     cc[0] = n;
  74.     for (int i = 1; i <= bCnt; ++i)
  75.         for (int j = 1; j <= bCnt; ++j) {
  76.             sort(qr[i][j].begin(), qr[i][j].end());
  77.             for (auto&& qry: qr[i][j]) {
  78.                 while (lP < qry.l)
  79.                     rmv(a[lP++]);
  80.                 while (lP > qry.l)
  81.                     add(a[--lP]);
  82.                 while (rP < qry.r)
  83.                     add(a[++rP]);
  84.                 while (rP > qry.r)
  85.                     rmv(a[rP--]);
  86.                 while (tP < qry.t) {
  87.                     ++tP;
  88.                     if (lP <= opX[tP] && opX[tP] <= rP)
  89.                         rmv(a[opX[tP]]);
  90.                     a[opX[tP]] = opT[tP];
  91.                     if (lP <= opX[tP] && opX[tP] <= rP)
  92.                         add(opT[tP]);
  93.                 }
  94.                 while (tP > qry.t) {
  95.                     if (lP <= opX[tP] && opX[tP] <= rP)
  96.                         rmv(a[opX[tP]]);
  97.                     a[opX[tP]] = opS[tP];
  98.                     if (lP <= opX[tP] && opX[tP] <= rP)
  99.                         add(opS[tP]);
  100.                     --tP;
  101.                 }
  102.                 int curAns = 1;
  103.                 while (cc[curAns])
  104.                     ++curAns;
  105.                 ans[qry.id] = curAns;
  106.             }
  107.         }
  108.     for (int i = 1; i <= q - opCnt; ++i)
  109.         printf("%d\n", ans[i]);
  110.     return 0;
  111. }
  112.  
  113. void add(int x) {
  114.     --cc[c[x]];
  115.     ++cc[++c[x]];
  116. }
  117.  
  118. void rmv(int x) {
  119.     --cc[c[x]];
  120.     ++cc[--c[x]];
  121. }
Add Comment
Please, Sign In to add comment