SHARE
TWEET

Untitled

lalalalalalalaalalla Nov 14th, 2019 73 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 long long
  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 = 1e17;
  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.     node *l = nullptr;
  62.     node *r = nullptr;
  63.     node *nxt = nullptr;
  64.     node() {
  65.         size = 1;
  66.     }
  67. };
  68.  
  69. typedef node* pnode;
  70. typedef pair<pnode, pnode> Pair;
  71.  
  72. pnode root = nullptr;
  73. vector<node> a(MAXN);
  74. vector<pnode> p(MAXN);
  75.  
  76. int gs(pnode a) {
  77.     if (a) return a->size;
  78.     return 0;
  79. }
  80.  
  81. void update(pnode a) {
  82.     a->size = 1 + gs(a->l) + gs(a->r);
  83. }
  84.  
  85. pnode merge(pnode a, pnode b) {
  86.     if (!a) return b;
  87.     if (!b) return a;
  88.     if (a->y > b->y) {
  89.         a->r = merge(a->r, b);
  90.         update(a);
  91.         return a;
  92.     } else {
  93.         b->l = merge(a, b->l);
  94.         update(b);
  95.         return b;
  96.     }
  97. }
  98.  
  99. void split(pnode a, int k, pnode &l, pnode &r) {
  100.     if (!a) {
  101.         l = r = nullptr;
  102.         return;
  103.     }
  104.     if (gs(a->l) >= k) {
  105.         split(a->l, k, l, r);
  106.         a->l = r;
  107.         update(a);
  108.         r = a;
  109.     } else {
  110.         split(a->r, k - gs(a->l) - 1, l, r);
  111.         a->r = l;
  112.         update(a);
  113.         l = a;
  114.     }
  115. }
  116.  
  117. pnode left_most(pnode a) {
  118.     if (!a) return nullptr;
  119.     if (a->l) return left_most(a->l);
  120.     return a;
  121. }
  122.  
  123. pnode right_most(pnode a) {
  124.     if (!a) return nullptr;
  125.     if (a->r) return right_most(a->r);
  126.     return a;
  127. }
  128.  
  129. void ch(int l1, int r1, int l2, int r2) {
  130.     Pair p1, p2, p3, p4;
  131.     split(root, r2, p4.first, p4.second);
  132.     split(p4.first, l2 - 1, p3.first, p3.second);
  133.     split(p3.first, r1, p2.first, p2.second);
  134.     split(p2.first, l1 - 1, p1.first, p1.second);
  135.     pnode q = right_most(p1.first);
  136.     if (q) q->nxt = left_most(p3.second);
  137.     q = right_most(p1.second);
  138.     if (q) q->nxt = left_most(p4.second);
  139.     q = right_most(p3.second);
  140.     if (p2.second) {
  141.         q->nxt = left_most(p2.second);
  142.         q = right_most(p2.second);
  143.         q->nxt = left_most(p1.second);
  144.     } else {
  145.         q->nxt = left_most(p1.second);
  146.     }
  147.     root = merge(merge(p1.first, p3.second), merge(merge(p2.second, p1.second), p4.second));
  148. }
  149.  
  150. int x, first, second, third;
  151.  
  152. void ans() {
  153.     pnode q = p[x - 1];
  154.     first = second = third = -1;
  155.     if (q->nxt) {
  156.         first = q->nxt->val;
  157.         if (q->nxt->nxt) {
  158.             second = q->nxt->nxt->val;
  159.             if (q->nxt->nxt->nxt) {
  160.                 third = q->nxt->nxt->nxt->val;
  161.             }
  162.         }
  163.     }
  164.     cout << first << " " << second << " " << third << "\n";
  165. }
  166.  
  167. signed main() {
  168.     ios_base::sync_with_stdio(0);
  169.     cin.tie(0);
  170.     cout.tie(0);
  171.     int n, m;
  172.     cin >> n >> m;
  173.     pnode q = nullptr, lst = nullptr;
  174.     for (int i = 0; i < n; i++) {
  175.         cin >> x;
  176.         q = &a[x - 1];
  177.         q->val = x;
  178.         p[x - 1] = q;
  179.         if (lst) lst->nxt = q;
  180.         root = merge(root, q);
  181.         lst = q;
  182.     }
  183.     int t, l1, r1, l2, r2;
  184.     for (int i = 0; i < m; i++) {
  185.         cin >> t;
  186.         if (t == 2) {
  187.             cin >> x;
  188.             ans();
  189.         } else {
  190.             cin >> l1 >> r1 >> l2 >> r2;
  191.             ch(l1, r1, l2, r2);
  192.         }
  193.     }
  194. }
  195. /*
  196.  
  197. */
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
 
Top