Cod sursa(job #2626316)

Utilizator RomanacheRoman Alexandru-George Romanache Data 6 iunie 2020 13:21:38
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.37 kb
#include <bits/stdc++.h>

#define ROMAN 200005

using namespace std;

fstream f("heapuri.in",ios::in);
fstream g("heapuri.out",ios::out);

int a[ROMAN],h[ROMAN],poz[ROMAN];
int n,ord,op;

int tata(int nod)
{
    return nod/2;
}

int st(int nod)
{
    return 2*nod;
}
int dr(int nod)
{
    return 2*nod+1;
}

void Schimbare(int p,int q)
{
    swap(h[p],h[q]);
    poz[h[p]]=p;
    poz[h[q]]=q;
}

void Ssus(int k)
{
    while(k>1 && a[h[k]] < a[h[tata(k)]])
    {
        Schimbare(k,tata(k));
        k=tata(k);
    }
}

void Sjos(int k)
{
    int fiu=0;
    if(st(k)<=n)
    {
        fiu=st(k);
        if(dr(k)<=n && a[h[dr(k)]]<a[h[st(k)]])
            fiu=dr(k);
        if(a[h[fiu]]>=a[h[k]])
            fiu=0;
    }
    if(fiu)
    {
        Schimbare(k,fiu);
        Sjos(fiu);
    }
}

void Inserare(int x)
{
    h[++n]=x;
    poz[x]=n;
    Ssus(n);
}

void Sterge(int k)
{
    Schimbare(k,n);
    n--;
    Ssus(k);
    Sjos(k);
}

int main()
{
    f>>op;
    for(int i=1;i<=op;i++)
    {
        int c,x;
        f>>c;
        if(c==3)
            g<<a[h[1]]<<"\n";
        else
        {
            f>>x;
            if(c==1)
            {
                a[++ord]=x;
                Inserare(ord);
            }
            else
                Sterge(poz[x]);
        }
    }

    return 0;
}