Cod sursa(job #2156789)

Utilizator cezinatorCezar D cezinator Data 9 martie 2018 00:05:43
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.36 kb
#include <fstream>

using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int n,cod,x,cron[200005],HS,heap[200005],invers_cron[200005],HC;
void peek()
{
    fout<<heap[1]<<'\n';
}
void swapp(int x, int y)
{
    swap(heap[x],heap[y]);
    swap(cron[invers_cron[x]],cron[invers_cron[y]]);
    swap(invers_cron[x],invers_cron[y]);

}
int bubble_up(int x)
{
    if(heap[x]<heap[x/2])
    {
        swapp(x,x/2);
        bubble_up(x/2);
    }
}
int bubble_down(int x)
{
    int ls=2*x;
    int rs=2*x+1;
    int s=0;
    if(rs>HS)
    {
        if(ls<=HS) s=ls;
        else return 0;
    }
    else
    {
        if(heap[rs]<heap[ls]) s=rs;
        else s=ls;
    }
    if(heap[x]>heap[s])
    {
        swapp(x,s);
        bubble_down(s);
    }
}
void insereaza(int x)
{
    HC++;
    HS++;
    heap[HS]=x;
    cron[HC]=HS;
    invers_cron[HS]=HC;
    bubble_up(HS);
}
void sterge(int x)
{
    swapp(HS,x);
    heap[HS]=0; invers_cron[HS]=0; HS--;
    bubble_down(x);
    bubble_up(x);
}
int main()
{
    fin>>n;
    for(int i=1;i<=n;i++)
    {
        fin>>cod;
        if(cod==1)
        {
            fin>>x;
            insereaza(x);
        }
        else if(cod==2)
        {
            fin>>x;
            sterge(cron[x]);
        }
        else peek();
    }
    return 0;
}