tien_noob

Mo's Algorithm

May 5th, 2021 (edited)
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.24 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int N = 3e4, Q = 2e5;
  4. int n, k, q, block;
  5. struct QE
  6. {
  7.     int l, r, id;
  8.     bool operator < (const QE & other) const
  9.     {
  10.         return l/k < other.l/k || (l/k == other.l/k && r < other.r);
  11.     }
  12. };
  13. QE Query[Q + 1];
  14. int a[N + 1], ans[Q + 1], res, Left, Right;
  15. map<int, int> Container;
  16. void read()
  17. {
  18.    cin >> n; k = sqrt(n); block = -1;
  19.    for (int i = 1; i <= n; ++ i)
  20.    {
  21.        cin >> a[i];
  22.    }
  23.    cin >> q;
  24.    for (int i = 1; i <= q; ++ i)
  25.    {
  26.        cin >> Query[i].l >> Query[i].r;
  27.        Query[i].id = i;
  28.    }
  29.    sort(Query + 1, Query + q + 1);
  30. }
  31. void Refresh(int i)
  32. {
  33.     Left = Query[i].l; Right = Query[i].r;
  34.     res = 0;
  35.     Container.clear();
  36.     block = Query[i].l/k;
  37.     for (int j = Query[i].l; j <= Query[i].r; ++ j)
  38.     {
  39.         ++Container[a[j]];
  40.         if (Container[a[j]] == 1)
  41.         {
  42.             ++res;
  43.         }
  44.     }
  45. }
  46. void solve()
  47. {
  48.    for (int i = 1; i <= q; ++ i)
  49.    {
  50.        if (Query[i].l / k != block)
  51.        {
  52.            Refresh(i);
  53.        }
  54.        else
  55.        {
  56.            while (Left < Query[i].l)
  57.            {
  58.                --Container[a[Left]];
  59.                if (Container[a[Left]] == 0)
  60.                {
  61.                    --res;
  62.                }
  63.                ++Left;
  64.            }
  65.            while (Left > Query[i].l)
  66.            {
  67.                --Left;
  68.                ++Container[a[Left]];
  69.                if (Container[a[Left]] == 1)
  70.                {
  71.                    ++res;
  72.                }
  73.            }
  74.            while (Right > Query[i].r)
  75.            {
  76.                --Container[a[Right]];
  77.                if (Container[a[Right]] == 0)
  78.                {
  79.                    --res;
  80.                }
  81.                --Right;
  82.            }
  83.            while (Right < Query[i].r)
  84.            {
  85.                ++Right;
  86.                ++Container[a[Right]];
  87.                if (Container[a[Right]] == 1)
  88.                {
  89.                    ++res;
  90.                }
  91.            }
  92.        }
  93.        ans[Query[i].id] = res;
  94.    }
  95.    for (int i = 1; i <= q; ++ i)
  96.    {
  97.        cout << ans[i] << '\n';
  98.    }
  99. }
  100. int main()
  101. {
  102.     ios_base::sync_with_stdio(false);
  103.     cin.tie(nullptr);
  104.     read();
  105.     solve();
  106. }
  107.  
Add Comment
Please, Sign In to add comment