Cod sursa(job #2156827)

Utilizator cezinatorCezar D cezinator Data 9 martie 2018 00:42:06
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.45 kb
#include <fstream>

using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
unsigned long long n,cod,x,cron[200005],HS,heap[200005],invers_cron[200005],HC;
void afis()
{
    for(int i=1;i<=HS;i++) fout<<heap[i]<<' ';
    fout<<'\n';
}
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);
    }
    return 0;
}
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);
    }
    return 0;
}
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);
    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;
}