Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2023
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.28 KB | None | 0 0
  1. [CODE]#include <iostream>
  2.  
  3.  
  4. using namespace std;
  5. }
  6. int64_t GetSumm(int64_t k1, int64_t k2);
  7. private:
  8.     void Splay(Node* node);
  9.     void Zig(Node* node);
  10.     void Zag(Node* node);
  11.     void ZigZig(Node* node);
  12.     void ZigZag(Node* node);
  13.     void ZagZig(Node* node);
  14.     void ZagZag(Node* node);
  15.  
  16.     void Merge(Node* v1, Node* v2);
  17.     Node* root = nullptr; };
  18.     void SplayTree::Insert(int64_t key) {
  19.         if (root == nullptr) {
  20.             root = new Node(key); return;
  21.         }
  22.         Node* current = root; while (true) {
  23.             if (current->value == key) {
  24.                 return;
  25.             }
  26.             if (current->value < key) {
  27.                 if (current->right != nullptr) {
  28.                     current = current->right; continue;
  29.                 }
  30.                 else {
  31.                     current->right = new Node(key); current->right->parrent = current; current = current->right;
  32.                     break;
  33.                 }
  34.             }
  35.             if (current->value > key) {
  36.                 if (current->left != nullptr) {
  37.                     current = current->left;
  38.                     continue;
  39.                 }
  40.                 else {
  41.  
  42.                     current->left = new Node(key); current->left->parrent = current; current = current->left;
  43.                     break;
  44.                 }
  45.             }
  46.         }
  47.         Node* prev = current->parrent; while (prev != nullptr) {
  48.             prev->sum += key;
  49.             prev = prev->parrent;
  50.         }
  51.         Splay(current);
  52.     }
  53.     bool SplayTree::Find(const int64_t value) {
  54.         if (root == nullptr) {
  55.             return false;
  56.         }
  57.         Node* current = root;
  58.         Node* prev;
  59.         while (current != nullptr && current->value != value) {
  60.             prev = current;
  61.             if (current->value < value) {
  62.                 current = current->right;
  63.             }
  64.             else {
  65.                 current = current->left;
  66.             }
  67.         }
  68.         if (current == nullptr) {
  69.             Splay(prev); return false;
  70.  
  71.         }
  72.         else {
  73.             Splay(current); return true;
  74.         }
  75.     }
  76.     void SplayTree::Remove(int64_t key) {
  77.         if (Find(key)) {
  78.             if (root->left != nullptr) {
  79.                 Node* c = root; root->left->parrent = nullptr; if (root->right != nullptr) {
  80.                     root->right->parrent = nullptr;
  81.                 }
  82.                 root = root->left; Merge(c->left, c->right); c->left = nullptr; c->right = nullptr; delete c;
  83.             }
  84.             else {
  85.                 if (root->right != nullptr) {
  86.                     root->right->parrent = nullptr; Node* c = root->right; root->right = nullptr;
  87.                     delete root; root = c;
  88.                 }
  89.                 else {
  90.                     delete root; root = nullptr;
  91.                 }
  92.             }
  93.         }
  94.  
  95.     }
  96.     void SplayTree::Splay(Node* node) {
  97.         if (node == root) {
  98.             return;
  99.         }
  100.         if (node->parrent == root) {
  101.             if (root->right == node) {
  102.                 Zag(node);
  103.                 return;
  104.             }
  105.             else {
  106.                 Zig(node);
  107.                 return;
  108.             }
  109.         }
  110.         Node* a = node->parrent, * b = node->parrent->parrent; if (b->left == a) {
  111.             if (a->left == node) {
  112.                 ZigZig(node);
  113.             }
  114.             else {
  115.                 ZagZig(node);
  116.             }
  117.         }
  118.         else {
  119.             if (a->left == node) {
  120.                 ZigZag(node);
  121.             }
  122.             else {
  123.                 ZagZag(node);
  124.             }
  125.         }
  126.         Splay(node);
  127.     }
  128.  
  129.     void SplayTree::Zig(Node* node) {
  130.         node->parrent->left = node->right; if (node->right != nullptr) {
  131.             node->right->parrent = node->parrent;
  132.         }
  133.         node->parrent->parrent = node; node->right = node->parrent; node->parrent = nullptr;
  134.         root = node;
  135.         node->sum += node->right->value; if (node->right->right != nullptr) {
  136.             node->sum += node->right->right->sum;
  137.         }
  138.         node->right->sum -= node->value; if (node->left != nullptr) {
  139.             node->right->sum -= node->left->sum;
  140.         }
  141.     }
  142.     void SplayTree::Zag(Node* node) {
  143.         node->parrent->right = node->left;
  144.         if (node->left != nullptr) {
  145.             node->left->parrent = node->parrent;
  146.         }
  147.         node->parrent->parrent = node; node->left = node->parrent; node->parrent = nullptr;
  148.         root = node;
  149.         node->sum += node->left->value; if (node->left->left != nullptr) {
  150.             node->sum += node->left->left->sum;
  151.  
  152.         }
  153.         node->left->sum -= node->value; if (node->right != nullptr) {
  154.             node->left->sum -= node->right->sum;
  155.         }
  156.     }
  157.     void SplayTree::ZigZig(Node* node) {
  158.         Node* a = node->parrent;
  159.         node->parrent = a->parrent->parrent; a->left = node->right;
  160.         if (node->right != nullptr) {
  161.             node->right->parrent = a;
  162.         }
  163.         node->right = a;
  164.         Node* b = a->parrent; a->parrent = node; b->left = a->right;
  165.         if (a->right != nullptr) {
  166.             a->right->parrent = b;
  167.         }
  168.         a->right = b; b->parrent = a; b->sum -= a->sum;
  169.         if (b->left != nullptr) {
  170.             b->sum += b->left->sum;
  171.         }
  172.         a->sum = b->sum + a->value; if (a->left != nullptr) {
  173.             a->sum += a->left->sum;
  174.         }
  175.         node->sum = a->sum + node->value;
  176.  
  177.         if (node->left != nullptr) {
  178.             node->sum += node->left->sum;
  179.         }
  180.         if (b == root) {
  181.             root = node;
  182.         }
  183.         else {
  184.             if (node->value < node->parrent->value) {
  185.                 node->parrent->left = node;
  186.             }
  187.             else {
  188.                 node->parrent->right = node;
  189.             }
  190.         }
  191.     }
  192.     void SplayTree::ZigZag(Node* node) {
  193.         Node* b = node->parrent, * a = b->parrent; node->parrent = a->parrent;
  194.         a->parrent = node;
  195.         a->right = node->left;
  196.         if (node->left != nullptr) {
  197.             node->left->parrent = a;
  198.         }
  199.         node->left = a;
  200.         b->left = node->right;
  201.         if (node->right != nullptr) {
  202.             node->right->parrent = b;
  203.         }
  204.         node->right = b; b->parrent = node; b->sum -= node->value; if (a->right != nullptr) {
  205.             b->sum -= a->right->sum;
  206.  
  207.         }
  208.         a->sum -= b->sum;
  209.         a->sum -= node->value;
  210.         node->sum = node->value + a->sum + b->sum; if (a == root) {
  211.             root = node;
  212.         }
  213.         else {
  214.             if (node->value < node->parrent->value) {
  215.                 node->parrent->left = node;
  216.             }
  217.             else {
  218.                 node->parrent->right = node;
  219.             }
  220.         }
  221.     }
  222.     void SplayTree::ZagZig(Node* node) {
  223.         Node* a = node->parrent, * b = a->parrent; a->right = node->left;
  224.         if (node->left != nullptr) {
  225.             node->left->parrent = a;
  226.         }
  227.         b->left = node->right;
  228.         if (node->right != nullptr) {
  229.             node->right->parrent = b;
  230.         }
  231.         a->parrent = node; node->parrent = b->parrent; b->parrent = node; node->right = b;
  232.         node->left = a;
  233.         b->sum -= a->sum;
  234.         if (b->left != nullptr) {
  235.  
  236.             b->sum += b->left->sum;
  237.             a->sum -= b->left->sum;
  238.         }
  239.         a->sum -= node->value;
  240.         node->sum = a->sum + b->sum + node->value; if (b == root) {
  241.             root = node;
  242.         }
  243.         else {
  244.             if (node->value < node->parrent->value) {
  245.                 node->parrent->left = node;
  246.             }
  247.             else {
  248.                 node->parrent->right = node;
  249.             }
  250.         }
  251.     }
  252.     void SplayTree::ZagZag(Node* node) {
  253.         Node* b = node->parrent, * a = b->parrent; a->right = b->left;
  254.         if (b->left != nullptr) {
  255.             b->left->parrent = a;
  256.         }
  257.         node->parrent = a->parrent; a->parrent = b;
  258.         b->left = a;
  259.         b->right = node->left;
  260.         if (node->left != nullptr) {
  261.             node->left->parrent = b;
  262.         }
  263.         b->parrent = node; node->left = b; a->sum -= b->sum;
  264.  
  265.         if (a->right != nullptr) {
  266.             a->sum += a->right->sum;
  267.         }
  268.         b->sum = b->value + a->sum; if (b->right != nullptr) {
  269.             b->sum += b->right->sum;
  270.             node->sum -= b->right->sum;
  271.         }
  272.         node->sum += b->sum; if (a == root) {
  273.             root = node;
  274.         }
  275.         else {
  276.             if (node->value < node->parrent->value) {
  277.                 node->parrent->left = node;
  278.             }
  279.             else {
  280.                 node->parrent->right = node;
  281.             }
  282.         }
  283.     }
  284.     void SplayTree::Merge(Node* v1, Node* v2) {
  285.         while (v1->right != nullptr) {
  286.             v1 = v1->right;
  287.         }
  288.         Splay(v1); v1->right = v2;
  289.         if (v2 != nullptr) {
  290.             v2->parrent = v1;
  291.         }
  292.         if (v2 != nullptr) {
  293.             v1->sum += v2->sum;
  294.         }
  295.  
  296.     }
  297.     int64_t SplayTree::GetSumm(int64_t k1, int64_t k2) {
  298.         if (root == nullptr) {
  299.             return 0;
  300.         }
  301.         Find(k1);
  302.         int64_t sum = root->sum; if (root->left != nullptr) {
  303.             sum -= root->left->sum;
  304.         }
  305.         if (root->value < k1) {
  306.             sum -= root->value;
  307.         }
  308.         Find(k2);
  309.         if (root->right != nullptr) {
  310.             sum -= root->right->sum;
  311.         }
  312.         if (k2 < root->value) {
  313.             sum -= root->value;
  314.         }
  315.         return sum;
  316.     }
  317.     int main() {
  318.         std::iostream::sync_with_stdio(false); cin.tie(nullptr);
  319.         cout.tie(nullptr);
  320.         SplayTree tree;
  321.         int n;
  322.         char c;
  323.         int64_t s = 0;
  324.  
  325.         int64_t a, b;
  326.         cin >> n;
  327.         for (int i = 0; i < n; ++i) {
  328.             cin >> c >> a; if (c == '+') {
  329.                 a = (a % 1000000001) + (s % 1000000001);
  330.                 a %= 1000000001; tree.Insert(a);
  331.             }
  332.             if (c == '-') {
  333.                 a = (a % 1000000001) + (s % 1000000001); a %= 1000000001;
  334.                 tree.Remove(a);
  335.             }
  336.             if (c == '?') {
  337.                 a = (a % 1000000001) + (s % 1000000001); a %= 1000000001;
  338.                 if (tree.Find(a)) {
  339.                     cout << "Found" << std::endl;
  340.                 }
  341.                 else {
  342.                     cout << "Not found" << std::endl;
  343.                 }
  344.             }
  345.             if (c == 's') {
  346.                 if (a == 0) {
  347.                     a = 0;
  348.                 }
  349.                 cin >> b;
  350.                 a = (a % 1000000001) + (s % 1000000001);
  351.                 a %= 1000000001;
  352.                 b = (b % 1000000001) + (s % 1000000001); b %= 1000000001;
  353.  
  354.                 s = tree.GetSumm(a, b);
  355.                 cout << s << std::endl;
  356.             }
  357.         }
  358.     }[/CODE]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement