Cod sursa(job #2152518)

Utilizator ContDeRacistAliniateEBlat ContDeRacist Data 5 martie 2018 16:59:10
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.18 kb
#include <fstream>

using namespace std;

ifstream cin("heapuri.in");
ofstream cout("heapuri.out");

const int N = 200002;

int nh(0), h[N], v[N], poz[N], numz(0);

void swapp(int p, int q) {
    swap(h[p], h[q]);
    poz[h[p]] = p;
    poz[h[q]] = q;
}

void urca(int p) {
    while (p > 1 && v[h[p]] < v[h[p / 2]]) {
        swapp(p, p / 2);
        p /= 2;
    }
}

void coboara(int p) {
    int fs(2 * p), fd(2 * p + 1), bun(p);
    if (fs <= nh && v[h[fs]] < v[h[bun]]) {
        bun = fs;
    }
    if (fd <= nh && v[h[fd]] < v[h[bun]]) {
        bun = fd;
    }
    if (bun != p) {
        swapp(p, bun);
        coboara(bun);
    }
}

void adauga(int val) {
    h[++nh] = ++numz;
    poz[numz] = nh;
    v[numz] = val;
    urca(nh);
}

void sterge(int p) {
    swapp(p, nh--);
    urca(p);
    coboara(p);
}

int main()
{
    int n, x, o;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> o;
        if (o == 1) {
            cin >> x;
            adauga(x);
        }
        if (o == 2) {
            cin >> x;
            sterge(poz[x]);
        }
        if (o == 3) {
            cout << v[h[1]] << "\n";
        }
        //cout << poz[4] << " poz[4]\n";
    }
    return 0;
}