Cod sursa(job #1821767)

Utilizator Tudor_CandeaCandea Tudor Tudor_Candea Data 3 decembrie 2016 16:44:59
Problema Heapuri Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.4 kb
#include <fstream>
#include <cmath>
#define nmax 200010
#define nt 0;
using namespace std;
ifstream fin ("heapuri.in");
ofstream fout ("heapuri.out");

int heap[nmax],nod[nmax],poz[nmax], n;
void verif(int k)
{
    int fiu=heap[k];
    {
        int fiuviz=k;
        while((fiu<heap[k/2])and(fiu and heap[k/2]))
        {
            heap[k]=heap[k/2];
            poz[heap[k]]=k;
            k=k/2;
        }
        heap[k]=fiu;
        poz[heap[k]]=k;
    }
}

void verif2(int k)
{
    int fiu=1;
    while(fiu)
    {
        if(k*2<=n and heap[k*2])
        {
                fiu=k*2;
            if(k*2+1<=n and (heap[k*2+1]<heap[k*2])and heap[k*2+1])
                fiu=k*2+1;
        }
        if(heap[fiu]<=heap[k])
                fiu=0;
        if(fiu)
        {
            swap(heap[fiu],heap[k]);
            swap(poz[heap[fiu]],poz[heap[k]]);
            k=fiu;
        }
    }
}

int main()
{
    int k=0;
    fin>>n;
    for(int i=1;i<=n;i++)
    {
        int x, y;
        fin>>x;
        if(x==1)
        {
            fin>>y;
            nod[++k]=y;
            heap[k]=y;
            poz[y]=k;
            verif(k);
        }
        else if(x==2)
        {
            fin>>y;
            heap[poz[nod[y]]]=nt;
            verif2(poz[nod[y]]);
        }
        else if(x==3)
            fout<<heap[1]<<'\n';
    }
    return 0;
}