Cod sursa(job #1923309)

Utilizator vladm98Munteanu Vlad vladm98 Data 10 martie 2017 22:18:37
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 3.98 kb
#include <bits/stdc++.h>

using namespace std;
int n;
default_random_engine generator;
uniform_int_distribution<int> distribution (1, 2000000000);
class Treapuri
{
public:
    struct treap
    {
        treap *st, *dr;
        int val;
        int greutate;
        treap (treap *stanga, treap *dreapta, int valuare, int gr)
        {
            this->st = stanga;
            this->dr = dreapta;
            this->val = valuare;
            this->greutate = gr;
        }
    };
    treap *NILL, *root;
    void LeftRotate (treap *& node)
    {
        treap *nou;
        nou = node->st;
        node->st = nou->dr;
        nou->dr = node;
        node = nou;
    }
    void RightRotate (treap *& node)
    {
        treap *nou;
        nou = node->dr;
        node->dr = nou->st;
        nou->st = node;
        node = nou;
    }
    void Balance (treap *& node)
    {
        if (node->st!=NILL && node->st->greutate > node->greutate)
            LeftRotate(node);
        if (node->dr!=NILL && node->dr->greutate > node->greutate)
            RightRotate(node);
    }
    void Start()
    {
        NILL = new treap (NULL, NULL, -1, 0);
        root = NILL;
    }
    void Add_value (treap *& node, int valoare)
    {
        if (node == NILL)
        {
            node = new treap(NILL, NILL, valoare, distribution(generator));
            return;
        }
        if (node->val > valoare)
            Add_value(node->st, valoare);
        else Add_value(node->dr, valoare);
        Balance(node);
    }
    bool Find_element (treap *& node, int valoare)
    {
        if (node == NILL)
            return 0;
        if (node->val == valoare)
            return 1;
        if (node->val > valoare)
            return Find_element(node->st, valoare);
        return Find_element(node->dr, valoare);
    }
    void Erase_element (treap *& node, int valoare)
    {
        if (node == NILL)
            return;
        if (node->val == valoare)
        {
            if (node->st==NILL && node->dr==NILL)
            {
                delete(node);
                node = NILL;
                return;
            }
            if (node->st!=NILL && node->dr!=NILL)
            {
                if (node->st->greutate > node->dr->greutate)
                {
                    LeftRotate(node);
                    Erase_element(node->dr, valoare);
                }
                else
                {
                    RightRotate(node);
                    Erase_element(node->st, valoare);
                }
                return;
            }
            if (node->st!=NILL)
            {
                LeftRotate(node);
                Erase_element(node->dr, valoare);
                return;
            }
            RightRotate(node);
            Erase_element(node->st, valoare);
            return;
        }
        if (node->val > valoare)
            Erase_element(node->st, valoare);
        else Erase_element(node->dr, valoare);
    }
} *Tr;
int readInt () {
	bool minus = false;
	int result = 0;
	char ch;
	ch = getchar();
	while (true) {
		if (ch == '-') break;
		if (ch >= '0' && ch <= '9') break;
		ch = getchar();
	}
	if (ch == '-') minus = true; else result = ch-'0';
	while (true) {
		ch = getchar();
		if (ch < '0' || ch > '9') break;
		result = result*10 + (ch - '0');
	}
	if (minus)
		return -result;
	else
		return result;
}
int main()
{
    freopen ("hashuri.in", "r", stdin);
    freopen ("hashuri.out", "w", stdout);
    n = readInt();
    Tr = new Treapuri;
    Tr->Start();
    while (n--)
    {
        int tip, x;
        tip = readInt();
        x = readInt();
        if (tip == 1 && !Tr->Find_element(Tr->root, x))
            Tr -> Add_value (Tr -> root, x);
        if (tip == 2 && Tr->Find_element(Tr->root, x))
            Tr->Erase_element (Tr->root, x);
        if (tip == 3)
            printf("%d\n", Tr->Find_element(Tr->root, x));
    }
    return 0;
}