Cod sursa(job #464718)

Utilizator APOCALYPTODragos APOCALYPTO Data 21 iunie 2010 15:33:07
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.62 kb
using namespace std;
#include<iostream>
#include<fstream>
#include<algorithm>
int H[200005],poz[200005],N,tot,pos[200005];
ofstream fout("heapuri.out");

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 min()
{
    return H[1];
}

void percolate(int K)
{int key=H[K];
while(K>1&&H[K]<H[father(K)])
    {
        H[K]=H[father(K)];
        H[father(K)]=key;

        swap(pos[K],pos[father(K)]);
        poz[pos[K]]=K;
        poz[pos[father(K)]]=father(K);
        K=father(K);
    }

}
void sift(int K)
{int m=K,key=H[K];
if(left_son(K)<=N) if(H[2*K]<H[m]) m=2*K;
if(2*K+1<=N) if(H[2*K+1]<H[m]) m=2*K+1;

if(m!=K)
{H[K]=H[m];
H[m]=key;

        swap(pos[K],pos[m]);
        poz[pos[K]]=K;
        poz[pos[m]]=m;
sift(m);
}
}


void insereaza(int y)
{
    tot++;
    H[++N]=y;
    poz[tot]=N;
    pos[N]=tot;
    percolate(N);
}
void sterge(int y)
{y=poz[y];
    H[y]=H[N];
    N--;

    if((y>1)&&(H[y]<H[father(y)]))
    {
        percolate(y);
    }
    else
    sift(y);
}

void cit()
{int i,x,y,n,z;
    ifstream fin("heapuri.in");
    fin>>n;
    cout<<n<<"\n";
    for(i=1;i<=n;i++)
       {fin>>x;

       if(x==1)
           {fin>>y;
           cout<<x;
           insereaza(y);

           }
       else
         if(x==2)
           {fin>>y;
           sterge(y);
           }
         else
           fout<<min()<<"\n";
       }
      cout<<tot;
   fin.close();
}
int main()
{
    cit();
    fout.close();
    return 0;
}