Cod sursa(job #2838806)

Utilizator BriannaBrianna Stan Brianna Data 24 ianuarie 2022 17:26:00
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.21 kb
#include <fstream>
#include <vector>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
static int heap[200002],poz[200002],v[200002];
int timp,h;
int minim(){
    return v[heap[1]];
}
void schimb(int a, int b)
{
    swap(heap[a],heap[b]);
    poz[heap[a]]=a;
    poz[heap[b]]=b;
}
void urca(int p)
{
    while(v[heap[p]]<v[heap[p/2]])
    {
        schimb(p,p/2);
        p/=2;
    }
}
void coboara(int p) {
    int fs = 2 * p, fd = 2 * p + 1, bun = p;
    if (fs <= h && v[heap[fs]] < v[heap[bun]])
        bun = fs;
    if (fd <= h && v[heap[fd]] < v[heap[bun]])
        bun = fd;
    if (bun != p) {
        schimb(p, bun);
        coboara(bun);
    }
}
void sterge(int p)
{
    if(p==h)
        h--;
    else
    {
        schimb(p,h--);
        urca(p);
        coboara(p);
    }
}
void adauga(int val)
{
    heap[++h]=val;
    urca(h);
}
int main()
{
    int t,cer,val;
    in>>t;
    while(t--)
    {
        in>>cer;
        if(cer==1)
        {
            in>>v[++timp];
            poz[timp]=timp;
            adauga(timp);
        }
        if(cer==2)
        {
            in>>val;
            sterge(poz[val]);
        }
        if(cer==3)
            out<<minim()<<"\n";
    }
    return 0;
}