Cod sursa(job #853934)

Utilizator chimistuFMI Stirb Andrei chimistu Data 12 ianuarie 2013 16:07:29
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.46 kb
#include <cstdio>
#include <cstdlib>
#define nmax 200001
#define mmax 100000001
FILE*f=fopen("heapuri.in","r");
FILE*g=fopen("heapuri.out","w");
int heap[nmax],poz[mmax],A[nmax];
int ind=0;

void swap(int &a,int &b)
{
     int aux;
     aux=a;
     a=b;
     b=aux;
}

void up_heap(int x)
{
     if (x>1)
        if (A[heap[x/2]]>A[heap[x]])
        {
            swap(heap[x/2],heap[x]);
            poz[heap[x/2]]=x/2;
            poz[heap[x]]=x;
            up_heap(x/2);
            }
}

void down_heap(int x)
{
     int m;
     if (2*x<=ind)
     {
           m=2*x;
           if (2*x+1<=ind && heap[2*x+1]<heap[2*x])
              m=2*x+1;
           if (A[heap[m]]<A[heap[x]])
           {
                swap(heap[m],heap[x]);
                poz[heap[m]]=m;
                poz[heap[x]]=x;
                down_heap(m);}
     }
}     
     
int main()
{
    int n,tip,x;
    fscanf(f,"%d",&n);
    while (n>0)
    {
          fscanf(f,"%d",&tip);
         if (tip==1)
          {
             fscanf(f,"%d",&x);
              ind++;
              heap[ind]=ind;
              A[ind]=x;
              poz[ind]=ind;
              up_heap(ind);}
          else
              if (tip==2)
              {
                  fscanf(f,"%d",&x);
                  A[x]=mmax;
                  down_heap(poz[x]);}
              else
                  fprintf(g,"%d \n",A[heap[1]]);
    n--;
    }
    return 0;
}