Cod sursa(job #851555)

Utilizator blechereyalBlecher Eyal blechereyal Data 10 ianuarie 2013 01:15:16
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.37 kb
using namespace std;
#include <fstream>
#define father(nod) (nod/2)
#define left_son(nod) (nod*2)
#define right_son(nod) (nod*2+1)
#define max 20000000
ifstream f("heapuri.in");
ofstream g("heapuri.out");


int H[200003],vals[200003],poz[200003],N;

void swap(int &a,int &b)
{int aux;
aux=a;
a=b;
b=aux;
    
     
}

void sift(int K)
{
int son;
do
  {
  son=0;
  
  if (left_son(K) <= N)
     {
     son=left_son(K);
     if (( right_son(K)<=N ) && (vals[H[right_son(K)]] <vals[H[left_son(K)]]))
      son=right_son(K);
     }
     
  if (vals[H[son]]>=vals[H[K]]) son=0;    
  
  if (son) 
     {
     swap(H[son],H[K]);
     poz[H[son]]=son;
     poz[H[K]]=K;
     K=son;
     }
  } while (son);
}

void percolate(int K)
{
while ( ( vals[H[K]]<vals[H[father(K)]] ) && (K>1) )
 {swap(H[K],H[father(K)]);
 poz[H[K]]=K;
 poz[H[father(K)]]=father(K);
 K=father(K);
 }

}



void del(int care)
{

vals[care]=max;
sift(poz[care]);
}

void add(int val)
{
N++;
vals[N]=val;
H[N]=N;
poz[N]=N;
percolate(N);  

}


int main()
{int lines,N=0,cod,x;
f>>lines;
for (int i=1;i<=lines;i++)
{
f>>cod;
switch (cod)
       {case 1:
       f>>x;
       add(x);
       break;
       case 2:
       f>>x;
       del(x);
       break;
       case 3:
       g<<vals[H[1]]<<"\n";
       break;
       }
}


return 0;
}