ke_timofeeva7

своппер

Nov 4th, 2022
818
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <cmath>
  5. #include <memory.h>
  6. #include <algorithm>
  7. #include <stack>
  8. #include <deque>
  9. #include <iomanip>
  10. #include <stdio.h>
  11. #include <queue>
  12. #include <map>
  13. #include <set>
  14. #include <unordered_map>
  15. #include <unordered_set>
  16. #include <random>
  17. #include <ctime>
  18. #include <cstdlib>
  19. #include <cassert>
  20. #include <chrono>
  21. #include <array>
  22. #define int long long
  23. #define pii pair <int, int>
  24. #define pb push_back
  25. #define all(vc) vc.begin(), vc.end()
  26. #define fir first
  27. #define sec second
  28. #define endl "\n"
  29. #define un unsigned
  30. #define INF 1000000009
  31. #define double long double
  32. using namespace std;
  33.  
  34. const int N = 1005, R = 1 << 20, MOD = 1e9 + 7, ABC = 26, logn = 19;
  35.  
  36. mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
  37.  
  38. struct Node {
  39.     int x, y, sz, sum;
  40.     Node* left = nullptr;
  41.     Node* right = nullptr;
  42.  
  43.     Node(int a) :x(a), y(rnd()), sz(1), sum(a) {}
  44.     Node() :x(0), y(rnd()), sz(1), sum(0) {}
  45. };
  46.  
  47. int get_sum(Node* a) {
  48.     if (a == nullptr)
  49.         return 0;
  50.     return a->sum;
  51. }
  52.  
  53. int get_sz(Node* a) {
  54.     if (a == nullptr)
  55.         return 0;
  56.     return a->sz;
  57. }
  58.  
  59. Node* update(Node* a) {
  60.     if (a == nullptr)
  61.         return a;
  62.     a->sz = 1 + get_sz(a->left) + get_sz(a->right);
  63.     a->sum = a->x + get_sum(a->left) + get_sum(a->right);
  64.     return a;
  65. }
  66.  
  67. Node* merge(Node* a, Node* b) {
  68.     if (a == nullptr)
  69.         return b;
  70.     if (b == nullptr)
  71.         return a;
  72.  
  73.     if (a->y > b->y) {
  74.         a->right = merge(a->right, b);
  75.         a = update(a);
  76.         return a;
  77.     }
  78.     else
  79.     {
  80.         b->left = merge(a, b->left);
  81.         b = update(b);
  82.         return b;
  83.     }
  84. }
  85.  
  86. pair<Node*, Node*>split(Node* root, int k) {
  87.     if (root == nullptr)
  88.         return { nullptr, nullptr };
  89.     if (get_sz(root->left) + 1 <= k) {
  90.         auto sp = split(root->right, k - get_sz(root->left) - 1);
  91.         root->right = sp.first;
  92.         root = update(root);
  93.         return { root, sp.sec };
  94.     }
  95.     else {
  96.         auto sp = split(root->left, k);
  97.         root->left = sp.second;
  98.         root = update(root);
  99.         return { sp.fir, root };
  100.     }
  101. }
  102.  
  103. Node* insert(Node* root, int pos, Node* nn) {
  104.     auto sp = split(root, pos - 1);
  105.     root = merge(sp.first, merge(nn, sp.sec));
  106.     return root;
  107. }
  108.  
  109. signed main()
  110. {
  111.     ios_base::sync_with_stdio(false);
  112.     cin.tie(0);
  113.     cout.tie(0);
  114.     srand(time(0));
  115.  
  116.     int n, m;
  117.     int number = 1;
  118.  
  119.     while (cin >> n >> m) {
  120.         if (n == 0 && m == 0)
  121.             break;
  122.  
  123.         Node* root1 = nullptr;
  124.         Node* root2 = nullptr;
  125.         for (int i = 0; i < n; i++) {
  126.             int a;
  127.             cin >> a;
  128.  
  129.             Node* q = new Node(a);
  130.  
  131.             if ((i+1) & 1) {
  132.                 root1 = insert(root1, i / 2 + 1, q);
  133.             }
  134.             else
  135.             {
  136.                 root2 = insert(root2, i / 2 + 1, q);
  137.             }
  138.         }
  139.  
  140.         cout << "Swapper " << number << ":" << endl;
  141.         number++;
  142.  
  143.         while (m--) {
  144.             int type, l, r;
  145.             cin >> type >> l >> r;
  146.             l--;
  147.             r--;
  148.  
  149.             if (type == 1) {
  150.                 if (l & 1 == 0) {
  151.                     auto sp1 = split(root1, ((r - 1) / 2 + 1));
  152.                     auto sp2 = split(sp1.fir, ((l) / 2 + 1) - 1);
  153.                     auto sp3 = split(root2, ((r) / 2 + 1));
  154.                     auto sp4 = split(sp3.fir, ((l + 1) / 2 + 1) - 1);
  155.                     root1 = merge(sp2.first, merge(sp4.sec, sp1.second));
  156.                     root2 = merge(sp4.first, merge(sp2.sec, sp3.sec));
  157.                 }
  158.                 else {
  159.                     auto sp1 = split(root1, (r / 2 + 1));
  160.                     auto sp2 = split(sp1.fir, ((l + 1) / 2 + 1) - 1);
  161.                     auto sp3 = split(root2, ((r - 1) / 2 + 1));
  162.                     auto sp4 = split(sp3.fir, (l / 2 + 1) - 1);
  163.                     root1 = merge(sp2.first, merge(sp4.sec, sp1.second));
  164.                     root2 = merge(sp4.first, merge(sp2.sec, sp3.sec));
  165.                 }
  166.             }
  167.             else {
  168.                 l++;
  169.                 r++;
  170.                 auto sp1 = split(root1, ((r + 1) / 2));
  171.                 auto sp2 = split(sp1.fir, (l / 2 ));
  172.                 auto sp3 = split(root2, ((r) / 2));
  173.                 auto sp4 = split(sp3.fir, (l-1) / 2);
  174.                 int ans = get_sum(sp2.sec) + get_sum(sp4.sec);
  175.                 cout << ans << endl;
  176.                 root1 = merge(merge(sp2.first, sp2.sec), sp1.sec);
  177.                 root2 = merge(merge(sp4.first, sp4.sec), sp3.sec);
  178.             }
  179.         }
  180.     }
  181.     return 0;
  182. }
  183.  
Advertisement
Add Comment
Please, Sign In to add comment