Cod sursa(job #1523703)

Utilizator ThomasFMI Suditu Thomas Thomas Data 13 noiembrie 2015 00:38:10
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
#include <fstream>
using namespace std;

#define NMax 200005
#define inf 0x7fffffff

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

int H[NMax], h;
int v[NMax], ct;
bool elim[NMax];

void ins(int x)
{
    H[++h] = x;
    int father, nod = h;
    while(nod > 1)
    {
        father = nod>>1;
        if(v[H[father]] > v[H[nod]]) swap(H[father], H[nod]);
        else break;
        nod = father;
    }
}

void del()
{
    H[1] = H[h];
    H[h--] = 0;
    int nod = 1;
    while(nod <= h)
    {
        int son1 = nod<<1;
        int son2 = son1|1;
        if(v[H[nod]] > v[H[son1]] || v[H[nod]] > v[H[son2]])
        {
            if(v[H[son1]] <= v[H[son2]])
            {
                swap(H[nod], H[son1]);
                nod = son1;
            }
            else
            {
                swap(H[nod], H[son2]);
                nod = son2;
            }
        }
        else break;
    }
}

int getmin()
{
    while(elim[H[1]])  del();
    return v[H[1]];
}

int main()
{
    int Q,p,x;

    v[H[0]] = inf;

    f>>Q;
    while(Q--)
    {
        f>>p;
        if(p == 1) f>>v[++ct], ins(ct);
        if(p == 2) f>>x, elim[x] = true;
        if(p == 3) g<<getmin()<<"\n";
    }

    f.close();
    g.close();
    return 0;
}