Cod sursa(job #2251131)

Utilizator Alex18maiAlex Enache Alex18mai Data 1 octombrie 2018 10:15:34
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 3.36 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 ("heapuri.in");ofstream cout ("heapuri.out");

int rng(){
    return rand() * RAND_MAX + rand() + 1;
}

int v[200100];

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

nod *root;
nod *nil;

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

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){
        now -> fv ++;
        return;
    }
    if (key < now -> 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 (key == now -> key){
        if (now -> fv > 1){
            now -> fv--;
            return;
        }
        if (now -> left == nil && now -> right == nil){
            delete now;
            now = nil;
            return;
        }
        if (now -> left -> priority > now -> right -> priority){
            rotleft (now);
            erase (now -> left , key);
        }
        else{
            rotright (now);
            erase (now -> right, key);
        }
        return;
    }

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

int query (nod *now){
    if (now -> left == nil){
        return now -> key;
    }
    return query (now -> left);
}

int main() {

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

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

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

    int n;
    cin>>n;

    int cont = 0;

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

        if (tip == 1){
            //insert
            int x;
            cin>>x;
            v[++cont] = x;
            insert (root , x , rng());
        }
        if (tip == 2){
            //erase al x-lea inserat
            int x;
            cin>>x;
            erase (root , v[x]);
        }
        if (tip == 3){
            //query
            int now = query (root);
            cout<<now<<'\n';
        }
    }

    return 0;
}