Cod sursa(job #1964711)

Utilizator nicu_serteSerte Nicu nicu_serte Data 13 aprilie 2017 17:09:55
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.44 kb
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
#define nmax 200005
int h[nmax], v[nmax], pozh[nmax], n, nr;
void promoveaza(int nod)
{
    while(nod/2 && v[h[nod/2]]>v[h[nod]])
    {
        swap(h[nod/2], h[nod]);
        pozh[h[nod]]=nod;
        pozh[h[nod/2]]=nod/2;
    }
}
void add(int x)
{
    n++; nr++;
    v[nr]=x;
    h[n]=nr;
    pozh[nr]=n;
    promoveaza(n);
}
void sterge(int x)
{
    int k, son=0;
    k=pozh[x];
    h[k]=h[n];
    pozh[h[n]]=0;
    n--;
    pozh[h[k]]=k;
    if(k/2 && v[h[k]]<v[h[k/2]])
        promoveaza(k);
    else
    {
        son=0;
        if(2*k<=n)
            son=2*k;
        if(2*k+1<=n && v[h[2*k+1]]<v[h[son]])
            son=2*k+1;
        while(son && v[h[k]]>v[h[son]] && k<=n)
        {
            swap(h[k], h[son]);
            pozh[h[k]]=k;
            pozh[h[son]]=son;
            k=son;
            son=0;
            if(2*k<=n)
                son=2*k;
            if(2*k+1<=n && v[h[2*k+1]]<v[h[son]])
                son=2*k+1;
        }
    }
}
int main()
{
    int n, cod, x;
    fin>>n;
    while(n)
    {
        n--;
        fin>>cod;
        if(cod==1)
        {
            fin>>x;
            add(x);
        }
        else if(cod==2)
        {
            fin>>x;
            sterge(x);
        }
        else if(cod==3)
            fout<<v[h[1]]<<'\n';
    }
    return 0;
}