Cod sursa(job #2653866)

Utilizator AlexandruabcdeDobleaga Alexandru Alexandruabcde Data 29 septembrie 2020 14:15:09
Problema Secv8 Scor 25
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.29 kb
#include <bits/stdc++.h>

using namespace std;

ifstream f ("secv8.in");
ofstream g ("secv8.out");

struct nod {
    nod *st, *dr;

    int prior;
    int val;
    int sz;

    bool R;
};

nod nil;

nod *Reverse (nod *T) {
    swap(T->st, T->dr);

    T->R = !T->R;

    return T;
}

nod *Propag (nod *T) {
    if (T != &nil && T->R) {
        T->st = Reverse(T->st);
        T->dr = Reverse(T->dr);
        T->R = 0;
    }

    return T;
}

nod *mod_fiu (nod *T, int care, nod *fiu) {
    if (care == 0) T->st = fiu;
    else T->dr = fiu;

    T->sz = T->st->sz + 1 + T->dr->sz;

    return T;
}

nod *join (nod *st, nod *dr) {
    st = Propag(st);
    dr = Propag(dr);
    if (st == &nil) return dr;
    if (dr == &nil) return st;

    if (st->prior >= dr->prior) {
        return mod_fiu(st, 1, join(st->dr, dr));
    }
    else return mod_fiu(dr, 0, join(st, dr->st));
}

pair <nod*, nod*> split (nod *T, int k) {
    T = Propag(T);
    if (T == &nil) return make_pair(&nil, &nil);

    if (T->st->sz >= k) {
        auto t = split(T->st, k);

        return make_pair(t.first, mod_fiu(T, 0, t.second));
    }
    else {
        auto t = split(T->dr, k - T->st->sz - 1);

        return make_pair(mod_fiu(T, 1, t.first), t.second);
    }
}

nod *Adaug (nod *T, int poz, int val) {
    T = Propag(T);
    pair <nod*, nod*> S = split(T, poz);

    nod *now = new nod;
    *now = nod{&nil, &nil, rand(), val, 1, 0};

    return join(join(S.first, now), S.second);
}

int Acces (nod *T, int poz) {
    T = Propag(T);
    auto t = split(T, poz+1);
    auto t_ = split(t.first, poz);

    Propag(t.first);
    Propag(t.second);
    Propag(t_.first);
    Propag(t_.second);

    T = join(join(t_.first, t_.second), t.second);

    return t_.second->val;
}

nod *Invers (nod *T, int x, int y) {
    T = Propag(T);
    auto t = split(T, y+1);
    auto t_ = split(t.first, x);

    Propag(t.first);
    Propag(t.second);
    Propag(t_.first);
    Propag(t_.second);

    t_.second = Reverse(t_.second);

    return join(join(t_.first, t_.second), t.second);
}

nod *Delete (nod *T, int x, int y) {
    T = Propag(T);
    auto t = split(T, y+1);
    auto t_ = split(t.first, x);

    Propag(t.first);
    Propag(t.second);
    Propag(t_.first);
    Propag(t_.second);

    return join(t_.first, t.second);
}

void Afis (nod *T) {
    if (T == &nil) return;

    Afis(T->st);
    g << T->val << " ";
    Afis(T->dr);
}

int main()
{
    srand(time(NULL));
    bool we = 0;
    int N;
    f >> N >> we;

    nod *T = &nil;

    for (; N; -- N ) {
        char op;
        f >> op;

        if (op == 'I') {
            int x, y;
            f >> x >> y;

            -- x;

            T = Adaug(T, x, y);
        }
        if (op == 'A') {
            int x;
            f >> x;
            --x;

            g << Acces(T, x) << '\n';
        }
        if (op == 'R') {
            int x, y;
            f >> x >> y;
            -- x;
            -- y;

            T = Invers(T, x, y);
        }
        if (op == 'D') {
            int x, y;
            f >> x >> y;

            -- x;
            -- y;

            T = Delete(T, x, y);
        }
    }

    Afis(T);

    return 0;
}