Cod sursa(job #830436)

Utilizator antonioteoZait Teodor Antonio antonioteo Data 6 decembrie 2012 21:01:19
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.22 kb
#include <fstream>
#define N 200001
#define L ((i)<<1)
#define R (L+1)
#define T ((i)>>1)
using namespace std;
const char iname[] = "heapuri.in";
const char oname[] = "heapuri.out";
ifstream fin(iname);
ofstream fout(oname);
int H[ N ] , pos[ N ] , poz[ N ];
int n , i , op , x , nh , nr;
inline void swap ( int i , int j )
{
	int aux = H[ i ];
    H[ i ] = H[ j ];
    H[ j ] = aux;
	aux = poz[i];
    poz[ i ] = poz[ j ];
    poz[ j ] = aux;
    pos[ poz[ i ] ] = i;
    pos[ poz[ j ] ] = j;
}
inline void UpHeap ( int i )
{
	if ( i <= 1 ) return;
	if ( H[ i ] < H[ T ] ) swap( i , T ) , UpHeap( T );
}
inline void DownHeap ( int i )
{
	int m = i;
    if ( L <= nh ) if ( H[ L ] < H[ m ] ) m = L;
    if ( R <= nh ) if ( H[ R ] < H[ m ] ) m = R;
    if ( m != i ) swap( i , m ), DownHeap( m );
}
int main()
{
	fin >> n;
	while ( n-- )
	{
		fin >> op;
		if ( op != 3 ) fin >> x;
		if ( op == 1 )
		{
			H[ ++nh ] = x;
            poz[ nh ] = ++nr;
            pos[ poz[ nh ] ] = nh;
            UpHeap( nh );
		}
		if ( op == 2 )
		{
			int px = pos[ x ];
            swap( px , nh-- );
            DownHeap( px );
		}
		if ( op == 3 )
		{
			fout << H[ 1 ] << '\n';
		}
	}
	return 0;
}