Cod sursa(job #1268472)

Utilizator cipriancxFMI - gr143 Timofte Ciprian cipriancx Data 20 noiembrie 2014 23:20:08
Problema Heapuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.63 kb
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

int h[200011],n,stiva[200001],dimstv,operatii;

inline int father(int nod){return nod/2;}
inline int left_son(int nod){return nod*2;}
inline int right_son(int nod){return nod*2+1;}
inline int minim(){return h[1];}

void sift(int k)
{
    int son=0;
    do{ if(left_son(k)<=n){ son=left_son(k);
                            if(right_son(k)<=n && h[right_son(k)]<h[left_son(k)]){ son = right_son(k); }
                            if( h[son] >= h[k] ){ son = 0; }

                          }
                          else son=0;

        if(son){int aux=h[k]; h[k]=h[son]; h[son]=aux;
                k=son;
            }


      }while(son);
}

void percolate(int k)
{
int val=h[k];
while((k>1) && (val < h[father(k)])){
                                        h[k]=h[father(k)];
                                        k=father(k);
                                    }
h[k]=val;

}

//eliminare nod poz k

void elim(int k)
{
    h[k]=h[n];
    n--;
    if((k>1) && (h[k]<h[father(k)]))percolate(k);
    else sift(k);

}

void ins(int val)
{
    n++;
    h[n]=val;
    percolate(n);
}

int caut(int val)
{
    for(int i=1; i<=n; i++)if(h[i]==val)return i;
    return 0;
}
int main()
{
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
int a,b;
scanf("%d",&operatii);
for(int i=1; i<=operatii; i++)
{
    scanf("%d",&a);
    if(a==3){ printf("%d \n",h[1]);}
    else{
scanf("%d",&b);
if(a==1){ dimstv++; stiva[dimstv]=b; ins(b);    }
if(a==2){ elim(caut(stiva[b]));     }

}
}

    return 0;
}