Alex_tz307

Treap

Apr 8th, 2021 (edited)
512
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream fin("secv8.in");
  6. ofstream fout("secv8.out");
  7.  
  8. struct treap {
  9.   treap* l;
  10.   treap* r;
  11.   int val, priority, sz;
  12.   bool rev;
  13. };
  14.  
  15. using pt = pair<treap*,treap*>;
  16. const int mod = 1e9 + 7;
  17. const int NMAX = 5e5 + 5;
  18. int a[NMAX];
  19. treap* emptyNode = new treap{nullptr, nullptr, 0, 0, 0, false};
  20. treap* root = emptyNode;
  21. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  22.  
  23. void update_sz(treap* nod) {
  24.   if (nod != emptyNode)
  25.     nod->sz = nod->l->sz + nod->r->sz + 1;
  26. }
  27.  
  28. void heapify(treap* nod) {
  29.   if (nod == emptyNode)
  30.     return;
  31.   treap* best = nod;
  32.   if (nod->l != nullptr && nod->l->priority > best->priority)
  33.     best = nod->l;
  34.   if (nod->r != nullptr && nod->r->priority > best->priority)
  35.     best = nod->r;
  36.   if (best != nod) {
  37.     swap(nod->priority, best->priority);
  38.     heapify(best);
  39.   }
  40. }
  41.  
  42. treap* build(int st, int dr) {
  43.   int mid = (st + dr) >> 1;
  44.   treap* nod = new treap{emptyNode, emptyNode, a[mid], rng() % mod, 1};
  45.   if (st < mid)
  46.     nod->l = build(st, mid - 1);
  47.   if (mid < dr)
  48.     nod->r = build(mid + 1, dr);
  49.   heapify(nod);
  50.   update_sz(nod);
  51.   return nod;
  52. }
  53.  
  54. void push_rev(treap* nod) {
  55.   if (nod != emptyNode && nod->rev) {
  56.     swap(nod->l, nod->r);
  57.     nod->l->rev ^= true;
  58.     nod->r->rev ^= true;
  59.     nod->rev = false;
  60.   }
  61. }
  62.  
  63. pt split(treap* nod, int poz) {
  64.   if (nod == emptyNode)
  65.     return {emptyNode, emptyNode};
  66.   push_rev(nod);
  67.   pt ans;
  68.   if (nod->l->sz >= poz) {
  69.     ans.second = nod;
  70.     pt aux = split(nod->l, poz);
  71.     ans.first = aux.first;
  72.     ans.second->l = aux.second;
  73.     update_sz(ans.second);
  74.   } else {
  75.     ans.first = nod;
  76.     pt aux = split(nod->r, poz - nod->l->sz - 1);
  77.     ans.first->r = aux.first;
  78.     ans.second = aux.second;
  79.     update_sz(ans.first);
  80.   }
  81.   return ans;
  82. }
  83.  
  84. treap* join(treap* st, treap* dr) {
  85.   push_rev(st), push_rev(dr);
  86.   if (st == emptyNode)
  87.     return dr;
  88.   if (dr == emptyNode)
  89.     return st;
  90.   if (st->priority > dr->priority) {
  91.     st->r = join(st->r, dr);
  92.     update_sz(st);
  93.     return st;
  94.   }
  95.   dr->l = join(st, dr->l);
  96.   update_sz(dr);
  97.   return dr;
  98. }
  99.  
  100. treap* add(treap* nod, int poz, int val) {
  101.   pt aux = split(nod, poz - 1);
  102.   treap* newNode = new treap{emptyNode, emptyNode, val, rng() % mod, 1, false};
  103.   return join(join(aux.first, newNode), aux.second);
  104. }
  105.  
  106. treap* invers(treap* nod, int st, int dr) {
  107.   pt p1 = split(nod, st - 1);
  108.   pt p2 = split(p1.second, dr - st + 1);
  109.   p2.first->rev ^= true;
  110.   return join(join(p1.first, p2.first), p2.second);
  111. }
  112.  
  113. treap* del(treap* nod, int st, int dr) {
  114.   pt p1 = split(nod, st - 1);
  115.   pt p2 = split(p1.second, dr - st + 1);
  116.   return join(p1.first, p2.second);
  117. }
  118.  
  119. int element(treap* nod, int poz) {
  120.   push_rev(nod);
  121.   if (nod->l->sz + 1 == poz)
  122.     return nod->val;
  123.   if (nod->l->sz >= poz)
  124.     return element(nod->l, poz);
  125.   return element(nod->r, poz - nod->l->sz - 1);
  126. }
  127.  
  128. void solve_query() {
  129.   char op;
  130.   fin >> op;
  131.   if (op == 'A') {
  132.     int k;
  133.     fin >> k;
  134.     fout << element(root, k) << '\n';
  135.     return;
  136.   }
  137.   if (op == 'I') {
  138.     int k, x;
  139.     fin >> k >> x;
  140.     root = add(root, k, x);
  141.     return;
  142.   }
  143.   if (op == 'R') {
  144.     int st, dr;
  145.     fin >> st >> dr;
  146.     root = invers(root, st, dr);
  147.     return;
  148.   }
  149.   int st, dr;
  150.   fin >> st >> dr;
  151.   root = del(root, st, dr);
  152. }
  153.  
  154. void solve() {
  155.   int Q;
  156.   bool ok_rev;
  157.   fin >> Q >> ok_rev;
  158.   for (int q = 0; q < Q; ++q)
  159.     solve_query();
  160.   for (int i = 1; i <= root->sz; ++i)
  161.     fout << element(root, i) << ' ';
  162.   fout << '\n';
  163. }
  164.  
  165. void close_files() {
  166.   fin.close();
  167.   fout.close();
  168. }
  169.  
  170. int main() {
  171.   solve();
  172.   close_files();
  173.   return 0;
  174. }
  175.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×