Cod sursa(job #437475)

Utilizator alexandru92alexandru alexandru92 Data 9 aprilie 2010 19:44:27
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 kb
/* 
 * File:   main.cpp
 * Author: VirtualDemon
 *
 * Created on April 9, 2010, 6:29 PM
 */
#include <cstdlib>
#include <fstream>
#define Nmax 200001

/*
 *
 */
using namespace std;
int N;
int H[Nmax], v[Nmax], P[Nmax];
inline void DownHeap( int k )
{
    int son;
    for( ; ; )
    {
        son=2*k;
        if( son > N )
            return;
        if( son < N && v[H[son+1]] < v[H[son]] )
            ++son;
        if( v[H[k]] <= v[H[son]] )
            return;
        swap( H[k], H[son] );
        P[H[k]]=k;
        P[H[son]]=son;
        k=son;
    }
}
inline void UpHeap( int k )
{
    int key=v[H[k]], f=k/2;
    while( k > 1 && key < v[H[f]] )
    {
        swap( H[k], H[f] );
        P[H[k]]=k;
        P[H[f]]=f;
        k=f;
        f=k/2;
    }
}
inline void pop( int x )
{
    int y=P[x];
    P[x]=P[H[N]];
    H[y]=H[N];
    --N;
    DownHeap(y);
}
inline void push( int x )
{
    H[++N]=x;
    P[x]=N;
    UpHeap( N );
}
int main( void )
{
    int M, i, j, k;
    ifstream in( "heapuri.in" );
    ofstream out( "heapuri.out" );
    for( in>>M, k=1; M; --M )
    {
        in>>i;
        if( i <= 2 )
        {
            in>>j;
            if( 1 == i )
                v[k]=j, push( k ), ++k;
            else pop(j);
        }
        else out<<v[H[1]]<<'\n';
    }
    return EXIT_SUCCESS;
}