Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. struct node {
  10.     node *left, *right;
  11.     int size;
  12.     int value;
  13.     int priority;
  14.     int sum;
  15.     int promise;
  16.     int reverse;
  17.     node (int value = 0):
  18.         left(nullptr),
  19.         right(nullptr),
  20.         size(1),
  21.         value(value),
  22.         priority((rand() << 16) | rand()),
  23.         sum(value),
  24.         promise(0),
  25.         reverse(0)
  26.         {}
  27. };
  28.  
  29. int sum(node *v) {
  30.     if (v == nullptr) {
  31.         return 0;
  32.     } else {
  33.         return v->sum;
  34.     }
  35. }
  36.  
  37. int size(node *v) {
  38.     if (v == nullptr) {
  39.         return 0;
  40.     } else {
  41.         return v->size;
  42.     }
  43. }
  44.  
  45. void down(node *v, int promise, int r) {
  46.     if (v == nullptr) {
  47.         return;
  48.     } else {
  49.         v->promise += promise;
  50.         v->reverse ^= r;
  51.     }
  52. }
  53.  
  54. void push(node *v) {
  55.     if (v == nullptr) {
  56.         return;
  57.     }
  58.     v->sum += v->promise * v->size;
  59.     v->value += v->promise;
  60.     if (v->reverse) {
  61.         swap(v->left, v->right);
  62.     }
  63.  
  64.     down(v->left, v->promise, v->reverse);
  65.     down(v->right, v->promise, v->reverse);
  66.  
  67.     v->promise = 0;
  68.     v->reverse = 0;
  69. }
  70.  
  71. void up(node *v) {
  72.     push(v->left);
  73.     push(v->right);
  74.     v->size = size(v->left) + size(v->right) + 1;
  75.     v->sum = sum(v->left) + sum(v->right) + v->value;
  76. }
  77.  
  78. node *merge(node *root_l, node *root_r) {
  79.     push(root_l);
  80.     push(root_r);
  81.     if (root_l == nullptr) {
  82.         return root_r;
  83.     }
  84.     if (root_r == nullptr) {
  85.         return root_l;
  86.     }
  87.     if (root_l->priority < root_r->priority) {
  88.         root_l->right = merge(root_l->right, root_r);
  89.         up(root_l);
  90.         return root_l;
  91.     } else {
  92.         root_r->left = merge(root_l, root_r->left);
  93.         up(root_r);
  94.         return root_r;
  95.     }
  96. }
  97.  
  98. pair <node*, node*> split(node *root, int cnt) {
  99.     if (root == nullptr) {
  100.         return {nullptr, nullptr};
  101.     }
  102.     push(root);
  103.     if (size(root->left) >= cnt) {
  104.         auto p = split(root->left, cnt);
  105.         root->left = p.second;
  106.         up(root);
  107.         return {p.first, root};
  108.     } else {
  109.         auto p = split(root->right, cnt - size(root->left) - 1);
  110.         root->right = p.first;
  111.         up(root);
  112.         return {root, p.second};
  113.     }
  114. }
  115.  
  116. void write(node *root) {
  117.     if (root == nullptr) {
  118.         return;
  119.     }
  120.     push(root);
  121.     write(root->left);
  122.     cout << root->value << " ";
  123.     write(root->right);
  124. }
  125.  
  126. void write(node *root, int cnt) {
  127.     if (root == nullptr) {
  128.         return;
  129.     }
  130.     write(root->left, cnt + 1);
  131.     for (int i = 0; i < cnt; ++i) {
  132.         cout << " ";
  133.     }
  134.     cout << root->value << " " << root->reverse << endl;
  135.     write(root->right, cnt + 1);
  136. }
  137.  
  138. node *root = nullptr;
  139.  
  140. int sum(int l, int r) {
  141.     auto p1 = split(root, r + 1);
  142.     auto p2 = split(p1.first, l);
  143.     int ans = sum(p2.second);
  144.     root = merge(p2.first, merge(p2.second, p1.second));
  145.     return ans;
  146. }
  147.  
  148. void upd(int l, int r, int k) {
  149.     auto p1 = split(root, r + 1);
  150.     auto p2 = split(p1.first, l);
  151.     down(p2.second, k, 0);
  152.     root = merge(p2.first, merge(p2.second, p1.second));
  153. }
  154.  
  155. void reverse(int l, int r) {
  156.     auto p1 = split(root, r + 1);
  157.     auto p2 = split(p1.first, l);
  158.     down(p2.second, 0, 1);
  159.     root = merge(p2.first, merge(p2.second, p1.second));
  160. }
  161.  
  162. int main() {
  163.     int n, m;
  164.     cin >> n >> m;
  165.     for (int i = 0; i < n; ++i) {
  166.         int a;
  167.         cin >> a;
  168.         root = merge(root, new node(a));
  169.     }
  170.     for (int i = 0; i < m; ++i) {
  171.         char t;
  172.         cin >> t;
  173.         if (t == '?') {
  174.             int l, r;
  175.             cin >> l >> r;
  176.             --l;
  177.             --r;
  178.             cout << sum(l, r) << endl;
  179.             cout << endl;
  180.             write(root, 0);
  181.             cout << endl;
  182.         }
  183.         if (t == '+') {
  184.             int l, r, k;
  185.             cin >> l >> r >> k;
  186.             upd(l - 1, r - 1, k);
  187.         }
  188.         if (t == 'w') {
  189.             write(root);
  190.             cout << endl;
  191.         }
  192.         if (t == 'r') {
  193.             int l, r;
  194.             cin >> l >> r;
  195.             reverse(l - 1, r - 1);
  196.             write(root, 0);
  197.         }
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement