Cod sursa(job #1258993)

Utilizator laurageorgescuLaura Georgescu laurageorgescu Data 9 noiembrie 2014 16:52:29
Problema Heapuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.33 kb
#include<fstream>

using namespace std;

ifstream fin( "heapuri.in" );
ofstream fout( "heapuri.out" );

const int nmax = 200000;
int w, hw, v[ nmax + 1 ], p[ nmax + 1 ], h[ nmax + 10 ];

void insert_elem() {
    int aux, poz;
    h[ hw ++ ] = w;
    p[ w ] = hw - 1;
    poz = hw - 1;
    while ( 1 < poz && v[ h[ poz / 2 ] ] > v[ h[ poz ] ] ) {
        p[ h[ poz / 2 ] ] = poz;
        p[ h[ poz ] ] = poz / 2;

        aux = h[ poz / 2 ];
        h[ poz / 2 ] = h[ poz ];
        h[ poz ] = aux;

        poz /= 2;
    }
}
void erase_elem( int x ) {
    int poz = p[ x ];
    while ( 2 * poz <= hw ) {
        if ( 2 * poz + 1 >= hw || v[ h[ 2 * poz ] ] < v[ h[ 2 * poz + 1 ] ] ) {
            p[ h[ 2 * poz ] ] = poz;
            h[ poz ] = h[ 2 * poz ];
            poz = 2 * poz;
        } else {
            p[ h[ 2 * poz + 1 ] ] = poz;
            h[ poz ] = h[ 2 * poz + 1 ];
            poz = 2 * poz + 1;
        }
    }
    -- hw;
}
int main() {
    int n, t, x;
    fin >> n;
    w = hw = 1;
    for( int i = 0; i < n; ++ i ) {
        fin >> t;
        if ( t == 1 ) {
            fin >> v[ w ];
            insert_elem();
            ++ w;
        } else if ( t == 2 ) {
            fin >> x;
            erase_elem( x );
        } else {
            fout << v[ h[ 1 ] ] << "\n";
        }
    }
    fin.close();
    fout.close();
    return 0;
}