Cod sursa(job #2592549)

Utilizator Horia14Horia Banciu Horia14 Data 1 aprilie 2020 20:53:08
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.34 kb
#include<iostream>
#include<fstream>
#define HEAP_SIZE 200000
using namespace std;

int v[HEAP_SIZE + 1], h[HEAP_SIZE + 1], pos[HEAP_SIZE + 1];
int heapSize, k, n;

void Swap(int i, int j) {
    int aux = h[i];
    h[i] = h[j];
    h[j] = aux;
    aux = pos[h[i]];
    pos[h[i]] = pos[h[j]];
    pos[h[j]] = aux;
}

void heapDown(int i) {
    int l, r, p;
    l = 2 * i;
    r = 2 * i + 1;
    if(l <= heapSize && v[h[l]] < v[h[i]])
        p = l;
    else p = i;
    if(r <= heapSize && v[h[r]] < v[h[p]])
        p = r;
    if(p != i) {
        Swap(i, p);
        heapDown(p);
    }
}

void heapUp(int i) {
    while(v[h[i / 2]] > v[h[i]]) {
        Swap(i, i / 2);
        i >>= 1;
    }
}

inline int getMin() {
    return v[h[1]];
}

int main() {
    ifstream fin("heapuri.in");
    ofstream fout("heapuri.out");
    int op, x;
    fin >> n;
    v[0] = -1;
    for(int i = 0; i < n; ++i) {
        fin >> op;
        if(op < 3)
            fin >> x;
        if(op == 1) {
            ++k; ++heapSize;
            v[k] = x;
            h[heapSize] = k;
            pos[k] = heapSize;
            heapUp(heapSize);
        } else if(op == 2) {
            v[x] = -1;
			heapUp(pos[x]);
            Swap(1, heapSize);
            heapSize--;
            heapDown(1);
        } else fout << getMin() << "\n";
    }
    fin.close();
    fout.close();
    return 0;
}