Cod sursa(job #2623991)

Utilizator andreizZenoveiov Andrei andreiz Data 4 iunie 2020 12:37:31
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.35 kb
#include <fstream>
using namespace std;

ifstream f("heapuri.in");
ofstream g("heapuri.out");

int n,poz[200001],k,i,ha,d;

struct nya{
    int st,dr;
};

nya h[200001];

void swapp(int x)
{
    swap (h[x],h[x/2]);
    poz[h[x/2].dr]=x/2;
    poz[h[x].dr]=x;
}

void heapup(int x)
{
    if (x>1)
    {
        if (h[x].st<h[x/2].st)
        {
            swapp (x);
            heapup(x/2);
        }
    }
}

void heapdown(int x)
{
    int ma=h[x].st;
    if(x*2<=k)
    {
        if(h[x*2].st<ma)
            ma=h[x*2].st;
        if(x*2<k)
        {
            if(h[x*2+1].st<ma)
                ma=h[x*2+1].st;
        }
    }
    if(ma!=h[x].st)
    {
        if(ma==h[x*2].st)
        {
            swapp(x*2);
            heapdown(x*2);
        }
        else
            if(ma==h[x*2+1].st)
            {
                swapp(x*2+1);
                heapdown(x*2+1);
            }
    }
}

int main()
{
    f>>n;
    for(i=1;i<=n;++i)
    {
        f>>ha;
        if(ha==1)
        {
            f>>d;
            k++;
            poz[k]=k;
            h[k].st=d;
            h[k].dr=k; heapup(k);
        }
    else
        if(ha==3)
            g<<h[1].st<<'\n';
    else
        {
            f>>d;
            h[poz[d]].st=1000000001; heapdown(poz[d]);
        }
    }
return 0;
}