Cod sursa(job #1782902)

Utilizator george_stelianChichirim George george_stelian Data 18 octombrie 2016 17:03:26
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <bits/stdc++.h>

using namespace std;

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

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

void down_heap(int nod)
{
    while(1)
    {
        int fiu=nod;
        if(nod*2<=nr && h[nod*2].x<h[fiu].x) fiu=nod*2;
        if(nod*2+1<=nr && h[nod*2+1].x<h[fiu].x) fiu=nod*2+1;
        if(fiu==nod) return;
        swap(h[nod],h[fiu]);
        nod=fiu;
    }
}

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