Cod sursa(job #1023025)

Utilizator IonSebastianIon Sebastian IonSebastian Data 6 noiembrie 2013 12:27:58
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include <fstream>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
const int N = 200001;
int v[N], poz[N], h[N], nh;
void schimba(int p1, int p2){
    int aux;
    aux = h[p1];
    h[p1] = h[p2];
    h[p2] = aux;
    poz[h[p2]] = p2;
    poz[h[p1]] = p1;
}
void urca(int p){
    while(p != 1 && v[h[p]] < v[h[p/2]]){
        schimba(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){
        schimba(p, bun);
        coboara(bun);
    }
}
void sterge(int p){
    schimba(p,nh--);
    urca(p);
    coboara(p);
}
void adauga(int pozitie){
    h[++nh] = pozitie;
    poz[pozitie] = nh;
    urca(nh);
}
int main()
{
    int n, i, tip, val, citit = 0;
    in >> n;
    for(i = 1; i <= n; i++){
        in >> tip;
        if(tip == 1){
            in >> val;
            v[++citit] = val;
            adauga(citit);
        } else {
            if(tip == 2){
                in >> val;
                sterge(poz[val]);
            } else {
                out << v[h[1]] << " ";
            }
        }
    }
    return 0;
}