Cod sursa(job #3135461)

Utilizator DariusGhercaDarius Gherca DariusGherca Data 3 iunie 2023 12:30:03
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.3 kb
#include <bits/stdc++.h>

using namespace std;
const int N=2e5+10;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int v[N],h[N],poz[N],nh;
void schimba(int x,int y)
{
    swap(h[x],h[y]);
    poz[h[x]]=x;
    poz[h[y]]=y;
}
void urca(int p)
{
    while(p>1 && v[h[p]] < v[h[p/2]])
    {
        schimba(p,p/2);
        p=p/2;
    }
}
void adauga(int x)
{
    h[++nh]=x;
    poz[x]=nh;
    urca(nh);
}
void coboara(int p)
{
    int fs=2*p,fd=2*p+1,bun=p;
    if(fs<=nh && v[h[fs]]<v[h[bun]])
    {
        bun=fs;
    }
    if(fd<=nh && v[h[fd]]<v[h[bun]])
    {
        bun=fd;
    }
    if(bun!=p)
    {
        schimba(p,bun);
        coboara(bun);
    }
}
void sterge(int p)
{
    if(p==nh)
    {
        nh--;
    }
    else
    {
        h[p]=h[nh--];
        poz[h[p]]=p;
        urca(p);
        coboara(p);
    }
}
int main()
{
    int nop,n=0;
    f>>nop;
    for(int i=0;i<nop;i++)
    {
        int tip;
        f>>tip;
        if(tip==1)
        {
            f>>v[++n];
            adauga(n);
        }
        if(tip==2)
        {
            int p;
            f>>p;
            sterge(poz[p]);
        }
        if(tip==3)
        {
            g<<v[h[1]]<<"\n";
        }
    }
    g.close();
    f.close();
    return 0;
}