SHARE
TWEET

Untitled

lalalalalalalaalalla Nov 12th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <iomanip>
  5. #include <queue>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <tuple>
  9. #include <iomanip>
  10. #include <stdio.h>
  11. #include <map>
  12. #include <bitset>
  13. #include <set>
  14. #include <stack>
  15. #include <queue>
  16. #include <unordered_set>
  17. #include <cassert>
  18. #include <stdlib.h>
  19. #include <time.h>
  20. #include <random>
  21.  
  22.  
  23. //#pragma GCC optimize("Ofast,no-stack-protector")
  24. //#pragma GCC target("sse,sse2,sse3,sse3,sse4");
  25. //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
  26. //#pragma GCC optimize("unroll-loops")
  27. //#pragma GCC optimize("fast-math")
  28. //#pragma GCC optimize("section-anchors")
  29. //#pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
  30. //#pragma GCC optimize("vpt")
  31. //#pragma GCC optimize("rename-registers")
  32. //#pragma GCC optimize("move-loop-invariants")
  33. //#pragma GCC optimize("unswitch-loops")
  34. //#pragma GCC optimize("function-sections")
  35. //#pragma GCC optimize("data-sections")
  36. //#pragma GCC optimize("branch-target-load-optimize")
  37. //#pragma GCC optimize("branch-target-load-optimize2")
  38. //#pragma GCC optimize("btr-bb-exclusive")
  39.  
  40.  
  41. #define int short
  42. #define ll long long
  43. #define ull unsigned long long
  44. #define all(a) a.begin(), a.end()
  45. #define pii pair<int, int>
  46. #define pb push_back
  47. #define ld long double
  48.  
  49.  
  50. using namespace std;
  51.  
  52. //const int INF = 2e9;
  53. //const int mod = 2600000069;
  54. //const int p = 179;
  55. const int MAXN = 1e4 + 1;
  56.  
  57. mt19937 my_rand(1329);
  58.  
  59. struct node {
  60.     int y = my_rand(), size = 0, val = 0;
  61.     bitset<10000> sub = 0;
  62.     node *l, *r;
  63.     node() {
  64.         size = 1;
  65.     }
  66. };
  67.  
  68. typedef node* pnode;
  69. typedef pair<pnode, pnode> Pair;
  70.  
  71. vector<node> a(MAXN);
  72. //int ind = 0;
  73.  
  74. pnode root = nullptr;
  75.  
  76. void update(pnode a) {
  77.     a->size = 1;
  78.     a->sub = 0;
  79.     a->sub[a->val - 1] = 1;
  80.     if (a->l) {
  81.         a->sub |= a->l->sub;
  82.         a->size += a->l->size;
  83.     }
  84.     if (a->r) {
  85.         a->sub |= a->r->sub;
  86.         a->size += a->r->size;
  87.     }
  88. }
  89.  
  90. int gs(pnode a) {
  91.     if (a) return a->size;
  92.     return 0;
  93. }
  94.  
  95. pnode merge(pnode a, pnode b) {
  96.     if (!a) return b;
  97.     if (!b) return a;
  98.     if (a->y > b->y) {
  99.         a->r = merge(a->r, b);
  100.         update(a);
  101.         return a;
  102.     } else {
  103.         b->l = merge(a, b->l);
  104.         update(b);
  105.         return b;
  106.     }
  107. }
  108.  
  109. void split(pnode a, int k, pnode &l, pnode &r) {
  110.     if (!a) {
  111.         l = r = nullptr;
  112.         return;
  113.     }
  114.     if (gs(a->l) >= k) {
  115.         split(a->l, k, l, r);
  116.         a->l = r;
  117.         update(a);
  118.         r = a;
  119.     } else {
  120.         split(a->r, k - gs(a->l) - 1, l, r);
  121.         a->r = l;
  122.         update(a);
  123.         l = a;
  124.     }
  125. }
  126.  
  127. void swap(int l1, int r1, int l2, int r2) {
  128.     Pair p1, p2, p3, p4;
  129.     split(root, r2, p4.first, p4.second);
  130.     split(p4.first, l2 - 1, p3.first, p3.second);
  131.     split(p3.first, r1, p2.first, p2.second);
  132.     split(p2.first, l1 - 1, p1.first, p1.second);
  133.     root = merge(merge(p1.first, p3.second), merge(p2.second, merge(p1.second, p4.second)));
  134. }
  135.  
  136. int x;
  137.  
  138. int gt(pnode a, int k) {
  139.     if (a->val == x) return k + gs(a->l);
  140.     if (a->l && a->l->sub[x - 1]) return gt(a->l, k);
  141.     if (a->r && a->r->sub[x - 1]) return gt(a->r, k + gs(a->l) + 1);
  142. }
  143.  
  144. int leftt;
  145.  
  146. void print(pnode a) {
  147.     if (!a) return;
  148.     print(a->l);
  149.     cout << a->val << " ";
  150.     --leftt;
  151.     print(a->r);
  152. }
  153.  
  154. void ans() {
  155.     int k = gt(root, 0);
  156.     Pair p1, p2;
  157.     split(root, k + 4, p2.first, p2.second);
  158.     split(p2.first, k + 1, p1.first, p1.second);
  159.     leftt = 3;
  160.     print(p1.second);
  161.     for (int i = 0; i < leftt; i++) {
  162.         cout << "-1 ";
  163.     }
  164.     cout << "\n";
  165.     root = merge(merge(p1.first, p1.second), p2.second);
  166. }
  167.  
  168. signed main() {
  169.     ios_base::sync_with_stdio(0);
  170.     cin.tie(0);
  171.     cout.tie(0);
  172.     int n;
  173.     long m;
  174.     cin >> n >> m;
  175.     pnode q;
  176.     int x1;
  177.     for (int i = 0; i < n; i++) {
  178.         cin >> x1;
  179.         q = &a[x1 - 1];
  180.         q->sub[x1 - 1] = 1;
  181.         q->val = x1;
  182.         root = merge(root, q);
  183.     }
  184.     int l1, r1, l2, r2, t;
  185.     for (long i = 0; i < m; i++) {
  186.         cin >> t;
  187.         if (t == 2) {
  188.             cin >> x;
  189.             ans();
  190.         } else {
  191.             cin >> l1 >> r1 >> l2 >> r2;
  192.             swap(l1, r1, l2, r2);
  193.         }
  194.     }
  195. }
  196. /*
  197.  
  198. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top