Cod sursa(job #3149467)

Utilizator Robert_MitriRobert Mitri Robert_Mitri Data 8 septembrie 2023 19:35:53
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.44 kb
#include <fstream>
#define sz 200005
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");


int v[sz];
int h[sz];
int poz[sz];
int n;
int m;

int pr(int nod)
{
    return nod/2;
}
int lc(int nod)
{
    return nod*2;
}
int rc(int nod)
{
    return nod*2+1;
}
void swp(int a,int b)
{
    swap(h[a],h[b]);
    poz[h[a]] = a;
    poz[h[b]] = b;
}
void up_heap(int key)
{

    while(v[h[key]]<v[h[pr(key)]] && key > 1)
    {
        swp(key,pr(key));
        key=pr(key);
    }
}
void down_heap(int p)
{
    int x = p;
    int l = lc(p);
    int r = rc(p);

    if(l <= n && v[h[l]] < v[h[x]])
        x=l;
    if(r <= n && v[h[r]] < v[h[x]])
        x=r;
    if(p!=x)
        swp(p,x),down_heap(x);
}

void push(int ind)
{
    h[++n] = ind;
    poz[ind] = n;
    up_heap(n);
};
void del(int key){
    if(key==n)
    {
        n--;
        return;
    }
    h[key] = h[n--];
    poz[h[key]]=key;
    up_heap(key);
    down_heap(key);

};
int get_min()
{
    return h[1];
}


int q;

int main()
{
    fin>>m;
    for(int i=1;i<=m;i++)
    {
        int tip;
        fin>>tip;
        if(tip==1)
        {
            fin>>v[++q];
            push(q);
        }
        else if (tip==2)
        {
            int x;
            fin>>x;
            del(poz[x]);
        }
        else
        {
            fout<<v[get_min()]<<'\n';
        }

    }
}