Cod sursa(job #2466190)

Utilizator Savu_Stefan_CatalinSavu Stefan Catalin Savu_Stefan_Catalin Data 1 octombrie 2019 18:23:46
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.03 kb
#include <fstream>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int n,poz[200001],k,i,ha,g;
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()
{
    in>>n;
    for (i=1;i<=n;++i)
    {
in>>ha;

if (ha==1) {in>>g; k++; poz[k]=k; h[k].st=g; h[k].dr=k; heapup(k);}
else if (ha==3) {out<<h[1].st<<'\n';}
else {in>>g; h[poz[g]].st=1000000001; heapdown(poz[g]);}
    }
 return 0;
}