Cod sursa(job #2429801)

Utilizator MortemPlaiasu Iulia-Silvia Mortem Data 11 iunie 2019 10:41:35
Problema Heapuri Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.29 kb
#include <iostream>
#include <fstream>
#define next pos*2+1
std::ifstream fin("heapuri.in");
std::ofstream fout("heapuri.out");

int v[200500];
int ind[200500];
int h[800500];
int n;

void pushH(int a, int pos)
{
  if(h[pos]==0)
  {
    h[pos]=a;
    ind[a]=pos;
    return;
  }
  if(v[h[pos]]>v[a])
  {
    int aux=h[pos];
    h[pos]=a;
    ind[a]=pos;
    pushH(aux,pos);
    return;
  }
  for(int i=0;i<2;i++)
    if(h[next+i]==0)
    {
      h[next+i]=a;
      ind[a]=next+i;
      return;
    }
  for(int i=0;i<2;i++)
    if(v[h[next+i]]>v[a])
    {
      int aux=  h[next+i];
      h[next+i]=a;
      ind[a]=next+i;
      pushH(aux,next+i);
      return;
    }
}

void erase(int pos)
{
  h[pos]=0;
  if(h[next+1]==0&& h[next]==0)
    return;
  for(int i=0;i<2;i++)
    if(h[next+i]==0)
    {
      h[pos]=h[next+(i+1)%2];
      erase(next+(i+1)%2);
      return;
    }
  if(v[h[next+1]]<v[h[next]])
  {
    h[pos]=h[next+1];
    erase(next+1);
    return;
  }
  else
  {
    h[pos]=h[next];
    erase(next);
    return;
  }
}
int main()
{
  fin>>n;
  int elem=0;
  for(int i=1;i<=n;i++)
  {
    int c,j;
    fin>>c;
    if(c==1)
    {
      fin>>j;
      elem++;
      v[elem]=j;
      pushH(elem,0);
    }
    else if(c==2)
    {
      fin>>j;
      erase(ind[j]);
    }
    else
      fout<<v[h[0]]<<"\n";
  }
}