Cod sursa(job #2629038)

Utilizator proflaurianPanaete Adrian proflaurian Data 18 iunie 2020 18:03:10
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.18 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
const int N = 100010;
int n,m,op,x,lg,v[N],h[N],p[N];
void heapDown(int),heapUp(int),heapSwap(int,int);
int main()
{
    f>>n;
    for(int i=1;i<=n;i++)
    {
        f>>op;
        if(op==1)
        {
            f>>x;
            v[++m]=x;
            h[++lg]=m;
            p[m]=lg;
            heapUp(lg);
        }
        else
            if(op==2)
            {

                f>>x;
                x=p[x];
                heapSwap(x,lg);
                lg--;
                heapUp(x);
                heapDown(x);

            }
            else
            {
                g<<v[h[1]]<<'\n';
            }
    }
    return 0;
}
void heapSwap(int i,int j)
{
    swap(h[i],h[j]);
    p[h[i]]=i;
    p[h[j]]=j;
}
void heapUp(int F)
{
    int T=F/2;
    if(!T)return;
    if(v[h[F]]<v[h[T]])
    {
        heapSwap(F,T);
        heapUp(T);
    }
}
void heapDown(int T)
{
    int F = 2*T;
    if(F>lg)return;
    if(F<lg)if(v[h[F+1]]<v[h[F]])F++;
    if(v[h[F]]<v[h[T]])
    {
        heapSwap(F,T);
        heapDown(F);
    }
}