Cod sursa(job #1183817)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 10 mai 2014 11:25:09
Problema Hashuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 2.98 kb
#include <iostream>
#include <fstream>
#include <set>
#include <cstdlib>
#include <ctime>

using namespace std;

const int Nodes_Max = 1000000;

struct Treap
{
    int key;
    int priority;
    int left, right;

    Treap()
    {
        key = 0;
        priority = -1;
        left = 0;
        right = 0;
    }

    void Tclear( int a, int b )
    {
        key = a;
        priority = b;
        left = right = 0;
    }

} T[Nodes_Max + 1];

int max_dim, root;


inline void rol( int &node )
{
    int aux = T[node].left;
    T[node].left = T[aux].right;
    T[aux].right = node;

    node = aux;
}

inline void ror( int &node )
{
    int aux = T[node].right;
    T[node].right = T[aux].left;
    T[aux].left = node;

    node = aux;
}

inline void balance( int &node )
{
    if ( T[ T[node].left ].priority > T[node].priority )
            rol( node );

    if ( T[ T[node].right ].priority > T[node].priority )
            ror( node );
}

void insert( int &node, int key, int prior = rand() + 1 )
{
    if ( node == 0 )
    {
        node = ++max_dim;
        T[node].Tclear( key, prior );
    }
    else
    {
        if ( key < T[node].key )
                insert( T[node].left, key, prior );
        else
            if ( key > T[node].key )
                    insert( T[node].right, key, prior );

        balance( node );
    }
}

void erase( int &node, int key )
{
    if ( node == 0 ) return;

    if ( key < T[node].key )
            erase( T[node].left, key );
    else
        if ( key > T[node].key )
                erase( T[node].right, key );
        else
        {
            if ( T[node].left == 0 && T[node].right == 0 )
            {
                node = 0;
            }
            else
            {
                if ( T[node].left && T[node].right )
                {
                    if ( T[ T[node].left ].priority > T[ T[node].right ].priority )
                            rol( T[node].left );
                    else
                            ror( T[node].right );
                }
                else
                {
                    if ( T[node].left )
                            rol( T[node].left );
                    else
                            ror( T[node].right );
                }

                erase( node, key );
            }
        }
}

int find( int ind, int value )
{
    if ( ind == 0 )   return 0;
    if ( T[ind].key == value ) return 1;
    if ( T[ind].key > value )  return find( T[ind].left, value );
    else                         return find( T[ind].right, value );
}

int N, type, key;

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

    srand( time( NULL ) );

    scanf("%d", &N);

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

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

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

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

    return 0;
}