Cod sursa(job #2086352)

Utilizator AlexVulpoiuAlexandru Vulpoiu AlexVulpoiu Data 11 decembrie 2017 22:02:48
Problema Heapuri Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.29 kb
#include <cstdio>
#include <algorithm>
using namespace std;
int n,m,i,j,k,p,op,a[200001],b[200001],c[200001];
void pushup(int x)
{
    int val;
    val=a[x];
    while(x>1 && val<a[x/2])
    {
        swap(a[x],a[x/2]);
        c[a[x]]=x;
        c[a[x/2]]=x/2;
        x/=2;
    }
}
void pushdown(int x)
{
    int son;
    do
    {
        son=0;
        if(x<=m)
        {
            son=x*2;
            if(x*2+1<=m && a[x*2+1]<a[x*2])
                son=x*2+1;
            if(a[son]<=a[x])
                son=0;
        }
        if(son)
        {
            swap(a[son],a[x]);
            swap(c[a[son]],c[a[x]]);
            x=son;
        }
    }
    while(son);
}
int main()
{
    freopen("heapuri.in","r",stdin);
    freopen("heapuri.out","w",stdout);
    scanf("%d\n",&n);
    while(n)
    {
        scanf("%d",&op);
        if(op==1)
        {
            scanf("%d",&k);
            a[++m]=k;
            c[a[m]]=m;
            b[++p]=k;
            pushup(m);
        }
        else
            if(op==2)
            {
                scanf("%d",&k);
                pushdown(c[b[k]]);
                m--;
            }
            else
                if(op==3)
                    printf("%d\n",a[1]);
        n--;
    }
    return 0;
}