Cod sursa(job #1268516)

Utilizator cipriancxFMI - gr143 Timofte Ciprian cipriancx Data 21 noiembrie 2014 00:04:02
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.82 kb
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
struct nod{
int v;
int *p;
};
struct nod h[200011];
int 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;}

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)].v < h[left_son(k)].v){ son = right_son(k); }
                            if( h[son].v >= h[k].v ){ son = 0; }

                          }
                          else son=0;

        if(son){struct nod aux=h[k];  h[k]=h[son]; *(h[son].p)=k; h[son]=aux; *(h[son].p)=son;
                k=son;
            }


      }while(son);
}

void percolate(int k)
{
struct nod vv=h[k];
//int val=h[k].v;
while((k>1) && (vv.v < h[father(k)].v)){
                                        h[k]=h[father(k)];
                                        *(h[k].p)=k;
                                        k=father(k);
                                    }
h[k]=vv;
*(h[k].p)=k;
}

//eliminare nod poz k

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

}

void ins(int val)
{
    n++;
    h[n].v=val;
    h[n].p=&stiva[dimstv];
    *(h[n].p)=n;
    percolate(n);
}

int caut(int val)
{
    for(int i=1; i<=n; i++)if(h[i].v==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].v);}
    else{
scanf("%d",&b);
if(a==1){ dimstv++;  ins(b);    }
if(a==2){ elim(stiva[b]);     }

}
}

    return 0;
}