Cod sursa(job #1643898)

Utilizator george_stelianChichirim George george_stelian Data 9 martie 2016 20:39:56
Problema Heapuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.2 kb
#include <cstdio>
#include <algorithm>

using namespace std;

struct heap
{
    int poz,x;
}v[200010];
int nr;
char vaz[200010];

void up_heap(int nod)
{
    for(;nod>1;nod/=2)
        if(v[nod].x<v[nod/2].x) swap(v[nod],v[nod/2]);
}

void down_heap(int nod)
{
    while(nod*2<=nr)
        if(v[nod*2].x<v[nod*2+1].x || (nod*2==nr))
        {
            swap(v[nod],v[nod*2]);
            nod*=2;
        }
        else
        {
            swap(v[nod],v[nod*2+1]);
            nod=nod*2+1;
        }
}

int main()
{
    freopen("heapuri.in", "r", stdin);
    freopen("heapuri.out", "w", stdout);
    int n,tip,x;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&tip);
        if(tip==1)
        {
            scanf("%d",&x);
            ++nr;
            v[nr]={nr,x};
            up_heap(nr);
        }
        else if(tip==2)
        {
            scanf("%d",&x);
            vaz[x]=1;
        }
        else
        {
            while(vaz[v[1].poz])
            {
                swap(v[1],v[nr]);
                nr--;
                down_heap(1);
            }
            printf("%d\n",v[1].x);
        }
    }
    return 0;
}