Cod sursa(job #1984221)

Utilizator cella.florescuCella Florescu cella.florescu Data 23 mai 2017 23:56:17
Problema Secv8 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3 kb
#include <bits/stdc++.h>

using namespace std;

struct TreapNode {
  int val, prio, wei;
  bool rev;
  TreapNode *ls, *rs;
  TreapNode (int p) : val(0), prio(p), wei(0), rev(false), ls(nullptr),  rs(nullptr) {}
  TreapNode (int v, TreapNode *l, TreapNode *r) : val(v), prio(rand()), wei(1), rev(false), ls(l), rs(r) {}
} *nil = new TreapNode(-1);

inline void update_wei(TreapNode *node) {
  node->wei = node->ls->wei + node->rs->wei + 1;
}

inline void propag_rev(TreapNode *node) {
  if (node->rev) {
    node->rev = false;
    swap(node->ls, node->rs);
    node->ls->rev ^= 1; node->rs->rev ^= 1;
  }
}

TreapNode* join(TreapNode *t1, TreapNode *t2) {
  if (t1 == nil)
    return t2;
  if (t2 == nil)
    return t1;
  propag_rev(t1); propag_rev(t2);
  if (t1->prio > t2->prio) {
    t1->rs = join(t1->rs, t2);
    update_wei(t1);
    return t1;
  }
  t2->ls = join(t1, t2->ls);
  update_wei(t2);
  return t2;
}

pair <TreapNode*, TreapNode*> split(TreapNode *t, int pos) {
  if (t == nil)
    return {nil, nil};
  propag_rev(t);
  if (t->ls->wei == pos) {
    TreapNode *aux = t->ls;
    t->ls = nil;
    update_wei(t);
    return {aux, t};
  }
  if (t->ls->wei > pos) {
   auto aux = split(t->ls, pos);
    t->ls = aux.second;
    update_wei(t);
    return {aux.first, t};
  }
  auto aux = split(t->rs, pos - t->ls->wei - 1);
  t->rs = aux.first;
  update_wei(t);
  return {t, aux.second};
}

TreapNode* join_3t(TreapNode *t1, TreapNode *t2, TreapNode *t3) {
  return join(t1, join(t2, t3));
}

tuple <TreapNode*, TreapNode*, TreapNode*> split_3t(TreapNode *t, int p1, int p2) {
  auto aux2 = split(t, p2);
  auto aux1 = split(aux2.first, p1);
  return make_tuple(aux1.first, aux1.second, aux2.second);
}

int get_kth(TreapNode *t, int pos) {
  propag_rev(t);
  if (t->ls->wei + 1 == pos)
    return t->val;
  if (t->ls->wei + 1 > pos)
    return get_kth(t->ls, pos);
  return get_kth(t->rs, pos - t->ls->wei - 1);
}

ofstream fout("secv8.out");

void print_treap(TreapNode *t) {
  if (t == nil)
    return;
  propag_rev(t);
  print_treap(t->ls);
  fout << t->val << " ";
  print_treap(t->rs);
}

int main()
{
    int n, x, y;
    char ch;
    ifstream fin("secv8.in");
    fin >> n >> x;
    srand(time(NULL));
    TreapNode *root = nil;
    for (n = n; n > 0; --n) {
      fin >> ch;
      switch (ch) {
        case 'I': {
          fin >> x >> y;
          auto aux = split(root, x - 1);
          root = join_3t(aux.first, new TreapNode(y, nil, nil), aux.second);
          break;
        } case 'A': {
          fin >> x;
          fout << get_kth(root, x) << '\n';
          break;
         } case 'R': {
          fin >> x >> y;
          auto aux = split_3t(root, x - 1, y);
          get<1>(aux)->rev ^= 1;
          root = join_3t(get<0>(aux), get<1>(aux), get<2>(aux));
          break;
        } case 'D': {
          fin >> x >> y;
          auto aux = split_3t(root, x - 1, y);
          root = join(get<0>(aux), get<2>(aux));
          break;
        }
      }
    }
    print_treap(root);
    fin.close();
    fout.close();
    return 0;
}