Cod sursa(job #1512474)

Utilizator superstar1998Moldoveanu Vlad superstar1998 Data 28 octombrie 2015 09:22:21
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include <iostream>
#include <fstream>
using namespace std;
int H[200001],A[200001],Pos[200001],m,n,na;
void inserare(int x)
{
    while(x/2 && A[H[x]]<A[H[x/2]])
    {
        swap(H[x],H[x/2]);
        Pos[H[x]]=x;
        Pos[H[x/2]]=x/2;
        x=x/2;
    }
}
void sterge(int x)
{
    int y=0;
    while(x!=y)
    {
        y=x;
        if (y*2<=n && A[H[x]]>A[H[y*2]]) x = y*2;
        if (y*2+1<=n && A[H[x]]>A[H[y*2+1]]) x = y*2+1;
        swap(H[x],H[y]);
        Pos[H[x]]=x;
        Pos[H[y]]=y;
    }
}
int main()
{
    ifstream f("heapuri.in");
    ofstream g("heapuri.out");
    f>>m;
    int x,y;
    for(int i=1;i<=m;i++)
    {
        f>>x;
        if(x==1)
        {
            f>>y;
            A[++na]=y;
            H[++n]=na;
            Pos[na]=n;
            inserare(y);
        }
        else if(x==2)
        {
            f>>y;
            A[y]=-1;
            inserare(Pos[y]);
            Pos[H[1]]=0;
            H[1]=H[n--];
            Pos[H[1]]=1;
            sterge(1);
        }
        else g<<A[H[1]]<<'\n';
    }
    f.close();
    g.close();
    return 0;
}