Cod sursa(job #1183912)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 10 mai 2014 16:09:43
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 2.85 kb
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>

using namespace std;

struct Treap
{
    int key;
    int priority;
    int nr_nodes;

    Treap *lf, *rt;

    Treap( const int _key, const int _priority, const int _nr_nodes, Treap *st, Treap *dr )
    {
        key = _key;
        priority = _priority;
        nr_nodes = _nr_nodes;
        lf = st;
        rt = dr;
    }

} *R, *NIL;

void initTreap()
{
    srand( time( NULL ) );
    NIL = new Treap( 0, 0, 1, NULL, NULL );
    R = NIL;
}

void update( Treap *&T )
{
    T->nr_nodes = 1;

    if ( T->lf ) T->nr_nodes += T->lf->nr_nodes;
    if ( T->rt ) T->nr_nodes += T->rt->nr_nodes;
}

void rol( Treap *&T )
{
    Treap *aux = T->lf;
    T->lf = aux->rt;
    aux->rt = T;

    update( T );
    update( aux );

    T = aux;
}

void ror( Treap *&T )
{
    Treap *aux = T->rt;
    T->rt = aux->lf;
    aux->lf = T;

    update( T );
    update( aux );

    T = aux;
}

void balance( Treap *&T )
{
    if ( T->lf->priority > T->priority ) rol( T );
    if ( T->rt->priority > T->priority ) ror( T );

    update( T );
}

void insert( Treap *&T, int value, int priority = rand() + 1 )
{
    if ( T == NIL )
    {
        T = new Treap( value, priority, 1, NIL, NIL );
    }
    else
    {
        if ( value < T->key )
                insert( T->lf, value, priority );
        else
            if ( value > T->key )
                    insert( T->rt, value, priority );

        balance( T );
    }
}

void erase( Treap *&T, int value )
{
    if ( T == NULL ) return;

    if ( value < T->key )
            erase( T->lf, value );
    else
        if ( value > T->key )
                erase( T->rt, value );
        else
        {
            if ( T->lf == NIL && T->rt == NIL )
            {
                delete T;
                T = NIL;
            }
            else
            {
                if ( T->lf->priority > T->rt->priority )
                {
                    rol( T );
                    erase( T->rt, value );
                }
                else
                {
                    ror( T );
                    erase( T->lf, value );
                }
            }
        }

    if ( T ) update( T );
}

int find( Treap *T, int value )
{
    if ( T == NIL ) return 0;
    if ( T->key == value ) return 1;
    if ( value < T->key ) return find( T->lf, value );
    else                  return find( T->rt, value );
}

int main()
{
    freopen("hashuri.in", "r", stdin);
    freopen("hashuri.out", "w", stdout);

    int N, type, key;

    scanf("%d", &N);

    initTreap();

    while ( N-- )
    {
        scanf("%d %d", &type, &key);

        if ( type == 1 )
        {
            insert( R, key );
        }

        if ( type == 2 )
        {
           erase( R, key );
        }

        if ( type == 3 )
        {
            printf("%d\n", find( R, key ));
        }
    }

    return 0;
}