Cod sursa(job #2301673)

Utilizator Tiberiu02Tiberiu Musat Tiberiu02 Data 13 decembrie 2018 13:22:55
Problema Secv8 Scor 100
Compilator cpp-32 Status done
Runda Arhiva de probleme Marime 3.03 kb
#include <iostream>
#include <cstdlib>
#include <cstdio>

using namespace std;

struct Treap;
typedef Treap* Arbore;
typedef pair<Arbore, Arbore> Paa;

Arbore NIL;

Treap* raw;
int raw_ix = 0;

struct Treap {
    int val, prio, g, rev;
    Treap *st, *dr;

    Treap(int val = -1) : val(val), prio(rand()), g(1), rev(0), st(NIL), dr(NIL) {}
    void recalc() {
        g = st->g + dr->g + 1;
    }
    void prop() {
        if (rev) {
            swap(st, dr);
            st->rev ^= 1;
            dr->rev ^= 1;
            rev = 0;
        }
    }
};

Arbore new_treap(int v) {
    raw[raw_ix] = Treap(v);
    return &raw[raw_ix++];
}

Arbore join(Arbore a, Arbore b) {
    if (a == NIL) return b;
    if (b == NIL) return a;
    a->prop();
    b->prop();

    if (a->prio > b->prio) {
        a->dr = join(a->dr, b);
        a->recalc();
        return a;
    } else {
        b->st = join(a, b->st);
        b->recalc();
        return b;
    }
}

/// Split dupa nodul cu pozitia 'pos',
/// pozitiile indexate de la 1
/// bucata din stanga va avea pos noduri
Paa split(Arbore x, int pos) {
    if (x == NIL) return {NIL, NIL};
    x->prop();

    if (pos <= x->st->g) {
        Paa s = split(x->st, pos);
        x->st = s.second;
        x->recalc();
        return {s.first, x};
    } else {
        Paa s = split(x->dr, pos - x->st->g - 1);
        x->dr = s.first;
        x->recalc();
        return {x, s.second};
    }
}

int get_el(Arbore x, int pos) {
    x->prop();
    if (pos <= x->st->g)
        return get_el(x->st, pos);
    if (pos == x->st->g + 1)
        return x->val;
    return get_el(x->dr, pos - x->st->g - 1);
}

void print(Arbore root) {
    for (int i = 1; i <= root->g; ++i)
        printf("%d ", get_el(root, i));
    printf("\n");
}


int main() {
    raw = new Treap[300000];

    NIL = new_treap(-1);
    NIL->g = 0;
    NIL->prio = -1;
    NIL->st = NIL->dr = NIL;


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

    int n, k;
    scanf("%d%d ", &n, &k);
    Arbore root = NIL;
    for (int i = 1; i <= n; i++) {
        char c;
        scanf("%c", &c);
        if (c == 'I') {
            int k, e;
            scanf("%d%d ", &k, &e);
            Paa s = split(root, k - 1);
            root = join(s.first, join(new_treap(e), s.second));
        } else if (c == 'A') {
            int k;
            scanf("%d ", &k);
            printf("%d\n", get_el(root, k));
        } else if (c == 'R') {
            int l, r;
            scanf("%d%d ", &l, &r);
            Paa s1 = split(root, l - 1);
            Paa s2 = split(s1.second, r - l + 1);
            s2.first->rev ^= 1;
            root = join(s1.first, join(s2.first, s2.second));
        } else {
            int l, r;
            scanf("%d%d ", &l, &r);
            Paa s1 = split(root, l - 1);
            Paa s2 = split(s1.second, r - l + 1);
            root = join(s1.first, s2.second);
        }
    }

    print(root);
    return 0;
}