Cod sursa(job #2270163)

Utilizator stefantagaTaga Stefan stefantaga Data 27 octombrie 2018 09:44:31
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.59 kb
#include <fstream>

using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int v[200005],heap[200005],v1[200005],of[200005];
void heapup(int x)
{
    if (x>1)
    {
        if (v1[heap[x]]<v1[heap[x/2]])
        {
            swap(heap[x],heap[x/2]);
            heapup(x/2);
        }
    }
}
void heapdown(int x,int u)
{
    int st,dr;
    if (x*2<=u)
    {
        st=v1[heap[x*2]];
        if (2*x+1<=u)
        {
            dr=v1[heap[x*2+1]];
        }
        else
        {
            dr=1000000001;
        }
        if (min(st,dr)<v1[heap[x]])
        {
            if (st<dr)
            {
                swap(heap[x],heap[2*x]);
                heapdown(2*x,u);
            }
            else
            {
                swap(heap[x],heap[2*x+1]);
                heapdown(2*x+1,u);
            }
        }
    }
}
int n,q,i,x,nr,poz,j,numar,nr2=0;
int main()
{
    f>>n;
    q=0;
    numar=0;
    for (j=1;j<=200000;j++)heap[j]=1000000001;
    for (i=1;i<=n;i++)
    {
        f>>x;
        if (x==1)
        {
            f>>nr;
            q++;
            numar++;
            v1[numar]=nr;
            heap[q]=numar;
            heapup(q);
        }
        else
        if (x==2)
        {
            f>>poz;
            v1[poz]=1000000001;
            if (v1[heap[1]]==1000000001)
            {
                swap(heap[1],heap[q]);
                q--;
                heapdown(1,q);
            }
        }
        else
        {
            g<<v1[heap[1]]<<'\n';
        }
    }
    return 0;
}