Cod sursa(job #1972721)

Utilizator felixiPuscasu Felix felixi Data 23 aprilie 2017 15:43:14
Problema Secv8 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.51 kb
#include <bits/stdc++.h>

using namespace std;

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

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

struct Treap
{
    int val, prio, g, lazy;
    Arbore left, right;
    Treap(int _val) : val(_val), prio(rand()), g(1), lazy(0), left(NIL), right(NIL) { }
    ~Treap() {
        if (left != NIL) delete left;
        if (right != NIL) delete right;
    }
};

Arbore join(Arbore a, Arbore b);
Paa split(Arbore a, int poz);
void recalc(Arbore x);
void lazy_prop(Arbore x);
Arbore insert(Arbore a, int poz, int val);
Arbore reverse(Arbore x, int st, int dr);
Arbore q_delete(Arbore x, int st, int dr);
int query_poz(Arbore x, int poz);
void AFIS(Arbore x);

int main()
{
    NIL = new Treap(0);
    NIL->g = 0;
    NIL->left = NIL->right = NIL;
    srand(time(0));

    int N, meh;
    in >> N >> meh;
    Arbore SMEK = NIL;
    for( int i = 1;  i <= N;  ++i ) {
        char op;
        in >> op;
        int a,b;
        if( op == 'I' ) {
            in >> a >> b;
            SMEK = insert( SMEK, a, b );
        }
        else if( op == 'A' ) {
            in >> a;
            out << query_poz(SMEK, a) << '\n';
        }
        else if( op == 'R' ) {
            in >> a >> b;
            SMEK = reverse(SMEK, a, b);
        }
        else {
            in >> a >> b;
            SMEK = q_delete(SMEK, a, b);
        }
    }
    AFIS( SMEK );
    return 0;
}

void AFIS(Arbore x) {
    lazy_prop(x);
    if( x == NIL ) return;
    AFIS(x->left);
    out << x->val << ' ';
    AFIS(x->right);
}

int query_poz(Arbore x, int poz) {
    lazy_prop( x );
    if( x->left->g+1 == poz ) {
        return x->val;
    }
    if( x->left->g >= poz ) {
        return query_poz(x->left, poz);
    }
    return query_poz(x->right, poz - x->left->g - 1);
}

Arbore reverse(Arbore x, int st, int dr) {
    Paa s1 = split(x, dr+1);
    Paa s2 = split(s1.first, st);
    s2.second->lazy ^= 1;
    return join(s2.first, join(s2.second, s1.second));
}

Arbore q_delete(Arbore x, int st, int dr) {
    Paa s1 = split(x, dr+1);
    Paa s2 = split(s1.first, st);
    //delete s2.second;
    return join(s2.first, s1.second);
}

Arbore insert(Arbore a, int poz, int val) {
    Arbore x = new Treap(val);
    Paa ans = split( a, poz );
    return join( join(ans.first, x), ans.second );
}

void lazy_prop(Arbore x) {
    if( x->lazy ) {
        swap( x->left, x->right );
        x->left->lazy ^= 1;
        x->right->lazy ^= 1;
    }
    x->lazy = 0;
}

void recalc( Arbore x ) {
    x->g = x->left->g + x->right->g + 1;
}

Paa split(Arbore a, int poz) {
    lazy_prop(a);

    if( poz > a->g ) return {a, NIL};

    if( poz == a->left->g+1 ) {
        Paa ans = {a->left, a};
        a->left = NIL;
        recalc(a);
        return ans;
    }
    if( poz <= a->left->g ) {
        Paa ans = split( a->left, poz );
        a->left = NIL;
        recalc(a);
        return {ans.first, join(ans.second, a)};
    }
    Paa ans = split( a->right, poz - a->left->g - 1 );
    a->right = NIL;
    recalc( a );
    return { join(a, ans.first), ans.second };
}

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

    if (a == NIL)
        return b;
    if (b == NIL)
        return a;

    if (a->prio > b->prio) {
        a->right = join(a->right, b);
        recalc(a);
        return a;
    }
    b->left = join( a, b->left );
    recalc(b);
    return b;
}