Cod sursa(job #2268779)

Utilizator mihai2003LLL LLL mihai2003 Data 25 octombrie 2018 12:05:15
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include <iostream>
#include <fstream>

std::ifstream in("heapuri.in");
std::ofstream out("heapuri.out");

class Heap
{
public:
    int h[200001],poz[200001],v[200001];
    int nh=0,nr=0;
    int &size()
    {
        return nh;
    }

    void schimba(int p,int q)
    {
        int aux=h[p];
        h[p]=h[q];
        h[q]=aux;
        poz[h[p]]=p;
        poz[h[q]]=q;
    }
    void coboara(int p)
    {
        int fs=2*p,fd=2*p+1,bun=p;
        if(fs<=nh && v[h[fs]]<v[h[bun]])bun=fs;
        if(fd<=nh && v[h[fd]]<v[h[bun]])bun=fd;
        if(bun!=p)schimba(p,bun),coboara(bun);
    }
    void urca(int p)
    {

        if(p>1 && v[h[p]]<v[h[p/2]])
            schimba(p,p/2),urca(p/2);
    }
    void adauga(int x)
    {
        h[++nh]=x;
        poz[x]=nh;
        urca(nh);
    }
    void sterge(int p)
    {
        schimba(p,nh--);
        urca(p);
        coboara(p);
    }
};
Heap h;
int main()
{
    int n,tip,val;
    in>>n;
    h.nr=h.nh=0;
    std::cout<<n<<'\n';
    for(int i=1; i<=n; i++)
    {
        in>>tip;
        if(tip==1)
            in>>val,h.v[h.nr++ + 1]=val,h.adauga(h.nr);
        if(tip==2)
            in>>val,h.sterge(h.poz[val]);
        if(tip==3)
            out<<h.v[h.h[1]]<<'\n';
    }
    return 0;
}