Cod sursa(job #2247115)

Utilizator HumikoPostu Alexandru Humiko Data 27 septembrie 2018 21:45:29
Problema Hashuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 3.99 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ("hashuri.in");
ofstream fout ("hashuri.out");

class TREAPS
{
    public:
        struct TREAP
        {
            TREAP *left_Son;
            TREAP *right_Son;
            int value;
            int weight;

            TREAP ( TREAP *left_Son, TREAP *right_Son, int value, int weight)
            {
                this->left_Son = left_Son;
                this->right_Son = right_Son;
                this->value = value;
                this->weight = weight;
            }
        };

    TREAP *root, *NILL;

    TREAPS ()
    {
        srand(time(NULL));
        NILL = new TREAP(NULL, NULL, 0, 0);
        root = NILL;
    }

    void rotateLeftNode( TREAP *&node )
    {
        TREAP *aux;

        aux = node->left_Son;
        node->left_Son = aux->right_Son;
        aux->right_Son = node;
        node = aux;
    }

    void rotateRightNode ( TREAP *&node )
    {
        TREAP *aux;

        aux = node->right_Son;
        node->right_Son = aux->left_Son;
        aux->left_Son = node;
        node = aux;
    }

    void balance ( TREAP *&node )
    {
        if ( node->left_Son != NILL && node->left_Son->weight < node->weight )
            rotateLeftNode(node->left_Son);
        if ( node->right_Son != NILL && node->right_Son->weight < node->weight )
            rotateRightNode(node->right_Son);
    }

    bool findValue ( TREAP *&node, int value )
    {
        if ( node->value == value )
            return true;
        if ( node == NILL )
            return false;

        if ( value > node->value )
            return findValue(node->right_Son, value);
        else
            return findValue(node->left_Son, value);
    }

    void insertValue ( TREAP *&node, int value )
    {
        if ( node == NILL )
        {
            node = new TREAP(NILL, NILL, value, rand()%666013+1);
            return;
        }

        if ( value > node->value )
            insertValue( node->right_Son, value);
        else
            insertValue( node->left_Son, value);
    }

    void eraseValue ( TREAP *&node, int value )
    {
        if  ( node == NILL )
            return;

        if ( node->value > value )
            eraseValue( node->left_Son, value);

        if ( node->value < value )
            eraseValue( node->right_Son, value);

        if ( node->value == value )
        {
            if ( node->left_Son == NILL && node->right_Son == NILL )
            {
                delete(node);
                node = NILL;
                return;
            }

            if ( node->left_Son != NILL && node->right_Son != NILL )
            {
                if ( node->left_Son->weight < node->right_Son->weight )
                    rotateLeftNode(node->left_Son);
                else
                    rotateRightNode(node->right_Son);

                eraseValue(node, value);
            }
            else
            {
                if ( node->left_Son != NILL )
                    rotateLeftNode(node->left_Son);
                else
                    rotateRightNode(node->right_Son);

                eraseValue(node, value);
            }
        }

        balance(node);
    }
};

int n, operation, x;

int main()
{
    TREAPS *hashing;
    hashing = new TREAPS();

    fin>>n;

    for ( int i = 1; i <= n; ++i )
    {
        fin>>operation>>x;

        if ( operation == 1 )
        {
            if ( hashing->findValue(hashing->root, x) == false )
                hashing->insertValue(hashing->root, x);
        }

        if ( operation == 2 )
        {
            if ( hashing->findValue(hashing->root, x) == true )
                hashing->eraseValue(hashing->root, x);
        }

        if ( operation == 3 )
        {
            if ( hashing->findValue(hashing->root, x) == true )
                fout<<1<<'\n';
            else
                fout<<0<<'\n';
        }
    }
}