Cod sursa(job #1187198)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 17 mai 2014 20:31:58
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 11.03 kb
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <cstring>
#include <cassert>

template <typename Key, typename Comparator = std::less<Key> >
class Treap
{
public:

    Treap( Comparator comp = Comparator() );
    //Treap( const Treap & );
    //Treap& operator = ( const Treap & );

    ~Treap();

    ///class iterator;
    ///class const_iterator;

    ///typedef reverse_iterator<iterator> reverse_iterator;
    ///typedef reverse_iterator<const_iterator> const_reverse_iterator;

    ///std::pair <iterator, bool> insert( const Key &key );

    bool insert( const Key & );
    bool erase( const Key & );
    bool find( const Key & );

    Key kth_element( int ) const;

    ///iterator find( const Key &key );
    ///const_iterator find( const Key &key ) const;

    Key operator[] ( const int & ) const;
    Key at( const int & ) const;

    ///iterator begin();
    ///iterator end();
    ///const_iterator begin() const;
    ///const_iterator end() const;

    ///reverse_iterator rbegin();
    ///reverse_iterator rend();
    ///const_reverse_iterator rbegin();
    ///const_reverse_iterator rend();

    ///iterator lower_bound( const Key &key );
    ///iterator upper_bound( const Key &key );
    ///const_iterator lower_bound( const Key &key ) const;
    ///const_iterator upper_bound( const Key &key ) const;

    Key lower_bound( const Key & );
    Key upper_bound( const Key & );

    Key min_element() const;
    Key max_element() const;

    ///std::pair <iterator, iterator> equal_range( const Key &key );
    ///std::pair <const_iterator, const_iterator> equal_range( const Key &key ) const;

    size_t size() const;
    bool empty() const;
    void clear();
    //void swap( const Treap& );

    void inorder( std::vector <Key>& ) const;

private:

    struct Node
    {
        Key key;
        int priority;

        Node *left;
        Node *right;

        int nr_nodes;

        Node( const Key &, const int );
        Node( const Key &, const int , const int , Node *,  Node * );
    };

    void update( Node *& );
    void rotate_left_right( Node *& );
    void rotate_right_left( Node *& );
    void balance( Node *& );

    Node *ROOT;
    Comparator compare;
    size_t treapSize;

    ///std::pair <iterator, bool> insert( Node *&node, const Key &key, const int priority );
    bool insert( Node *&, const Key &, const int );
    bool erase( Node *&, const Key & );
    bool find( Node *, const Key & );

    Key kth_element( Node *, int ) const;

    Key lower_bound( Node *, const Key & );
    Key upper_bound( Node *, const Key & );

    Key min_element( Node * ) const;
    Key max_element( Node * ) const;

    void inorder( Node*, std::vector <Key>& ) const;

    void free_memory( Node *& );
};

template <typename Key, typename Comparator>
Treap <Key, Comparator>::Treap( Comparator comp )
{
    compare = comp;
    srand( time( NULL ) );
    ROOT = NULL;
    treapSize = 0;
}

template <typename Key, typename Comparator>
Treap <Key, Comparator>::~Treap()
{
    clear();
}

template <typename Key, typename Comparator>
void Treap <Key, Comparator>::free_memory( Node *&node )
{
    if ( node != NULL )
    {
        free_memory( node->left );
        free_memory( node->right );

        delete node;
        node = NULL;
    }
}

template <typename Key, typename Comparator>
Treap <Key, Comparator>::Node::Node( const Key &_key, const int _priority )
{
    key = _key;
    priority = _priority;
    nr_nodes = 1;
    left = NULL;
    right = NULL;
}

template <typename Key, typename Comparator>
Treap <Key, Comparator>::Node::Node( const Key &_key, const int _priority, const int _nr_nodes, Node *_left, Node *_right )
{
    key = _key;
    priority = _priority;
    nr_nodes = _nr_nodes;
    left = _left;
    right = _right;
}

template <typename Key, typename Comparator>
void Treap <Key, Comparator>::Treap::update( Node *&node )
{
    if ( node != NULL )
    {
        node->nr_nodes = 1;

        if ( node->left != NULL ) node->nr_nodes += node->left->nr_nodes;
        if ( node->right != NULL ) node->nr_nodes += node->right->nr_nodes;
    }
}

template <typename Key, typename Comparator>
void Treap <Key, Comparator>::rotate_left_right( Node *&node )
{
    Node *aux = node->left;
    node->left = aux->right;
    aux->right = node;

    update( aux );
    update( node );

    node = aux;
}

template <typename Key, typename Comparator>
void Treap <Key, Comparator>::rotate_right_left( Node *&node )
{
    Node *aux = node->right;
    node->right = aux->left;
    aux->left = node;

    update( aux );
    update( node );

    node = aux;
}

template <typename Key, typename Comparator>
void Treap <Key, Comparator>::balance( Node *&node )
{
    if ( node != NULL )
    {
        if ( node->left != NULL && node->left->priority > node->priority ) rotate_left_right( node );
        if ( node->right != NULL && node->right->priority > node->priority ) rotate_right_left( node );
    }
}

template <typename Key, typename Comparator>
size_t Treap <Key, Comparator>::size() const
{
    return treapSize;
}

template <typename Key, typename Comparator>
bool Treap <Key, Comparator>::empty() const
{
    return ( treapSize == 0 );
}

template <typename Key, typename Comparator>
void Treap <Key, Comparator>::clear()
{
    free_memory( ROOT );
    treapSize = 0;
}

template <typename Key, typename Comparator>
bool Treap <Key, Comparator>::insert( Node *&node, const Key &key, const int priority = rand() + 1 )
{
    bool value;

    if ( node == NULL )
    {
        node = new Node( key, priority, 1, NULL, NULL );
        value = true;
    }
    else
    {
        if ( compare( key, node->key ) )
                value = insert( node->left, key, priority );
        else
            if ( compare( node->key, key ) )
                    value = insert( node->right, key, priority );
            else
                    value = false;

        balance( node );
    }

    return value;
}

template <typename Key, typename Comparator>
bool Treap <Key, Comparator>::insert( const Key &key )
{
    bool value = insert( ROOT, key );

    if ( value == true )
    {
        treapSize++;
    }

    return value;
}

template <typename Key, typename Comparator>
bool Treap <Key, Comparator>::erase( Node *&node, const Key &key )
{
    if ( node == NULL ) return false;

    bool value;

    if ( compare( key, node->key ) )
            value = erase( node->left, key );
    else
        if ( compare( node->key, key ) )
                value = erase( node->right, key );
        else
        {
            if ( node->left == NULL && node->right == NULL )
            {
                delete node;
                node = NULL;
                value = true;
            }
            else
            {
                if ( node->left != NULL && node->right != NULL )
                {
                    if ( node->left->priority > node->right->priority )
                    {
                        rotate_left_right( node );
                        value = erase( node->right, key );
                    }
                    else
                    {
                        rotate_right_left( node );
                        value = erase( node->left, key );
                    }
                }
                else
                {
                    if ( node->left != NULL )
                    {
                        rotate_left_right( node );
                        value = erase( node->right, key );
                    }
                    else
                    {
                        rotate_right_left( node );
                        value = erase( node->left, key );
                    }
                }
            }
        }

    if ( node != NULL ) update( node );

    return value;
}

template <typename Key, typename Comparator>
bool Treap <Key, Comparator>::erase( const Key &key )
{
    bool value = erase( ROOT, key );

    if ( value == true )
        treapSize--;

    return value;
}

template <typename Key, typename Comparator>
bool Treap <Key, Comparator>::find( Node *node, const Key &key )
{
    if ( node == NULL )
            return false;

    if ( compare( key, node->key ) )
            return find( node->left, key );
    else
        if ( compare( node->key, key ) )
                return find( node->right, key );
        else
                return true;
}

template <typename Key, typename Comparator>
bool Treap <Key, Comparator>::find( const Key &key )
{
    return find( ROOT, key );
}

template <typename Key, typename Comparator>
Key Treap <Key, Comparator>::min_element( Node *node ) const
{
    assert( node != NULL );

    Key sol;

    while ( node != NULL )
    {
        sol = node->key;
        node = node->left;
    }

    return sol;
}

template <typename Key, typename Comparator>
Key Treap <Key, Comparator>::min_element() const
{
    return min_element( ROOT );
}

template <typename Key, typename Comparator>
Key Treap <Key, Comparator>::max_element( Node *node ) const
{
    assert( node != NULL );

    Key sol;

    while ( node != NULL )
    {
        sol = node->key;
        node = node->right;
    }

    return sol;
}

template <typename Key, typename Comparator>
Key Treap <Key, Comparator>::max_element() const
{
    return max_element( ROOT );
}

template <typename Key, typename Comparator>
Key Treap <Key, Comparator>::kth_element( Node *node, int position ) const
{
    assert( node != NULL );

    int s = 0;

    if ( node->left != NULL ) s = node->left->nr_nodes;

    if ( s + 1 == position )
            return node->key;

    if ( position <= s )
            return kth_element( node->left, position );
    else
            return kth_element( node->right, position - s - 1 );
}

template <typename Key, typename Comparator>
Key Treap <Key, Comparator>::kth_element( const int position ) const
{
    return kth_element( ROOT, position + 1 );
}

///Key operator[] ( const int & );
   /// Key at( const int & );

template <typename Key, typename Comparator>
Key Treap <Key, Comparator>::operator [] ( const int &position ) const
{
    return kth_element( ROOT, position + 1 );
}

template <typename Key, typename Comparator>
Key Treap <Key, Comparator>::at( const int &position ) const
{
    return kth_element( ROOT, position + 1 );
}


template <typename Key, typename Comparator>
void Treap <Key, Comparator>::inorder( Node *node, std::vector <Key> &v ) const
{
    if ( node != NULL )
    {
        inorder( node->left, v );
        v.push_back( node->key );
        inorder( node->right, v );
    }
}

template <typename Key, typename Comparator>
void Treap <Key, Comparator>::inorder( std::vector <Key> &v ) const
{
    inorder( ROOT, v );
}

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

    int N, type, key;

    Treap <int> T;

    scanf("%d", &N);

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

        if ( type == 1 )
        {
            T.insert( key );
        }

        if ( type == 2 )
        {
           T.erase( key );
        }

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

    return 0;
}