Cod sursa(job #1480673)

Utilizator cremarencodianaCremarenco Diana cremarencodiana Data 2 septembrie 2015 23:41:28
Problema Heapuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.35 kb
# include <cstdio>
#include <vector>

using namespace std;

int op,i,n,nr,a[200010], h[200010], aux, index, j, pos[200010],r;

void fixUp(int p)
{
    h[++n]=p; pos[p]=n;
    p=n;
    int aux;
    while (p>1 && a[h[p/2]]>=a[h[p]])
    {
        aux =h[p/2]; h[p/2]=h[p]; h[p]=aux;
        aux = pos[h[p/2]]; pos[h[p/2]]=pos[h[p]]; pos[h[p]]=aux;
        p=p/2;
    }
}

void fixDown(int p)
{
    p = pos[p];
    aux=h[p]; h[p]=h[n]; h[n]=aux;
    aux=pos[h[p]]; pos[h[p]]=pos[h[n]]; pos[h[n]]=aux;
    n--;
    while (2*p<=n)
    {
        j = 2*p;
        if (j+1<=n && a[h[j+1]]<a[h[j]])
        {
            j++;
        }
        if (a[h[p]]<a[h[j]]) break; else
        {
            aux=h[p];
            h[p]=h[j];
            h[j]=aux;
            aux=pos[h[p]];
            pos[h[p]]=pos[h[j]];
            pos[h[j]]=aux;
        }
    }
}
int main()
{
    freopen("heapuri.in","r",stdin);
    freopen("heapuri.out","w",stdout);
    scanf("%d\n",&nr);
    n=0;
    for (i=1; i<=nr; i++)
    {
        scanf("%d ",&op);
        if (op==1)
        {
            scanf("%d\n", &a[++r]);
            fixUp(r);
        }
        if (op==2)
        {
            scanf("%d\n", &index);
            fixDown(index);

        }
        if (op==3)
        {
            printf("%d\n",a[h[1]]);
        }
    }


}