Cod sursa(job #2722259)

Utilizator maraboneaMara Bonea marabonea Data 12 martie 2021 18:14:34
Problema Heapuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.15 kb
/**
*/
#include <fstream>
#include <algorithm>
#include <cstdio>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
const int nmax=2e5 +5;
int n,k,t,v[nmax],poz,h[nmax],p[nmax],m;
void swap_2(int b,int c)
{
    swap(p[h[b]],p[h[c]]);
    swap(h[b],h[c]);
}
void percolate(int x)
{
    int f=x>>1;
    if(f>0 && v[h[x]]<v[h[f]])
    {
        swap_2(x,f);
        percolate(f);
    }
}
void insert_in(int a)
{
    h[++m]=a;
    p[a]=m;
    percolate(m);

}
void sift(int x)
{
    int s=x<<1;
    if(s+1<=m && h[s+1]<h[s])
        s++;
    if(s<=m && h[s]<h[x])
    {
        swap_2(x,s);
        sift(s);
    }
}
void delete_elem(int x)
{
    swap_2(x,m);
    p[h[m]]=0;
    h[m--]=0;
    sift(x);
}
void read()
{
    fin>>n;
    while(n)
    {
        fin>>t;
        if(t==1)
        {
            fin>>v[++k];
            insert_in(k);
        }

        if(t==2)
        {
            fin>>poz;
            delete_elem(p[poz]);
        }
        if(t==3)
        {
            fout<<v[h[1]];
        }
        n--;
    }
}
int main()
{
    read();
    return 0;
}