Cod sursa(job #1256290)

Utilizator AdrianaMAdriana Moisil AdrianaM Data 6 noiembrie 2014 01:49:37
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.68 kb
#include <fstream>
#include <vector>
using namespace std;

ifstream is("heapuri.in");
ofstream os("heapuri.out");

int n, el, m, t, aux;
int h[200001], nr[200001], p[200001];

void INSERT();
void DELETE(int k);

int main()
{
    is >> m;
    while ( m-- )
    {
        is >> t;
        if ( t == 3 )
        {
            os << nr[h[1]] << "\n";
            continue;
        }
        if ( t == 1 )
        {
            is >> nr[++el];
            INSERT();
        }
        else
        {
            is >> aux;
            DELETE(p[aux]);
        }
        /*for ( int i = 1; i <= n; ++i )
            os << h[i] << " ";
        os << "\n";
        for ( int i = 1; i <= 4; ++i )
            os << p[i] << " ";
        os << "\n\n";*/
    }
    is.close();
    os.close();
    return 0;
}

void INSERT()
{
    ++n;
    h[n] = el;
    p[el] = n;
    int up = n / 2, down = n;
    while ( up && nr[h[down]] < nr[h[up]] )
    {
        aux = h[down];
        h[down] = h[up];
        h[up] = aux;
        aux = p[h[down]];
        p[h[down]] = p[h[up]];
        p[h[up]] = aux;
        down = up;
        up /= 2;
    }
}

void DELETE(int k)
{
    if ( k == n )
    {
        --n;
        return;
    }
    p[h[n]] = k;
    h[k] = h[n];
    --n;
    int up = k, down = k * 2;
    while ( down <= n && ( nr[h[up]] > nr[h[down]] || nr[h[up]] > nr[h[down + 1]] ) )
    {
        if ( nr[h[down]] > nr[h[down + 1]] )
            ++down;
        aux = h[down];
        h[down] = h[up];
        h[up] = aux;
        aux = p[h[down]];
        p[h[down]] = p[h[up]];
        p[h[up]] = aux;
        up = down;
        down *= 2;
    }
}