Cod sursa(job #2078581)

Utilizator trutruvasilicaHuhurez Marius trutruvasilica Data 29 noiembrie 2017 19:10:25
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.5 kb
#include <fstream>
#include <bitset>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
bitset<200010>viz;
int tot;
struct nod
{
    int x,ct;
}v[250000];
int left_son(int x)
{
    return x*2;
}
int right_son(int x)
{
    return x*2+1;
}
int father(int x)
{
    return x/2;
}
void down_heap(int x)
{
    int son=-1;
    while(son)
    {
        if(left_son(x)<=tot) son=left_son(x);
        if(right_son(x)<=tot)
        {
            if(v[right_son(x)].x < v[son].x) son=right_son(x);
        }
        if(son==-1) son=0;
        if(v[x].x <= v[son].x ) son=0;
        if(son)
        {
            swap(v[x],v[son]);
            x=son;
        }
    }
}
void up_heap(int x)
{
    nod val=v[x];
    while(x>1 && val.x < v[father(x)].x)
    {
        v[x]=v[father(x)];
        x=father(x);
    }
    v[x]=val;
}
/*void build()
{
    for(int i=n/2;i>=1;i--)
    {
        down_heap(i);
    }
}*/
void delete_node(int x)
{
    v[x]=v[tot];
    tot--;
    down_heap(x);
}
int main()
{
    int n,i=0,op,x;
    fin>>n;
    while(n--)
    {
        fin>>op;
        if(op==3)
        {
            while(viz[v[1].ct]==1) delete_node(1);
            fout<<v[1].x<<"\n";
        }
        if(op==1)
        {
            fin>>x;
            tot++;
            i++;
            v[tot].x=x;v[tot].ct=i;
            up_heap(tot);
        }
        if(op==2)
        {
            fin>>x;
            viz[x]=1;
        }
    }
}