Cod sursa(job #2058074)

Utilizator MiricaMateiMirica Matei MiricaMatei Data 5 noiembrie 2017 04:45:08
Problema Secv8 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.07 kb
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <ctime>

using namespace std;

struct Node {
  Node* l;
  Node* r;
  int val, prio, sz;
  bool rev;

};

Node* emptyNode = new Node {NULL, NULL, 0, 0, 0, 0};

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

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

Node* join(Node* root1, Node* root2) {
  unreverse(root1);
  unreverse(root2);
  if (root1 == emptyNode) {
    return root2;
  } else if (root2 == emptyNode) {
    return root1;
  } else if (root1->prio > root2->prio) {
    root1->r = join(root1->r, root2);
    recompute(root1);
    return root1;
  } else {
    root2->l = join(root1, root2->l);
    recompute(root2);
    return root2;
  }
}

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

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

Node* r(Node* root, int st, int dr) {
  pair<Node*, Node*>aux = split(root, st - 1);
  pair<Node*, Node*>aux1 = split(aux.second, dr - st + 1);
  aux1.first->rev ^= 1;
  return join(join(aux.first, aux1.first), aux1.second);
}

Node* d(Node* root, int st, int dr) {
  pair<Node*, Node*>aux = split(root, st - 1);
  pair<Node*, Node*>aux1 = split(aux.second, dr - st + 1);
  return join(aux.first, aux1.second);
}

Node* ins(Node* root, int k, int val) {
  pair<Node*, Node*>aux = split(root, k - 1);
  Node* nod = new Node {emptyNode, emptyNode, val, rand(), 1, 0};
  return join(join(aux.first, nod), aux.second);
}

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

  int n, k;
  scanf("%d%d ", &n, &k);
  Node* root = emptyNode;

  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 == 'R') {
      int st, dr;
      scanf("%d%d ", &st, &dr);
      root = r(root, st, dr);
    } else if (c == 'A') {
      int k;
      scanf("%d ", &k);
      printf("%d\n", access(root, k));
    } else {
      int st, dr;
      scanf("%d%d ", &st, &dr);
      root = d(root, st, dr);
    }
  }

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

  return 0;
}