Cod sursa(job #613527)

Utilizator zeeboBuzatu Vlad zeebo Data 28 septembrie 2011 22:12:59
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.04 kb
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int h[200001],a[200001],b[200001],n,i,j,x,y;
void swap (int &q, int &r)
{
   int aux=q;
   q=r;
   r=aux;
}
void heapup (int j)
{
    if (h[j]<h[j/2])
    {
        swap (h[j],h[j/2]);
        swap (a[h[j]],a[h[j/2]]);
        heapup (j/2);
    }
    else return;
}
void heaprepair (int m, int j)
{
   int x,s,d;
   if (j>=m*2)
   {
     s=m*2;
     if (j>=m*2+1) d=m*2+1; else d=s;
     if (h[s]<h[d]) x=s; else x=d;
     h[m]=h[x];
     heaprepair(x,j);
   }
   else return;
}
int main()
{
    f>>n;
    j=0;
    for (i=1;i<=n;i++)
    {
        f>>x;
        if (x==1)
        {
            f>>y;
            b[++j]=y;
            a[y]=j;
            h[j]=y;
            heapup(j);
        }
        else
        if (x==2)
        {
            f>>y;
            heaprepair(a[b[y]],j);
            a[b[y]]=0;
            j--;
        }
        else g<<h[1]<<"\n";
    }
f.close();
g.close();
return 0;
}