Cod sursa(job #236724)

Utilizator katakunaCazacu Alexandru katakuna Data 28 decembrie 2008 13:15:29
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.01 kb
#include<stdio.h>

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

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[t]]=t;
  p[h[c]]=c;

  c=t;t=c>>1;
  }

}


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

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

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

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

    t=c;c=t<<1;

     if(c<n && v[h[c]] > v[h[c+1]])
     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);
     nr++; n++;
     v[nr]=x;
     h[n]=nr;
     p[nr]=n;
     up(n);
     }

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

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

return 0;
}