Advertisement
rudolf222222

Untitled

Nov 19th, 2022 (edited)
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.72 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() % 1500000;
  15.     int size = 1;
  16.     Node* left = nullptr;
  17.     Node* right = nullptr;
  18.     bool rev = false;
  19.     Node(int val) { value = val; }
  20.   };
  21.   typedef Node* Pnode;
  22.   Pnode root_ = nullptr;
  23.   void Merge(Pnode first, Pnode second);
  24.   void MergeHelper(Pnode& current, Pnode t1, Pnode t2);
  25.   std::pair<Pnode, Pnode> Split(size_t key);
  26.   void SplitHelper(Pnode current, size_t key, Pnode& left, Pnode& right);
  27.   void Update(Pnode node);
  28.   static int Min(int val1, int val2, int val3) {
  29.     std::vector<int> temp = {val1, val2, val3};
  30.     std::sort(temp.begin(), temp.end());
  31.     return temp[0];
  32.   }
  33.   void PrintHelper(Pnode current) {
  34.     if (current == nullptr) {
  35.       return;
  36.     }
  37.     // i++;
  38.     // int temp = i;
  39.     // i++;
  40.     Push(current);
  41.     PrintHelper(current->left);
  42.     std::cout << current->value << " ";
  43.     PrintHelper(current->right);
  44.     // std::cout << current->value << " ";
  45.   }
  46.   Treap(Pnode node) { root_ = node; }
  47.   void ClearHelper(Pnode node) {
  48.     if (node == nullptr) {
  49.       return;
  50.     }
  51.     ClearHelper(node->left);
  52.     ClearHelper(node->right);
  53.     delete node;
  54.   }
  55.   void Push(Pnode node) {
  56.     if (node == nullptr) {
  57.       return;
  58.     }
  59.     if (!node->rev) {
  60.       return;
  61.     }
  62.  
  63.     if (node != nullptr && node->rev) {
  64.       node->rev = false;
  65.       std::swap(node->left, node->right);
  66.       if (node->left != nullptr) {
  67.         node->left->rev ^= true;
  68.       }
  69.       if (node->right != nullptr) {
  70.         node->right->rev ^= true;
  71.       }
  72.     }
  73.   }
  74.   void ReverseHelper(int left, int right);
  75.  
  76.  public:
  77.   void Print() {
  78.     // int i = 0;
  79.     PrintHelper(root_);
  80.     std::cout << "\n";
  81.   }
  82.   Treap() { root_ = nullptr; };
  83.   void Insert(size_t pos, int value);
  84.   size_t& Size();
  85.   int GetMin(size_t left, size_t right);
  86.   void Clear();
  87.   void Reverse(int left, int right);
  88. };
  89.  
  90. void Treap::Reverse(int left, int right) { ReverseHelper(left, right); }
  91. void Treap::ReverseHelper(int left, int right) {
  92.   std::pair<Pnode, Pnode> res = Split(left - 1);  // got t1, t2
  93.   Treap temp(res.second);                         // new tree to split
  94.   std::pair<Pnode, Pnode> res2 = temp.Split(right - left + 1);
  95.   res2.first->rev ^= true;
  96.   temp.Merge(res2.first, res2.second);
  97.   Merge(res.first, temp.root_);
  98. }
  99. void Treap::Clear() { ClearHelper(root_); }
  100. std::pair<Treap::Pnode, Treap::Pnode> Treap::Split(size_t key) {
  101.   Pnode left = nullptr;
  102.   Pnode right = nullptr;
  103.   SplitHelper(root_, key, left, right);
  104.   return {left, right};
  105. }
  106.  
  107. void Treap::SplitHelper(Pnode current, size_t key, Pnode& left, Pnode& right) {
  108.   if (current == nullptr) {
  109.     left = nullptr;
  110.     right = nullptr;
  111.     return;
  112.   }
  113.   Push(current);
  114.   size_t curr_key = (current->left == nullptr ? 0 : current->left->size) + 1;
  115.   if (curr_key <= key) {
  116.     SplitHelper(current->right, key - curr_key, current->right, right);
  117.     left = current;
  118.   } else {
  119.     SplitHelper(current->left, key, left, current->left);
  120.     right = current;
  121.   }
  122.   Update(current);
  123. }
  124.  
  125. void Treap::Update(Pnode node) {
  126.   node->lefts = (node->left != nullptr) ? node->left->size : 0;
  127.   node->rights = (node->right != nullptr) ? node->right->size : 0;
  128.   int min = node->value;
  129.   if (node->left != nullptr && node->right != nullptr) {
  130.     min = Min(node->left->min, node->right->min, node->value);
  131.   } else if (node->left != nullptr) {
  132.     min = std::min(node->left->min, node->value);
  133.   } else if (node->right != nullptr) {
  134.     min = std::min(node->right->min, node->value);
  135.   }
  136.   node->min = min;
  137.   node->size = 1 + node->lefts + node->rights;
  138. }
  139.  
  140. void Treap::MergeHelper(Pnode& current, Pnode t1, Pnode t2) {
  141.   Push(t1);
  142.   Push(t2);
  143.   if (t1 == nullptr || t2 == nullptr) {
  144.     current = t1 ? t1 : t2;
  145.   } else if (t1->prior > t2->prior) {
  146.     MergeHelper(t1->right, t1->right, t2);
  147.     current = t1;
  148.   } else {
  149.     MergeHelper(t2->left, t1, t2->left);
  150.     current = t2;
  151.   }
  152.   Update(current);
  153. }
  154. void Treap::Merge(Pnode first, Pnode other) {
  155.   MergeHelper(root_, first, other);
  156. }
  157. void Treap::Insert(size_t pos, int value) {
  158.   std::pair<Pnode, Pnode> res = Split(pos);
  159.   Pnode m = new Node(value);
  160.   Treap temp;
  161.   temp.Merge(res.first, m);
  162.   this->Merge(temp.root_, res.second);
  163. }
  164. int Treap::GetMin(size_t left, size_t right) {
  165.   int result = 0;
  166.   Treap r_tree;
  167.   Pnode copy_root = root_;
  168.   Treap copy(copy_root);
  169.   std::pair<Pnode, Pnode> res = copy.Split(left - 1);
  170.   r_tree.root_ = res.second;
  171.   // l_tree.root_ = res.first;
  172.   std::pair<Pnode, Pnode> res2 = r_tree.Split(right - left + 1);
  173.   result = res2.first->min;
  174.   r_tree.Merge(res2.first, res2.second);
  175.   Merge(res.first, r_tree.root_);
  176.   return result;
  177. }
  178.  
  179. void Requests() {
  180.   size_t n = 0;
  181.   size_t m = 0;
  182.   std::cin >> n >> m;
  183.   Treap treap;
  184.   // std::vector<int> vect;
  185.   int val = 0;
  186.   for (size_t i = 0; i < n; ++i) {
  187.     std::cin >> val;
  188.     // vect.push_back(val);
  189.     treap.Insert(i, val);
  190.     // treap.Print();
  191.   }
  192.   // treap.Print();
  193.   // std::cout<<"-------\n";
  194.   char request = 0;
  195.   for (size_t i = 0; i < m; ++i) {
  196.     std::cin >> request;
  197.     if (request == '1') {
  198.       int left = 0, right = 0;
  199.       std::cin >> left;
  200.       std::cin >> right;
  201.       treap.Reverse(left, right);
  202.       // treap.Print();
  203.       // treap.Print();
  204.       // treap.Print();
  205.     } else {
  206.       int left = 0, right = 0;
  207.       std::cin >> left >> right;
  208.       std::cout << treap.GetMin(left, right) << "\n";
  209.     }
  210.   }
  211.   treap.Clear();
  212.   // std::cout << "---\n";
  213.   // treap.Print();
  214. }
  215. int main() {
  216.   Requests();
  217.   return 0;
  218. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement