Cod sursa(job #294138)

Utilizator jeanFMI - Petcu Ion Cristian jean Data 2 aprilie 2009 12:41:25
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.13 kb
#include<fstream>
using namespace std;
int i,n,m,k,pozitie,x,op,p[200010];
struct sir{ int inf,pos;} ;
sir v[200010];

void swap (int i, int j)

 { sir aux; int aux2;

   aux2=p[v[i].pos]; p[v[i].pos]=p[v[j].pos]; p[v[j].pos]=aux2;

   aux=v[i]; v[i]=v[j]; v[j]=aux;
 }

void actualizare ( int poz)

 {
     while((poz<<1)<=n && v[poz].inf>v[poz<<1].inf)

       {swap(poz,poz<<1);
        poz<<=1;
       }
 }
void actualizare1 ( int poz)

 {
     while(poz >0 && v[poz].inf<v[poz>>1].inf)

       {swap(poz,poz>>1);
        poz>>=1;
       }
 }
 
int main()
{
ifstream f("heapuri.in");
ofstream g("heapuri.out");

f>>m;

for(i=1;i<=m;i++)

 { f>>op;

    if(op==1) { f>>x;
               v[++n].inf=x;
               v[n].pos=++k;
               p[k]=n;
               actualizare1(n);
              }

     else if(op==2) {f>>x;

                     pozitie=p[x];

                     swap(pozitie,n); --n;

                     actualizare(pozitie);
                    }
       else

         g<<v[1].inf<<'\n';
 }
f.close();
g.close();
return 0;
}