Cod sursa(job #1052850)

Utilizator uagamagaMatei Rogoz uagamaga Data 11 decembrie 2013 20:58:42
Problema Heapuri Scor 0
Compilator c Status done
Runda Arhiva educationala Marime 1.55 kb
#include <stdio.h>
#include <stdlib.h>

int H[200001],n,ret[200001],rn,pos[200001],x;

int father(int nod){return nod>>1;}
int right_son(int nod){return (nod<<1)+1;}
int left_son(int nod){return nod<<1;}
int min(){return H[1];}
void swap(int nod1,int nod2)
{
    int aux;
    aux = H[nod1];
    H[nod1] = H[nod2];
    H[nod2] = aux;

    pos[H[nod1]] = nod1;
    pos[H[nod2]] = nod2;

}
void up(int nod)
{
    if(nod==1 || H[nod]>=H[father(nod)]) return;
    swap(nod,father(nod));
    up(father(nod));
}
void dw(int nod)
{
    if(nod>n || !nod) return;
    if(H[nod]>H[left_son(nod)])
    {
        swap(nod,left_son(nod));
        dw(left_son(nod));
    }
    else
        if(H[nod]>H[right_son(nod)])
        {
            swap(nod,right_son(nod));
            dw(right_son(nod));
        }

}
void INS()
{
    H[++n] = x;
    ret[++rn] = x;
    pos[x] = n;
    up(n);
}
void DEL()
{
    H[pos[ret[x]]] = H[n];
    --n;

    if(pos[ret[x]]>1 && H[pos[ret[x]]]<H[father(H[pos[ret[x]]])])
        up(pos[ret[x]]);
    else
        dw(pos[ret[x]]);
}


int main()
{
    int i,nr,op;

    freopen("heapuri.in","r",stdin);
    freopen("heapuri.out","w",stdout);

    scanf("%d",&nr);


    while(nr--)
    {
        scanf("%d",&op);
        if(op==1)
        {
            scanf("%d",&x);
            INS();
        }
        if(op==2)
        {
            scanf("%d",&x);
            DEL();
        }
        if(op==3)
        {
            printf("%d\n",min());
        }
    }

    return 0;
}