Cod sursa(job #2032559)

Utilizator sotoc1999Sotoc George sotoc1999 Data 5 octombrie 2017 12:49:27
Problema Heapuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.72 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
struct heap{
    int val;
    int ap;
}h[200003];
int n,m;
inline int father(int nod)
{
    return nod/2;
}
inline int right_son(int nod)
{
    return 2*nod+1;
}
inline int left_son(int nod)
{
    return 2*nod;
}
void sift(int nod)
{
    int son;
    do
    {
        son=0;
        if(left_son(nod)<=n)
        {
            son=left_son(nod);
            if(right_son(nod)<=n&&h[left_son(nod)].val>h[right_son(nod)].val)
            {
                son=right_son(nod);
            }
            if(h[son].val>h[nod].val)
                son=0;
        }
        if(son)
        {
            swap(h[nod],h[son]);
            nod=son;
        }
    }while(son);
}
void percolate(int nod)
{
    while(nod>1&&h[nod].val<h[father(nod)].val)
    {
        swap(h[nod],h[father(nod)]);
        nod=father(nod);
    }
}
void inserare(int nod,int apar)
{
    n++;
    h[n].val=nod;
    h[n].ap=apar;
    percolate(n);
}
void stergere(int nod)
{
    h[nod].val=h[n].val;
    h[nod].ap=h[n].ap;
    n--;
    if(nod>1&&h[nod].val<h[father(nod)].val)
        percolate(nod);
    else
        sift(nod);
}
int main()
{
    f>>m;
    int op,x;
    n=0;
    int nr=0;
    for(int i=1;i<=m;i++){
        f>>op;
        if(op==1)
        {
            f>>x;
            nr++;
            inserare(x,nr);
        }
        else if(op==2)
        {
            f>>x;
            for(int j=1;j<=n;j++)
                if(h[j].ap==x)
                    stergere(j);
        }
        else
            g<<h[1].val<<"\n";
    }
    return 0;
}