Cod sursa(job #1686304)

Utilizator TibixbAndrei Tiberiu Tibixb Data 12 aprilie 2016 10:31:32
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 kb
#include<fstream>
using namespace std;
int n, x, h[200005], p[200005], a[200005], nrh, s, p1, op, kronos, nrop;
void insert_heap(int n)
{
    h[++nrh]=n;
    p[n]=nrh;
    s=nrh;
    p1=s/2;
    while(p1!=0 && a[h[s]]<a[h[p1]])
    {
        swap(h[s], h[p1]);
        p[h[s]]=s;
        p[h[p1]]=p1;
        s=p1;
        p1/=2;
    }
}
void erase_heap(int kronos)
{
    a[kronos]=-1;
    s=p[kronos];
    p1=s/2;
    while(p1!=0 && a[h[s]]<a[h[p1]])
    {
        swap(h[s], h[p1]);
        p[h[s]]=s;
        p[h[p1]]=p1;
        s=p1;
        p1/=2;
    }
    h[1]=h[nrh];
    p[h[1]]=1;
    nrh--;
    p1=1;
    s=2;
    while(s<=nrh)
    {
        if(s+1<=nrh && a[h[s+1]]<a[h[s]])
            s++;
        if(a[h[s]]<a[h[p1]])
        {
            swap(h[s], h[p1]);
            p[h[s]]=s;
            p[h[p1]]=p1;
            p1=s;
            s*=2;
        }else
        {
            break;
        }
    }
}
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int main()
{
    in>>nrop;
    for(;nrop--;)
    {
        in>>op;
        if(op==1)
        {
            in>>x;
            a[++n]=x;
            insert_heap(n);
            continue;
        }
        if(op==2)
        {
            in>>kronos;
            erase_heap(kronos);
            continue;
        }
        out<<a[h[1]]<<"\n";
    }
    return 0;
}