Cod sursa(job #711373)

Utilizator ion824Ion Ureche ion824 Data 11 martie 2012 23:31:09
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.63 kb
#include<cstdio>
#define nmax 200010
int n,l,nr;
int a[nmax],heap[nmax],pos[nmax];

void push(int x){
     int aux;
     
     while(x>1 && a[heap[x]]<a[heap[x>>1]])
     {
        aux = heap[x];
        heap[x] = heap[x>>1];
        heap[x>>1] = aux;
        
        pos[heap[x]]=x;
        pos[heap[x>>1]]=x>>1;
        x>>=1;                         
     }            
}

void pop(int x)
{
   int aux, y=0;
   
   while(x!=y){              
               y = x;
               
               if(y<<1<=l && a[heap[x]]>a[heap[y<<1]])x=y<<1;
               if((y<<1)+1<=l && a[heap[x]]>a[heap[(y<<1)+1]])x=(y<<1)+1;
               
               aux = heap[x];
               heap[x] = heap[y];
               heap[y] = aux;
               
               pos[heap[x]] = x;
               pos[heap[y]] = y;                            
               }              
}

int main(void){
    freopen("heapuri.in","r",stdin); 
    freopen("heapuri.out","w",stdout);   
    int i,x,y;
    scanf("%d\n",&n);
    for(i=1;i<=n;++i)
    {
    scanf("%d ",&x);                 
    if(x==1){
             scanf("%d",&y);
             l++; nr++;
             a[nr] = y;
             heap[l] = nr;
             pos[nr] = l;
             
             push(l);        
             }
    if(x==2){
             scanf("%d",&y);
             a[y] = -1;
             push(pos[y]);
             
             pos[heap[1]] = 0;
             heap[1] = heap[l--];
             pos[heap[1]] = 1;
             if(l>1)pop(1);           
             }         
    if(x==3)printf("%d\n",a[heap[1]]);
    }
   return 0; 
}