Cod sursa(job #520307)

Utilizator DuxarFII-Stefan-Negrus Duxar Data 7 ianuarie 2011 22:54:10
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.34 kb
#include<fstream>
#include<algorithm>
#define Nmax 200002
using namespace std;

ifstream f("heapuri.in");
ofstream g("heapuri.out");

int n,a[Nmax],P[Nmax],C[Nmax],Pc[Nmax],nr,N;

void heaps();
void combine(int i,int n);

int main()
{
  heaps();
  f.close();
  g.close();
  return 0;
}

void heaps()
{
  int i,c,x,N=0,pos,j;
  f>>n;
  for (i=1;i<=n;++i)
  {
    f>>c;
    if (c==3) 
    {
      g<<a[1]<<'\n';
      continue;
    }
      else if (c==1)
    {
      ++N;
      f>>x;
      a[N]=x;
      ++nr;
      Pc[N]=nr;      
      C[nr]=x;
      P[nr]=N;      
      swap(a[1],a[N]);
      swap(P[Pc[1]],P[Pc[N]]);
      swap(Pc[1],Pc[N]);
      //combine(1,N);
    }
    else if (c==2)
    {
      f>>x;
      pos=P[x];
      swap(a[pos],a[N]);
      swap(P[Pc[pos]],P[Pc[N]]);
      swap(Pc[pos],Pc[N]);
      --N;
      //combine(pos,N);
    }
    for (j=N/2;j;--j)
      combine(j,N);
  }
}

void combine(int i,int n)
{
  int c,t,aux=a[i],Pcax,Cax;
  Cax=P[Pc[i]];
  Pcax=Pc[i];
  t=i;
  c=t*2;
  while (c<=n)
  {
    if (c+1<=n&&a[c+1]<a[c])
      ++c;
    if (aux>a[c])
    {
      a[t]=a[c];
      P[Pc[c]]=P[Pc[t]];
      Pc[t]=Pc[c];
      //P[Pc[t]]=P[Pc[c]];
      
      t=c;
      c*=2;
    }
    else c=n+1;
  }
    a[t]=aux;
    Pc[t]=Pcax;
    P[Pc[t]]=t;

}