Cod sursa(job #1041335)

Utilizator adalLica Adela adal Data 25 noiembrie 2013 19:07:42
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.26 kb
#include <cstdio>
#define Max 200100
using namespace std;
int n,m,i,x,A[Max],Poz[Max],H[Max],Nr,N;

void swap(int a,int b)
{
    int aux;
    aux=H[a]; H[a]=H[b]; H[b]=aux;
    Poz[H[a]]=a;
    Poz[H[b]]=b;
}


void  HeapDw (int k)
{
    int St=2*k;
    if (St<=Nr)
    {
        if(2*k+1<=Nr)
          if (A[H[St+1]]<A[H[St]])St+=1;
        if (A[H[St]]<A[H[k]])
        {
            swap(k,St);
            HeapDw(St);
        }
    }
}

void HeapUp(int k)
{
    int t=k/2;
    if (t>0)
      if(A[H[k]]<A[H[t]])
        {
            swap(k,t);
            HeapUp(t);
        }

}


void del(int k)
{
    int aux;
    aux=H[k]; H[k]=H[Nr]; H[Nr]=aux;
    Poz[H[k]]=k;
    Poz[H[Nr]]=Nr;
    Nr--;

    HeapDw(k);
}



int main()
{

    freopen("heapuri.in","r",stdin);
    freopen("heapuri.out","w",stdout);
    scanf("%d",&n);
    for(i=1; i<=n; i++)
    { int op;
        scanf("%d",&op);
        if (op==1)
        {
            scanf("%d",&x);
            A[++N]=x;
            H[++Nr]=N;
            Poz[N]=Nr;
            HeapUp(Nr);
        }
        else if (op==2)
        {
            scanf("%d",&x);
            del(Poz[x]);
        }
        else printf("%d\n",A[H[1]]);
    }
    return 0;
}