Cod sursa(job #1825041)

Utilizator sebi110Ciobanu Sebastian sebi110 Data 8 decembrie 2016 18:04:32
Problema Heapuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.81 kb
#include <fstream>

using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int mom[200001],v[150001],vf,ult;
char bl[1000000001];
void push(int x)
{
    int aux;
    mom[++vf]=x;
    if(bl[x]==1)
        bl[x]=0;
    else
    {
        ult++;
        aux=ult;
        while(x<v[aux/2] && aux!=1)
        {
            v[aux]=v[aux/2];
            aux=aux/2;
        }
        v[aux]=x;
    }
}
void pop()
{
    int aux,ok;
    bl[v[1]]=0;
    v[1]=v[ult];
    ult--; ok=1; aux=1;
    while(ok==1)
    {
        ok=0;
        if(aux*2>ult)
            return ;
        if(aux*2+1>ult)
        {
            if(v[aux]>v[aux*2])
            {
                swap(v[aux],v[aux*2]);
                aux=aux*2;
                ok=1;
            }
        }
        else
        {
            if(v[aux*2]<=v[aux*2+1] )
            {
                if(v[aux]>v[aux*2])
                {
                    swap(v[aux],v[aux*2]);
                    aux=aux*2;
                    ok=1;
                }
            }
            else
                if(v[aux]>v[aux*2+1])
                {
                    swap(v[aux],v[aux*2+1]);
                    aux=aux*2+1;
                    ok=1;
                }
        }
    }
}
int main()
{
    int n,i,op,x;
    fin>>n;
    for(i=1;i<=n;i++)
    {
        fin>>op;
        if(op==1)
        {
            fin>>x;
            push(x);
        }
        else
        {
            if(op==2)
            {
                fin>>x;
                bl[mom[x]]=1;
            }
            else
            {
                while(bl[v[1]]==1)
                {
                    pop();
                }
                fout<<v[1]<<'\n';
            }
        }
    }
    return 0;
}