Advertisement
rudolf222222

Untitled

Nov 19th, 2022 (edited)
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.67 KB | None | 0 0
  1. #include <math.h>
  2.  
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <vector>
  6.  
  7. class Treap {
  8.  private:
  9.   struct Node {
  10.     int value = 0;
  11.     int lefts = 0;
  12.     int rights = 0;
  13.     int min = 0;
  14.     int prior = rand() % 150000;
  15.     int size = 1;
  16.     Node* left = nullptr;
  17.     Node* right = nullptr;
  18.     Node(int val) { value = val; }
  19.   };
  20.   typedef Node* Pnode;
  21.   Pnode root_ = nullptr;
  22.   void Merge(Pnode first, Pnode second);
  23.   void MergeHelper(Pnode& current, Pnode t1, Pnode t2);
  24.   std::pair<Pnode, Pnode> Split(size_t key);
  25.   void SplitHelper(Pnode current, size_t key, Pnode& left, Pnode& right);
  26.   void Update(Pnode node);
  27.   static int Min(int val1, int val2, int val3) {
  28.     std::vector<int> temp = {val1, val2, val3};
  29.     std::sort(temp.begin(), temp.end());
  30.     return temp[0];
  31.   }
  32.   void PrintHelper(Pnode current) {
  33.     if (current == nullptr) {
  34.       return;
  35.     }
  36.     // i++;
  37.     // int temp = i;
  38.     // i++;
  39.     PrintHelper(current->left);
  40.     std::cout << current->value << " ";
  41.     PrintHelper(current->right);
  42.     // std::cout << current->value << " ";
  43.   }
  44.   Treap(Pnode node) { root_ = node; }
  45.   void ClearHelper(Pnode node) {
  46.     if (node == nullptr) {
  47.       return;
  48.     }
  49.     ClearHelper(node->left);
  50.     ClearHelper(node->right);
  51.     delete node;
  52.   }
  53.  
  54.  public:
  55.   void Print() {
  56.     // int i = 0;
  57.     PrintHelper(root_);
  58.     std::cout << "\n";
  59.   }
  60.   Treap() { root_ = nullptr; };
  61.   void Insert(size_t pos, int value);
  62.   // size_t Size() const;
  63.   size_t& Size();
  64.   int GetMin(size_t left, size_t right);
  65.   void Clear();
  66. };
  67.  
  68. void Treap::Clear() { ClearHelper(root_); }
  69. std::pair<Treap::Pnode, Treap::Pnode> Treap::Split(size_t key) {
  70.   Pnode left = nullptr;
  71.   Pnode right = nullptr;
  72.   SplitHelper(root_, key, left, right);
  73.   return {left, right};
  74. }
  75.  
  76. void Treap::SplitHelper(Pnode current, size_t key, Pnode& left, Pnode& right) {
  77.   if (current == nullptr) {
  78.     left = nullptr;
  79.     right = nullptr;
  80.     return;
  81.   }
  82.   size_t curr_key = current->lefts;
  83.   if (key <= curr_key) {
  84.     SplitHelper(current->left, key, left, current->left);
  85.     right = current;
  86.     // Update(current);
  87.   } else {
  88.     SplitHelper(current->right, key - curr_key - 1, current->right, right);
  89.     left = current;
  90.   }
  91.   Update(current);
  92. }
  93.  
  94. void Treap::Update(Pnode node) {
  95.   node->lefts = (node->left != nullptr) ? node->left->size : 0;
  96.   node->rights = (node->right != nullptr) ? node->right->size : 0;
  97.   int min = node->value;
  98.   if (node->left != nullptr && node->right != nullptr) {
  99.     min = Min(node->left->min, node->right->min, node->value);
  100.   } else if (node->left != nullptr) {
  101.     min = std::min(node->left->min, node->value);
  102.   } else if (node->right != nullptr) {
  103.     min = std::min(node->right->min, node->value);
  104.   }
  105.   node->min = min;
  106.   node->size = 1 + node->lefts + node->rights;
  107. }
  108.  
  109. void Treap::MergeHelper(Pnode& current, Pnode t1, Pnode t2) {
  110.   if (t1 == nullptr || t2 == nullptr) {
  111.     current = t1 ? t1 : t2;
  112.   } else if (t1->prior > t2->prior) {
  113.     MergeHelper(t1->right, t1->right, t2);
  114.     current = t1;
  115.   } else {
  116.     MergeHelper(t2->left, t1, t2->left);
  117.     current = t2;
  118.   }
  119.   Update(current);
  120. }
  121.  
  122. void Treap::Merge(Pnode first, Pnode other) {
  123.   MergeHelper(root_, first, other);
  124. }
  125. void Treap::Insert(size_t pos, int value) {
  126.   std::pair<Pnode, Pnode> res = Split(pos);
  127.   Pnode m = new Node(value);
  128.   Treap temp;
  129.   temp.Merge(res.first, m);
  130.   this->Merge(temp.root_, res.second);
  131. }
  132. int Treap::GetMin(size_t left, size_t right) {
  133.   int result = 0;
  134.   Treap l_tree;
  135.   Treap r_tree;
  136.   Treap l2_tree;
  137.   Treap r2_tree;
  138.   Pnode copy_root = root_;
  139.   Treap copy(copy_root);
  140.   std::pair<Pnode, Pnode> res = copy.Split(left);
  141.   r_tree.root_ = res.second;
  142.   l_tree.root_ = res.first;
  143.   std::pair<Pnode, Pnode> res2 = r_tree.Split(right - left);
  144.   result = res2.first->min;
  145.   r_tree.Merge(res2.first, res2.second);
  146.   this->Merge(l_tree.root_, r_tree.root_);
  147.   return result;
  148. }
  149. /*public double MaxCostOn(int A, int B)
  150. {
  151.     ImplicitTreap l, m, r;
  152.     this.Split(A, out l, out r);
  153.     r.Split(B - A, out m, out r);
  154.     return CostOf(m);
  155. }*/
  156. void Requests() {
  157.   size_t n = 0;
  158.   std::cin >> n;
  159.   Treap treap;
  160.   char request = 0;
  161.   for (size_t i = 0; i < n; ++i) {
  162.     std::cin >> request;
  163.     if (request == '+') {
  164.       int value = 0, pos = 0;
  165.       std::cin >> pos;
  166.       std::cin >> value;
  167.       treap.Insert(pos, value);
  168.       // treap.Print();
  169.     } else {
  170.       int left = 0, right = 0;
  171.       std::cin >> left >> right;
  172.       std::cout << treap.GetMin(left - 1, right) << "\n";
  173.     }
  174.   }
  175.   treap.Clear();
  176.   // std::cout << "---\n";
  177.   // treap.Print();
  178. }
  179. int main() {
  180.   Requests();
  181.   return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement