Cod sursa(job #1825064)

Utilizator sebi110Ciobanu Sebastian sebi110 Data 8 decembrie 2016 18:23:33
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.94 kb
#include <fstream>
#include <map>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int mom[200001],v[150001],vf,ult;
map <int, int> bl;
map <int, int >::iterator it;
void push(int x)
{
    int aux;
    mom[++vf]=x;
    it=bl.find(x);
    if(it!=bl.end())
    {
        bl.erase(it);
        return ;
    }
    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;
    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
            {
                it=bl.find(v[1]);
                while(it!=bl.end())
                {
                    bl.erase(it);
                    pop();
                    it=bl.find(v[1]);
                }
                fout<<v[1]<<'\n';
            }
        }
    }
    return 0;
}