Cod sursa(job #2808427)

Utilizator rares89_Dumitriu Rares rares89_ Data 25 noiembrie 2021 00:45:50
Problema Heapuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.19 kb
#include <fstream>

using namespace std;

ifstream fin("heapuri.in");
ofstream fout("heapuri.out");

int n, h[200005], c, x, poz[200005], nrHeap, m;

void upHeap(int k) {
    while(k > 1 && h[k] < h[k / 2]) {
        swap(h[k], h[k / 2]);
        k /= 2;
    }
}

void insertHeap(int x) {
    h[++nrHeap] = x;
    upHeap(nrHeap);
}

void downHeap(int k) {
    while(2 * k <= nrHeap) {
        int poz = 2 * k;
        if(2 * k + 1 <= nrHeap && h[poz] > h[2 * k + 1]) {
            poz = 2 * k + 1;
        }
        if(h[poz] < h[k]) {
            swap(h[poz], h[k]);
            k = poz;
        } else {
            break;
        }
    }
}

void deleteHeap(int k) {
    h[k] = h[nrHeap--];
    if(k > 1 && h[k] < h[k / 2]) {
        upHeap(nrHeap);
    } else {
        downHeap(nrHeap);
    }
}

int main() {
    fin >> n;
    for(int i = 1; i <= n; i++) {
        fin >> c;
        if(c == 1) {
            fin >> x;
            poz[++m] = x;
            insertHeap(x);
        } else if(c == 2) {
            fin >> x;
            deleteHeap(poz[x] - 1);
        } else if(c == 3) {
            fout << h[1] << "\n";
        }
    }
    return 0;
}