Cod sursa(job #1816855)

Utilizator Tiberiu02Tiberiu Musat Tiberiu02 Data 27 noiembrie 2016 00:55:48
Problema Secv8 Scor 25
Compilator cpp Status done
Runda Arhiva de probleme Marime 5.35 kb
# include <iostream>
# include <fstream>
# include <cstdlib>
# include <cassert>

using namespace std;

struct Node
{
    int val, w;
    int prio;
    Node * left, * right;
    bool rvs;

    Node( int _val, int _prio = -2, Node * _st = NULL, Node * _dr = NULL )
    {
        rvs = 0;
        val = _val;
        if ( _prio == -2 )
            prio = rand();
        else
            prio = _prio;
        left = _st;
        right = _dr;
        w = ( left == NULL ? 0 : left->w ) + ( right == NULL ? 0 : right->w ) + 1;
    }
};

class Treap
{
public:
    Treap();
    int kth( int pos );
    void push( int val, int pos );
    void pop( int pos );
    void pop( int a, int b );
    void reverse( int a, int b );
    int size();
    void print( ofstream &fout );

private:
    Node * root;

    int search( Node * &n, int pos );
    void insert( Node * &n, int pos, int val, int prio );
    void erase( Node * &n, int pos );
    void split( Node * &n, int pos, Node * &t1, Node * &t2 );
    void join( Node * &t1, Node * &t2, Node * &t );
    int weight( Node * &n );
    void updateWeight( Node * &n );
    void prop( Node * &n );
    void balance( Node * &n );
    void rotLeft( Node * &n );
    void rotRight( Node * &n );
    void printNode( Node * n, ofstream &fout );
};

void Treap::printNode( Node * n, ofstream &fout )
{
    if ( n == NULL )
        return;

    prop( n );

    printNode( n->left, fout );
    fout << n->val << ' ';
    printNode( n->right, fout );
}

void Treap::print( ofstream &fout )
{
    printNode( root, fout );
    fout << endl;
}

void Treap::rotRight( Node * &n )
{
    prop( n->right );

    Node * t = n->right;
    n->right = t->left;
    t->left = n;
    n = t;
}

void Treap::rotLeft( Node * &n )
{
    prop( n->left );

    Node * t = n->left;
    n->left = t->right;
    t->right = n;
    n = t;
}

void Treap::balance( Node * &n )
{
    if ( n->prio == -1 && n->left == NULL && n->right == NULL ) {
        delete n;
        n = NULL;

        return;
    }

    prop( n );

    if ( n->left != NULL && n->prio < n->left->prio ) {
        rotLeft( n );
        balance( n->right );
    } if ( n->right != NULL && n->prio < n->right->prio ) {
        rotRight( n );
        balance( n->left );
    }

    updateWeight( n );
}

void Treap::prop( Node * &n )
{
    if ( n != NULL && n->rvs ) {
        swap( n->left, n->right );

        if ( n->left != NULL )
            n->left->rvs ^= 1;
        if ( n->right != NULL )
            n->right->rvs ^= 1;

        n->rvs = 0;
    }
}

void Treap::updateWeight( Node * &n )
{
    if ( n != NULL )
        n->w = 1 + weight( n->left ) + weight( n->right );
}

int Treap::weight( Node * &n )
{
    return ( n == NULL ? 0 : n->w );
}

void Treap::join( Node * &t1, Node * &t2, Node * &t )
{
    t = new Node( 0, -1, t1, t2 );
    updateWeight( t );
    prop( t );
    balance( t );
}

void Treap::split( Node * &n, int pos, Node * &t1, Node * &t2 )
{
    insert( n, pos, 0, RAND_MAX );
    t1 = n->left;
    t2 = n->right;


    delete n;
    n = NULL;
}

void Treap::erase( Node * &n, int pos )
{
    if ( pos <= weight( n->left ) )
        erase( n->left, pos );
    else if ( pos == weight( n->left ) + 1 )
        n->prio = -1;
    else
        erase( n->right, pos - weight( n->left ) - 1 );

    balance( n );
}

void Treap::insert( Node * &n, int pos, int val, int prio = -2 )
{
    prop( n );

    if ( n == NULL )
        n = new Node( val, prio );
    else {
        if ( pos <= weight( n->left ) )
            insert( n->left, pos, val, prio );
        else
            insert( n->right, pos - weight( n->left ) - 1, val, prio );
    }

    balance( n );
    updateWeight( n );
}

int Treap::search( Node * &n, int pos )
{
    if ( pos <= weight( n->left ) )
        return search( n->left, pos );
    else if ( pos == 1 + weight( n->left ) )
        return n->val;
    else
        return search( n->right, pos - weight( n->left ) - 1 );
}

int Treap::size()
{
    return weight( root );
}

void Treap::reverse( int a, int b )
{
    assert( a >= 1 && b >= a && size() >= b );
    Node * n1, *aux, * n2, * n3;
    split( root, a - 1, n1, aux ); //cout << "asd" << endl;
    split( aux, b - a + 1, n2, n3 );
    n2->rvs ^= 1;
    join( n2, n3, aux );
    join( n1, aux, root );
}

void Treap::pop( int a, int b )
{
    assert( 1 <= a && a <= b && b <= size() );
    Node * n1, *aux, * n2, * n3;
    split( root, a - 1, n1, aux );
    split( aux, b - a + 1, n2, n3 );
    join( n1, n3, root );
}

int Treap::kth( int pos )
{
    assert( pos > 0 && pos <= size() );
    return search( root, pos );
}

void Treap::push( int val, int pos )
{
    assert( pos >= 1 && pos <= size() + 1 );
    insert( root, pos - 1, val );
}

void Treap::pop( int pos )
{
    assert( pos > 0 && pos <= size() );
    erase( root, pos );
}

Treap::Treap()
{
    root = NULL;
    srand( time( NULL ) );
}

int main()
{
    ifstream fin( "secv8.in" );
    ofstream fout( "secv8.out" );

    int n, r, i, a, b;
    char ch;
    Treap t;

    fin >> n >> r;

    for ( i = 0; i < n; i ++ ) {
        fin >> ch;

        if ( ch == 'I' ) {
            fin >> a >> b;
            t.push( b, a );
        } else if ( ch == 'A' ) {
            fin >> a;
            fout << t.kth( a ) << '\n';
        } else if ( ch == 'R' ) {
            fin >> a >> b;
            t.reverse( a, b );
        } else {
            fin >> a >> b;
            t.pop( a, b );
        }
    }

    t.print( fout );

    return 0;
}