Cod sursa(job #2497090)

Utilizator Teo_1101Mititelu Teodor Teo_1101 Data 22 noiembrie 2019 00:46:35
Problema Heapuri Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.67 kb
#include <iostream>
#include <fstream>

using namespace std;

const int NMAX = 200001;

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

int Heap[NMAX];
int in_heap[NMAX],inh;
int pos[NMAX];
int h_size, N;

void HeapUp( int nod )
{
    int parent = nod/2;

    if( Heap[parent] > Heap[nod] )
    {
        swap( Heap[parent], Heap[nod] );
        swap( pos[Heap[parent]], pos[Heap[nod]] );
        HeapUp( parent );
    }
}

void HeapDown( int nod )
{
    int son = 0;
    int lf = nod * 2;
    int rg = nod * 2 + 1;
    do
    {
        son = 0;
        if( lf <= h_size )
        {

            son = lf;
            if( rg <= h_size && Heap[rg] < Heap[lf] )
                son = rg;

            if( Heap[nod] <= Heap[son] )
                son = 0;
        }
        if( son )
        {
            swap( Heap[nod], Heap[son] );
            swap( pos[Heap[nod]], pos[Heap[son]] );
        }
    }while( son );
}

void HeapAdd( int x )
{
    Heap[++h_size] = x;
    pos[x] = h_size;
    HeapUp( h_size );

}

void HeapDel( int nod )
{
    Heap[nod] = Heap[h_size--];

    if( nod > 1 && Heap[nod] < Heap[nod/2] )HeapUp( nod );
    else HeapDown( nod );
}

void Read()
{
    fin >> N;

    int op, x;
    for( int i = 1; i <= N; ++i )
    {
        fin >> op;

        if( op == 1 )
        {
            fin >> x;
            in_heap[++inh] = x;
            HeapAdd( x );
        }
        if( op == 2 )
        {
            fin >> x;
            HeapDel( pos[in_heap[x]] );
        }
        if( op == 3 )
            fout << Heap[1] << '\n';
    }
}
int main()
{
    Read();
    return 0;
}