Cod sursa(job #1169239)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 10 aprilie 2014 18:50:11
Problema Hashuri Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 3.87 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <cassert>
#include <deque>

using namespace std;

const char infile[] = "hashuri.in";
const char outfile[] = "hashuri.out";

ofstream fout(outfile);

const int MAXN = 100005;
const int oo = 0x3f3f3f3f;

typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;

const inline int min(const int &a, const int &b) { if( a > b ) return b;   return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b;   return a; }
const inline void Get_min(int &a, const int b)    { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b)    { if( a < b ) a = b; }

const int lim = (1 << 20);
char buff[lim];
int pos;

inline void getInt(int &x) {
    x = 0;
    while(!('0' <= buff[pos] && buff[pos] <= '9'))
        if(++ pos == lim) {
            pos =0 ;
            fread(buff, 1, lim, stdin);
        }
    while('0' <= buff[pos] && buff[pos] <= '9') {
        x = x * 10 + buff[pos] - '0';
        if(++ pos == lim) {
            pos = 0;
            fread(buff, 1, lim, stdin);
        }
    }
}

struct Treap {
    Treap *left, *right;
    int value, priority;
    Treap(Treap *_left, Treap *_right, int _value, int _priority) {
        left = _left;
        right = _right;
        value = _value;
        priority = _priority;
    }
};

Treap *NIL = new Treap(0, 0, 0, 0);
Treap *Root = NIL;

inline bool Find(Treap *Node, int value) {
    if(Node == NIL)
        return 0;
    if(Node->value == value)
        return 1;
    if(Node->value > value)
        return Find(Node->left, value);
    if(Node->value < value)
        return Find(Node->right, value);
    assert(false);
}

inline void RotateRight(Treap *Node) {
    Treap *aux = Node->right;
    Node->right = aux->left;
    aux->left = Node;
    Node = aux;
}

inline void RotateLeft(Treap *Node) {
    Treap *aux = Node->left;
    Node->left = aux->right;
    aux->right = Node;
    Node = aux;
}

inline void Balance(Treap * &Node) {
    /// Balance the tree so that the heap property maintain
    if(Node->left->priority > Node->priority)
        RotateLeft(Node);
    if(Node->right->priority > Node->priority)
        RotateRight(Node);
}

inline void Insert(Treap * &Node, int value, int priority) {
    if(Node == NIL) {
        Node = new Treap(NIL, NIL, value, priority);
        return;
    }
    if(Node->value > value)
        Insert(Node->left, value, priority);
    else if(Node->value < value)
        Insert(Node->right, value, priority);
    else if(Node->value == value)
        return;
    Balance(Node);
}

inline void Erase(Treap * &Node, int value) {
    if(Node == NIL)
        return;
    if(Node->value > value)
        Erase(Node->left, value);
    else if(Node->value < value)
        Erase(Node->right, value);
    else {
        if(Node->left == NIL && Node->right == NIL) { /// yey it's a leaf
            delete Node;
            Node = NIL;
        }
        else if(Node->left->priority > value) {
                RotateLeft(Node);
                Erase(Node->left, value);
            } else {
                RotateRight(Node);
                Erase(Node->right, value);
            }
    }
}

int main() {
    freopen(infile, "r", stdin);
    int M;
    getInt(M);
    for(int i = 1 ; i <= M ; ++ i) {
        int op, x;
        getInt(op);
        getInt(x);
        switch(op) {
            case 1:
                if(!Find(Root, x))
                    Insert(Root, x, rand() + 1);
                break;
            case 2:
                if(Find(Root, x))
                    Erase(Root, x);
                break;
            case 3:
                fout << Find(Root, x) << '\n';
                break;
        }
    }
    fout.close();
    return 0;
}