Cod sursa(job #2251650)

Utilizator Alex18maiAlex Enache Alex18mai Data 1 octombrie 2018 20:09:35
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 3.07 kb
//ALEX ENACHE

#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <unordered_map>
#include <time.h>
#include <iomanip>
#include <deque>
#include <math.h>
#include <cmath>
#include <assert.h>
#include <stack>
#include <bitset>
#include <random>
#include <chrono>

using namespace std;

//#include <iostream>

#include <fstream>
ifstream cin ("hashuri.in");ofstream cout ("hashuri.out");

struct nod {
    int key , priority;
    nod *left , *right;
    nod (int key , int priority , nod *left , nod *right){
        this -> key = key;
        this -> priority = priority;
        this -> left = left;
        this -> right = right;
    }
};

nod *root;
nod *nil;

void init(){
    nil = new nod(0 , 0 , NULL , NULL);
    root = nil;
}

int search (nod *now , int key){
    if (now == nil){
        return 0;
    }
    if (now -> key == key){
        return 1;
    }
    if (now -> key > key){
        return search (now -> left , key);
    }
    else{
        return search (now -> right , key);
    }
}

void rotleft (nod *&now){
    nod *aux = now -> left;
    now -> left = aux -> right;
    aux -> right = now;
    now = aux;
}

void rotright (nod *&now){
    nod *aux = now -> right;
    now -> right = aux -> left;
    aux -> left = now;
    now = aux;
}

void balance (nod *&now){
    if (now -> left -> priority > now -> priority){
        rotleft (now);
    }
    if (now -> right -> priority > now -> priority){
        rotright (now);
    }
}

void insert (nod *&now , int key , int priority){
    if (now == nil){
        now = new nod(key , priority , nil , nil);
        return;
    }
    if (now -> key == key){
        return;
    }
    if (now -> key > key){
        insert (now -> left , key , priority);
    }
    else{
        insert (now -> right , key , priority);
    }
    balance (now);
}

void erase (nod *&now , int key){
    if (now == nil){
        return;
    }

    if (now -> key == key){
        if (now -> left == nil && now -> right == nil){
            delete now;
            now = nil;
            return;
        }
        if (now -> left -> priority > now -> right -> priority){
            rotleft (now);
            erase (now -> right , key);
        }
        else{
            rotright (now);
            erase (now -> left , key);
        }
    }

    if (now -> key > key){
        erase (now -> left , key);
    }
    else{
        erase (now -> right , key);
    }
}

int main() {

    //freopen("input", "r", stdin);freopen("output", "w", stdout);

    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    srand(time(NULL));
    init();

    int n;
    cin>>n;

    for (int i=1; i<=n; i++){
        int tip , x;
        cin>>tip>>x;

        if (tip == 1){
            insert (root , x , rand() + 1);
        }
        if (tip == 2){
            erase (root , x);
        }
        if (tip == 3){
            cout<<search (root , x)<<'\n';
        }
    }

    return 0;
}