Cod sursa(job #1845711)

Utilizator FlorinHajaFlorin Gabriel Haja FlorinHaja Data 11 ianuarie 2017 20:24:57
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.24 kb
#include <fstream>

using namespace std;

ifstream f("heapuri.in");
ofstream g("heapuri.out");

int n, q, x;
int a[200005], hp[200005];
int poz[200005], l, nr;

void siftup(int x) {
    while (x/2 && a[hp[x]] < a[hp[x/2]]) {
        swap(hp[x], hp[x/2]);
        poz[hp[x]] = x;
        poz[hp[x/2]] = x/2;
        x/=2;
    }
}

void siftdown(int x) {
    int y;
    while (x != y) {
        y = x;
        if (y*2 <= l && a[hp[y]] > a[hp[y*2]])
            y *= 2;
        if (y*2+1 <= l && a[hp[y]] > a[hp[y*2+1]])
            y = y*2+1;

        swap(hp[x], hp[y]);
        poz[hp[x]] = x;
        poz[hp[y]] = y;
    }
}

int main() {
    f >> n;
    while (n--) {
        f >> q;
        if (q == 1) {
            f >> x;
            l++, nr++;
            poz[nr] = l;
            a[nr] = x;
            hp[l] = nr;
            siftup(l);
        }
        else if (q == 2) {
            f >> x;
            a[x] = -1;
            siftup(poz[x]);
            poz[hp[1]] = 0;
            hp[1] = hp[l];
            l--;
            poz[hp[1]] = 1;
            if (l>1)
                siftdown(1);
        }
        else if (q == 3) {
            g << a[hp[1]] << '\n';//
        }
    }
    return 0;
}