Cod sursa(job #2156821)

Utilizator cezinatorCezar D cezinator Data 9 martie 2018 00:27:39
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 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 (x==1) return 0;
    if(heap[x]<heap[x/2])
    {
        swapp(x,x/2);
        bubble_up(x/2);
    }
    return 0;
}
int bubble_down(int x)
{
    if(x>HS) return 0;
    int ls=2*x;
    int rs=2*x+1;
    int s=x;
    if(ls<=HS&&heap[ls]<heap[x]) s=ls;
    if(rs<=HS&&heap[rs]<heap[s]) s=rs;
    if(x==s) return 0;
    else
    {
        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(x,HS);
    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;
}