Cod sursa(job #559567)

Utilizator ssergiussSergiu-Ioan Ungur ssergiuss Data 17 martie 2011 21:49:57
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.62 kb
#include <algorithm>
#include <fstream>

using namespace std;

const char Input[] = "heapuri.in";
const char Output[] = "heapuri.out";

const int Dim = 200001;

int N;
int h[Dim], m[Dim], p[Dim];

void UpH( int nod ) {

    int aux;

    while( nod > 1 ) {

        aux = nod >> 1;
        if( m[h[nod]] < m[h[aux]] ) {

            swap( h[nod], h[aux] );
            p[h[nod]] = nod;
            p[h[aux]] = aux;
            nod = aux;
        }
        else
            return;
    }
}

void DownH( int nod ) {

    int aux;

    while( nod < h[0] ) {

        if( (nod << 1) <= h[0] ) {

            aux = nod << 1;
            if( m[h[aux + 1]] < m[h[aux]] )
                ++aux;
        }
        else
            return;

        if( m[h[nod]] > m[h[aux]] ) {

            swap( h[nod], h[aux] );
            p[h[nod]] = nod;
            p[h[aux]] = aux;
            nod = aux;
        }
        else
            return;
    }
}

void HPush( int x ) {

    h[++h[0]] = x;
    p[x] = h[0];
    UpH( h[0] );
}

void HPop( int nod ) {

    h[nod] = h[h[0]--];
    p[h[nod]] = nod;
    DownH( nod );
}

int main() {

    ifstream fin( Input );
    ofstream fout( Output );

    int x, tp;

    fin >> N;
    while( N-- ) {

        fin >> tp;
        if( tp == 1 ) {

            fin >> x;
            m[++m[0]] = x;
            HPush( m[0] );
        }
        else if( tp == 2 ) {

            fin >> x;
            HPop( p[x] );
        }
        else
            fout << m[h[1]] << "\n";
    }

    fin.close();
    fout.close();

    return 0;
}