Cod sursa(job #1042213)

Utilizator AlexMateialex matei AlexMatei Data 26 noiembrie 2013 18:15:48
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include<fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int v[200000],n,i,j,m,q,poz[200000],v1[200000],nr;
void schimba(int x1,int x2)
{
    int aux=v[x1];
    v[x1]=v[x2];
    v[x2]=aux;
    poz[v[x1]]=x1;
    poz[v[x2]]=x2;
}
void urca(int x)
{
    while(x>1 && v1[v[x]]<v1[v[x/2]])
    {
        schimba(x,x/2);
        x/=2;
    }
}
void coboara(int p)
{
    int fs=2*p,fd=2*p+1,bun=p;
    if(fs<=m && v1[v[fs]]<v1[v[bun]])
        bun=fs;
    if(fd<=m && v1[v[fd]]<v1[v[bun]])
        bun=fd;
    if(bun!=p)
    {
        schimba(bun,p);
        coboara(bun);
    }
}
void sterge(int p)
{
    schimba(p,m--);
    urca(p);
    coboara(p);
}
int main()
{
    f>>n;
    for(i=1;i<=n;i++)
    {
        f>>q;
        if(q==3)
            g<<v1[v[1]]<<"\n";
        else
        {
            if(q==1)
            {
                f>>v1[++nr];
                poz[nr]=++m;
                v[m]=nr;
                urca(m);
            }
            else
            {
                f>>q;
                sterge(poz[q]);
            }
        }
    }
}