# Cod sursa(job #2494654)

Utilizator Data 18 noiembrie 2019 11:05:33 Secv8 100 cpp-64 done Arhiva de probleme 2.89 kb
``````#include <bits/stdc++.h>

using namespace std;

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

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

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

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

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

Node* join(Node* a, Node* b) {
unrev(a);
unrev(b);
if (a == emptyNode)
return b;
if (b == emptyNode)
return a;
if (a->prio > b->prio) {
a->r = join(a->r, b);
recompute(a);
return a;
} else {
b->l = join(a, b->l);
recompute(b);
return b;
}
}

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

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

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

int access(Node* root, int pos) {
unrev(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);
}

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

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

return 0;
}
``````