Cod sursa(job #2148824)

Utilizator felixiPuscasu Felix felixi Data 2 martie 2018 02:07:09
Problema Secv8 Scor 25
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.02 kb
#include <bits/stdc++.h>

using namespace std;

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

typedef struct Node* Arbore;
typedef pair<Arbore, Arbore> Paa;
Arbore NIL;

struct Node
{
    int prio, g, lazy, val;
    Arbore st, dr;

    Node(int _v) : prio(rand()), g(1), lazy(0), val(_v), st(NIL), dr(NIL) {}
};

void recalc(Arbore x)
{
    x->g = x->st->g + x->dr->g + (x != NIL);
    if( x->lazy ) {
        x->lazy = 0;
        swap(x->st, x->dr);
        x->st->lazy ^= 1;
        x->dr->lazy ^= 1;
    }
}

Arbore join(Arbore a, Arbore b)
{
    recalc(a);
    recalc(b);

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

Paa split(Arbore x, int pos)
{
    recalc(x);

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

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

Arbore T_delete(int x, int y, Arbore a)
{
    Paa w = split(a, y);
    Paa q = split(w.first, x - 1);
    return join(q.first, w.second);
}

Arbore T_insert(int pos, int val, Arbore a)
{
    Paa w = split(a, pos - 1);
    Arbore arb = new Node(val);
    return join(w.first, join(arb, w.second));
}

Arbore T_reverse(int x, int y, Arbore a)
{
    Paa w = split(a, y);
    Paa q = split(w.first, x - 1);
    q.second->lazy ^= 1;
    return join(q.first, join(q.second, w.second));
}

void afis(Arbore a)
{
    for( int i = 1;  i <= a->g;   ++i ) cout << T_elem(i, a) << ' ';
    cout << '\n';
}

int main()
{
    int Q, useless;
    NIL = new Node(0);
    NIL->g = NIL->lazy = NIL->prio = 0;
    NIL->st = NIL->dr = NIL;

    Arbore root = NIL;
    in >> Q >> useless;
    while(Q--) {
        char ch;
        in >> ch;
        if( ch == 'I' ) {
            int pos, val;
            in >> pos >> val;
            root = T_insert(pos, val, root);
        }
        else if( ch == 'A' ) {
            int pos;
            in >> pos;
            out << T_elem(pos, root) << '\n';
        }
        else if( ch == 'R' ) {
            int x,y;
            in >> x >> y;
            root = T_reverse(x,y,root);
        }
        else {
            int x,y;
            in >> x >> y;
            root = T_delete(x, y, root);
        }
        ///afis(root);
    }
    for( int i = 1;  i <= root->g;  ++i ) {
        out << T_elem(i, root) << ' ';
    }
    out << '\n';
    return 0;
}