Advertisement
skimono

Untitled

Oct 7th, 2023
998
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.12 KB | None | 0 0
  1. struct Node {
  2.     ll key, pr;
  3.     ll left = -1, right = -1, cnt;
  4.     ll sz;
  5. };
  6.  
  7. struct dd {
  8.     //Djinchuriki Djubi
  9.     vector <Node> nodes;
  10.     ll nodesNumber = 0;
  11.     ll kor = -1;
  12.     ll makeNewNode(ll x) {
  13.         nodes.push_back({ x, (int)rnd(), -1, -1, 1, 1 });
  14.         return nodesNumber++;
  15.     }
  16.     ll getSZ(ll v) { return (v >= 0) ? nodes[v].sz : 0; }
  17.     void recalculate(ll v) {
  18.         if (v >= 0) {
  19.             nodes[v].sz = getSZ(nodes[v].left) + getSZ(nodes[v].right) + nodes[v].cnt;
  20.         }
  21.     }
  22.     bool exists(ll root, ll x) {
  23.         ll v = root;
  24.         while (v != -1) {
  25.             Node& nd = nodes[v];
  26.             if (nd.key == x)
  27.                 return true;
  28.             else if (nd.key > x)
  29.                 v = nodes[v].left;
  30.             else
  31.                 v = nodes[v].right;
  32.         }
  33.         return false;
  34.     }
  35.     pair <ll, ll> split(ll root, ll x) {
  36.         if (root == -1)
  37.             return { -1,-1 };
  38.         if (x < nodes[root].key) {
  39.             pair <ll, ll> p = split(nodes[root].left, x);
  40.             nodes[root].left = p.second;
  41.             recalculate(root);
  42.             return { p.first, root };
  43.         }
  44.         else {
  45.             pair <ll, ll> p = split(nodes[root].right, x);
  46.             nodes[root].right = p.first;
  47.             recalculate(root);
  48.             return { root,p.second };
  49.         }
  50.     }
  51.     ll merge(ll root1, ll root2) {
  52.         if (root2 == -1) {
  53.             return root1;
  54.         }
  55.         if (root1 == -1) {
  56.             return root2;
  57.         }
  58.         if (nodes[root1].pr > nodes[root2].pr) {
  59.             nodes[root1].right = merge(nodes[root1].right, root2);
  60.             recalculate(root1);
  61.             return root1;
  62.         }
  63.         else {
  64.             nodes[root2].left = merge(root1, nodes[root2].left);
  65.             recalculate(root2);
  66.             return root2;
  67.         }
  68.     }
  69.     ll insert(ll root, ll x) {
  70.         if (exists(root, x)) {
  71.             pair <ll, ll> p = split(root, x);
  72.             pair <ll, ll> pp = split(p.first, x - 1);
  73.             nodes[pp.second].cnt++;
  74.             nodes[pp.second].sz++;
  75.             return merge(merge(pp.first, pp.second), p.second);
  76.         }
  77.         else {
  78.             ll newNode = makeNewNode(x);
  79.             pair <ll, ll> p = split(root, x);
  80.             return merge(p.first, merge(newNode, p.second));
  81.         }
  82.     }
  83.     ll erase(ll root, ll x) {
  84.         if (!exists(root, x)) {
  85.             return root;
  86.         }
  87.         else {
  88.             pair <ll, ll> p = split(root, x);
  89.             pair <ll, ll> pp = split(p.first, x - 1);
  90.             nodes[pp.second].cnt--;
  91.             nodes[pp.second].sz--;
  92.             if (nodes[pp.second].cnt == 0) {
  93.                 return merge(pp.first, p.second);
  94.             }
  95.             else {
  96.                 return merge(merge(pp.first, pp.second), p.second);
  97.             }
  98.         }
  99.     }
  100.     ll prev(ll root, ll x) {
  101.         ll v = root, res = -inf, ans = 0;
  102.         while (v != -1) {
  103.             Node& nd = nodes[v];
  104.             if (nd.key > x) {
  105.                 v = nd.left;
  106.             }
  107.             else {
  108.                 if (nd.key == x) {
  109.                     ans += getSZ(nd.left);
  110.                 }
  111.                 else {
  112.                     ans += getSZ(nd.left) + nd.cnt;
  113.                 }
  114.                 v = nd.right;
  115.             }
  116.         }
  117.         return ans;
  118.     }
  119. };
  120.  
  121. struct fnw {
  122.     vector <dd> fn;//Fenwick
  123.     //I want to take vseros
  124.     void build(ll n) {
  125.         fn.resize(n + 4);
  126.     }
  127.     ll sump(ll r, ll x) {
  128.         ll ans = 0;
  129.         for (r; r > 0; r -= r & -r) {
  130.             ans += fn[r].prev(fn[r].kor, x);
  131.         }
  132.         return ans;
  133.     }
  134.     ll sum(ll l, ll r, ll x) {
  135.         // Кол-во элементов с l to r меньших x
  136.         return sump(r, x) - sump(l - 1, x);
  137.     }
  138.     void add(ll r, ll val) {
  139.         for (r; r < fn.size(); r += r & -r) {
  140.             if (val > 0) {
  141.                 fn[r].kor = fn[r].insert(fn[r].kor, val);
  142.             }
  143.             else {
  144.                 fn[r].kor = fn[r].erase(fn[r].kor, -val);
  145.             }
  146.         }
  147.     }
  148. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement