Cod sursa(job #2230385)

Utilizator MiricaMateiMirica Matei MiricaMatei Data 9 august 2018 22:05:49
Problema Secv8 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.93 kb
#include <bits/stdc++.h>

using namespace std;

struct Treap {
  Treap* l;
  Treap* r;
  int val;
  bool rev;
  int sz;
};

Treap* emptyTreap = new Treap {NULL, NULL, 0, 0, 0};

void recompute(Treap* root) {
  root->sz = root->l->sz + 1 + root->r->sz;
}

void unreverse(Treap* root) {
  if (root != emptyTreap && root->rev) {
    swap(root->l, root->r);
    root->rev = 0;
    root->l->rev ^= 1;
    root->r->rev ^= 1;
  }
}

Treap* join(Treap* a, Treap* b) {
  unreverse(a);
  unreverse(b);
  if (a == emptyTreap)
    return b;
  if (b == emptyTreap)
    return a;
  int x = rand() % 2;
  if (x) {
    a->r = join(a->r, b);
    recompute(a);
    return a;
  }
  b->l = join(a, b->l);
  recompute(b);
  return b;
}

pair<Treap*, Treap*> split(Treap* root, int pos) {
  if (root == emptyTreap)
    return {emptyTreap, emptyTreap};
  unreverse(root);
  pair<Treap*, Treap*> ans;
  if (root->l->sz >= pos) {
    ans.second = root;
    pair<Treap*, Treap*> aux = split(root->l, pos);
    ans.first = aux.first;
    ans.second->l = aux.second;
    recompute(ans.second);
  } else {
    ans.first = root;
    pair<Treap*, Treap*> aux = split(root->r, pos - root->l->sz - 1);
    ans.second = aux.second;
    ans.first->r = aux.first;
    recompute(ans.first);
  }
  return ans;
}

Treap* ins(Treap* root, int pos, int val) {
  Treap* newTreap = new Treap {emptyTreap, emptyTreap, val, 0, 1};
  pair<Treap*, Treap*> aux = split(root, pos - 1);
  return join(join(aux.first, newTreap), aux.second);
}

int access(Treap* root, int pos) {
  unreverse(root);
  if (root->l->sz + 1 == pos)
    return root->val;
  if (root->l->sz >= pos)
    return access(root->l, pos);
  return access(root->r, pos - root->l->sz - 1);
}

Treap* rev(Treap* root, int x, int y) {
  pair<Treap*, Treap*> aux1 = split(root, x - 1);
  pair<Treap*, Treap*> aux2 = split(aux1.second, y - x + 1);
  aux2.first->rev ^= 1;
  return join(join(aux1.first, aux2.first), aux2.second);
}

Treap* del(Treap* root, int x, int y) {
  pair<Treap*, Treap*> aux1 = split(root, x - 1);
  pair<Treap*, Treap*> aux2 = split(aux1.second, y - x + 1);
  return join(aux1.first, aux2.second);
}

int main() {
  freopen("secv8.in", "r", stdin);
  freopen("secv8.out", "w", stdout);

  srand(time(NULL));

  int n, m;
  scanf("%d%d ", &n, &m);
  Treap* root = emptyTreap;
  for (int i = 1; i <= n; ++i) {
    char c;
    scanf("%c", &c);
    if (c == 'I') {
      int k, e;
      scanf("%d%d ", &k, &e);
      root = ins(root, k, e);
    } else if (c == 'A') {
      int k;
      scanf("%d ", &k);
      printf("%d\n", access(root, k));
    } else if (c == 'R') {
      int x, y;
      scanf("%d%d ", &x, &y);
      root = rev(root, x, y);
    } else {
      int x, y;
      scanf("%d%d ", &x, &y);
      root = del(root, x, y);
    }
  }

  for (int i = 1; i <= root->sz; ++i)
    printf("%d ", access(root, i));

  return 0;
}