Cod sursa(job #236699)

Utilizator katakunaCazacu Alexandru katakuna Data 28 decembrie 2008 12:07:39
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include<stdio.h>

int nr,aux,t,i,n,N,x,h[200011],v[200011],p[200011];


void up(int x){
int t,c;
c=x;
t=c>>1;

     while(c!=1 && v[h[c]] < v[h[t]]){
     aux=h[c];
     h[c]=h[t];
     h[t]=aux;

     p[h[c]]=c;
     p[h[t]]=t;
     c=t;
     t=c>>1;
     }

}


void down(int x){
int t,c;
t=x;
c=t<<1;

   if(v[c] > v[c+1] && c<N)
   c++;

       while(c<=N && v[h[t]] > v[h[c]] ){
       aux=h[c];
       h[c]=h[t];
       h[t]=aux;

       p[h[c]]=c;
       p[h[t]]=t;
 


       t=c;
       c=t<<1;

         if(v[c] > v[c+1] && c<N)
         c++;
       }

}


int main(){

FILE *f=fopen("heapuri.in","r");
FILE *g=fopen("heapuri.out","w");
fscanf(f,"%d",&n);

    for(i=1;i<=n;i++){
    fscanf(f,"%d",&t);

      if(t==1){
      fscanf(f,"%d",&x);
      v[++nr]=x;
      h[++N]=nr;
      p[nr]=N;
      up(N);
      }

      if(t==2){
      fscanf(f,"%d",&x);
      aux=h[N];
      h[N]=h[p[x]];
      h[p[x]]=aux;
      N--;
      p[h[N]]=N;
      p[h[p[x]]]=p[x];
      down(p[x]);
      }

      if(t==3)
      fprintf(g,"%d\n",v[h[1]]);

    }
  
fclose(f);
fclose(g);

return 0;
}