Advertisement
Dang_Quan_10_Tin

BALLDROP

Feb 14th, 2022
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.19 KB | None | 0 0
  1. #define task "E"
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <vector>
  6. #include <set>
  7. #include <queue>
  8. #include <deque>
  9. #include <algorithm>
  10. #include <cstring>
  11. #include <functional>
  12.  
  13. using namespace std;
  14.  
  15. using ll = long long;
  16. using ld = long double;
  17.  
  18. constexpr int N = 1e6 + 5;
  19. constexpr int Inf = 1e9 + 7;
  20.  
  21. struct FenwickTree
  22. {
  23.     int n;
  24.     ll a[N];
  25.  
  26.     FenwickTree()
  27.     {
  28.         memset(a, 0, sizeof a);
  29.     }
  30.  
  31.     void Update(int p, ll v)
  32.     {
  33.         for (; p <= n; p += p & -p)
  34.             a[p] += v;
  35.     }
  36.  
  37.     ll Get(int p)
  38.     {
  39.         ll ans(0);
  40.         for (; p > 0; p -= p & -p)
  41.             ans += a[p];
  42.         return ans;
  43.     }
  44.  
  45.     ll Get(int l, int r)
  46.     {
  47.         return Get(r) - Get(l - 1);
  48.     }
  49. } g;
  50.  
  51. struct Edge
  52. {
  53.     int x, y, id;
  54.     Edge(int x = 0, int y = 0, int id = 0) : x(x), y(y), id(id) {}
  55. };
  56.  
  57. vector<Edge> st[N], res;
  58.  
  59. struct Queries
  60. {
  61.     int t, x, y;
  62. } s[N];
  63.  
  64. int n, m, q, p[N], id[N], in[N], pos[N];
  65.  
  66. void Read()
  67. {
  68.     cin >> n >> m;
  69.     g.n = n;
  70.  
  71.     for (int i = 1; i <= m; ++i)
  72.     {
  73.         cin >> p[i];
  74.         st[i].emplace_back(p[i], p[i] + 1, Inf);
  75.     }
  76.  
  77.     cin >> q;
  78.  
  79.     for (int i = 1; i <= q; ++i)
  80.         cin >> s[i].t >> s[i].x >> s[i].y;
  81. }
  82.  
  83. void Swap(int x, int y)
  84. {
  85.     swap(id[x], id[y]);
  86.     in[id[x]] = x;
  87.     in[id[y]] = y;
  88. }
  89.  
  90. void DAC(int l, int r)
  91. {
  92.     if (l == r)
  93.         return;
  94.     int mid = (l + r) / 2;
  95.     DAC(l, mid);
  96.     DAC(mid + 1, r);
  97.  
  98.     for (int i = mid; i >= l; --i)
  99.         Swap(res[i].x, res[i].y);
  100.  
  101.     vector<Edge> tmp;
  102.  
  103.     for (int i = l, j = mid + 1; i <= mid || j <= r;)
  104.     {
  105.         if (j > r)
  106.         {
  107.             tmp.emplace_back(res[i]);
  108.             Swap(res[i].x, res[i].y);
  109.             ++i;
  110.         }
  111.         else if (i > mid)
  112.         {
  113.             tmp.emplace_back(in[res[j].x], in[res[j].y], res[j].id);
  114.             ++j;
  115.         }
  116.         else if (res[i].id >= res[j].id)
  117.         {
  118.             tmp.emplace_back(res[i]);
  119.             Swap(res[i].x, res[i].y);
  120.             ++i;
  121.         }
  122.         else
  123.         {
  124.             tmp.emplace_back(in[res[j].x], in[res[j].y], res[j].id);
  125.             ++j;
  126.         }
  127.     }
  128.  
  129.     for (int i = r - l; ~i; --i)
  130.         res[l + i] = tmp[i];
  131. }
  132.  
  133. void Solve()
  134. {
  135.     for (int i = 1; i <= q; ++i)
  136.         if (s[i].t == 1)
  137.         {
  138.             st[s[i].x].back().id = i;
  139.             p[s[i].x] = s[i].y;
  140.  
  141.             st[s[i].x].emplace_back(s[i].y, s[i].y + 1, i);
  142.             st[s[i].x].emplace_back(s[i].y, s[i].y + 1, Inf);
  143.         }
  144.  
  145.     for (int i = 1; i <= n; ++i)
  146.         id[i] = in[i] = pos[i] = i;
  147.  
  148.     vector<Edge> tmp;
  149.  
  150.     for (int i = m; i; --i)
  151.         for (auto j : st[i])
  152.             tmp.emplace_back(j);
  153.  
  154.  
  155.     for (auto i = tmp.rbegin(); i != tmp.rend(); ++i)
  156.     {
  157.         if (i->id < Inf)
  158.             res.emplace_back(id[i->x], id[i->y], i->id);
  159.         Swap(i->x, i->y);
  160.     }
  161.  
  162.     for (int i = 1; i <= n; ++i)
  163.         id[i] = in[i] = pos[i] = i;
  164.  
  165.     DAC(0, res.size() - 1);
  166.  
  167.     reverse(res.begin(), res.end());
  168.  
  169.     for (auto i = tmp.rbegin(); i != tmp.rend(); ++i)
  170.         if (i->id == Inf)
  171.             res.emplace_back(*i);
  172.  
  173.     reverse(res.begin(), res.end());
  174.  
  175.     for (auto i : res)
  176.         Swap(i.x, i.y);
  177.  
  178.     for (int i = 1; i <= n; ++i)
  179.         g.Update(id[i], 1ll * i * i);
  180.  
  181.     for (int i = 1; i <= q; ++i)
  182.     {
  183.         while (!res.empty() && res.back().id == i)
  184.         {
  185.             g.Update(id[res.back().x], -1ll * res.back().x * res.back().x);
  186.             g.Update(id[res.back().y], -1ll * res.back().y * res.back().y);
  187.  
  188.             swap(id[res.back().x], id[res.back().y]);
  189.  
  190.             g.Update(id[res.back().x], 1ll * res.back().x * res.back().x);
  191.             g.Update(id[res.back().y], 1ll * res.back().y * res.back().y);
  192.  
  193.             res.pop_back();
  194.         }
  195.  
  196.         if (s[i].t == 2)
  197.             cout << g.Get(s[i].x, s[i].y) << "\n";
  198.     }
  199. }
  200.  
  201. int32_t main()
  202. {
  203.     ios::sync_with_stdio(0);
  204.     cin.tie(0);
  205.     cout.tie(0);
  206.     if (fopen(task ".INP", "r"))
  207.     {
  208.         freopen(task ".INP", "r", stdin);
  209.         freopen(task ".OUT", "w", stdout);
  210.     }
  211.  
  212.     {
  213.         Read();
  214.         Solve();
  215.     }
  216. }
  217.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement