Advertisement
pasholnahuy

Назад!

May 28th, 2023
929
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <tuple>
  3. #include <random>
  4.  
  5. using std::pair;
  6. using std::cin;
  7. using std::cout;
  8.  
  9. class InsertTree {
  10. private:
  11.     struct Node {
  12.         int val;
  13.         int size = 1;
  14.         Node *left = nullptr;
  15.         Node *right = nullptr;
  16.  
  17.         static int Size(Node *n) {
  18.             if (n != nullptr) {
  19.                 return n->size;
  20.             }
  21.             return 0;
  22.         }
  23.  
  24.         void Update() {
  25.             size = Size(left) + Size(right) + 1;
  26.         }
  27.     };
  28.  
  29.     pair<Node *, Node *> Split(Node *n, int ind) {
  30.         if (n == nullptr) {
  31.             return {nullptr, nullptr};
  32.         }
  33.         if (Node::Size(n->left) >= ind) {
  34.             auto [left, right] = Split(n->left, ind);
  35.             n->left = right;
  36.             n->Update();
  37.             return {left, n};
  38.         }
  39.         auto [left, right] = Split(n->right, ind - Node::Size(n->left) - 1);
  40.         n->right = left;
  41.         n->Update();
  42.         return {n, right};
  43.     }
  44.  
  45.     static Node *Merge(Node *root1, Node *root2) {
  46.         if (root1 == nullptr) {
  47.             return root2;
  48.         }
  49.         if (root2 == nullptr) {
  50.             return root1;
  51.         }
  52.         static std::minstd_rand rand_gen;
  53.         if (std::uniform_int_distribution(1, Node::Size(root1) + Node::Size(root2))(rand_gen) <= Node::Size(root1)) {
  54.             root1->right = Merge(root1->right, root2);
  55.             root1->Update();
  56.             return root1;
  57.         }
  58.         root2->left = Merge(root1, root2->left);
  59.         root2->Update();
  60.         return root2;
  61.     }
  62.  
  63.     static Node *Build(int l, int r) {
  64.         if (l >= r) {
  65.             return nullptr;
  66.         }
  67.         Node *new_node = new Node{(l + r) / 2};
  68.         new_node->left = Build(l, (l + r) / 2);
  69.         new_node->right = Build((l + r) / 2 + 1, r);
  70.         new_node->Update();
  71.         return new_node;
  72.     }
  73.     void PrintNodes(Node* n){
  74.         if (n == nullptr){
  75.             return;
  76.         }
  77.         PrintNodes(n->left);
  78.         cout << n->val << " ";
  79.         PrintNodes(n->right);
  80.     }
  81. public:
  82.     explicit InsertTree(int size) : root(Build(1, size + 1)) {
  83.     }
  84.     void Cut(int l, int r){
  85.         auto [less_eq_right, greater_right] = Split(root, r + 1);
  86.         auto [less_left, between] = Split(less_eq_right, l);
  87. //        root = Merge(Merge(between, less_left), greater_right);
  88.         root = Merge(less_left, Merge(greater_right, between));
  89.     }
  90.     void PrintNodes(){
  91.         PrintNodes(root);
  92.     }
  93.     Node* root = nullptr;
  94.  
  95. };
  96.  
  97. int main() {
  98.     int n, k;
  99.     cin >> n >> k;
  100.     InsertTree it(n);
  101.     for (int i = 0; i < k; ++i){
  102.         int l, r;
  103.         cin >> l >> r, --l, --r;
  104.         it.Cut(l, r);
  105.     }
  106.     it.PrintNodes();
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement