Cod sursa(job #523788)

Utilizator andu04Popa Andi andu04 Data 19 ianuarie 2011 11:52:00
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.49 kb
#include <iostream>
#include <stdio.h>
#define NMAX 200001
using namespace std;

int A[NMAX],H[NMAX],P[NMAX],n,c,nr,l;


void push(int x)
{
    while (x>1 && A[H[x]]<A[H[x/2]])
    {
        int aux=H[x];
        H[x]=H[x/2];
        H[x/2]=aux;
        P[H[x]]=x;
        P[H[x/2]]=x/2;

        x/=2;
    }
}
void pop(int x)
{
    int y=0;
    while (x!=y)
    {
        y=x;
        if (y*2<=l && A[H[x]]>A[H[y*2]])
                x=y*2;
        if (y*2+1<=l && A[H[x]]>A[H[y*2+1]])
                x=y*2+1;

            int aux=H[x];
            H[x]=H[y];
            H[y]=aux;
            P[H[x]]=x;
            P[H[y]]=y;
    }
}

void citire()
{
    freopen ("heapuri.in","r",stdin);
    freopen ("heapuri.out","w",stdout);
    scanf ("%d",&n);
    int x;
    for (int i=1;i<=n;i++)
    {
        scanf ("%d",&c);
        if (c==3)
            printf("%d\n",A[H[1]]);
        else
        {
            if (c==1)
            {
                scanf ("%d",&x);
                l++;nr++;
                A[nr]=x;
                H[l]=nr;
                P[nr]=l;
                push(l);
            }
            else
            {
                scanf ("%d",&x);
                A[x]=-1;
                push(P[x]);
                P[H[1]]=0;
                H[1]=H[l];l--;
                P[H[1]]=1;
                if (l>1)
                    pop(1);

            }
        }
    }
}

int main()
{
    citire();
    return 0;
}