Cod sursa(job #1312267)

Utilizator AdrianaMAdriana Moisil AdrianaM Data 9 ianuarie 2015 02:19:23
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.74 kb
#include <fstream>
#include <vector>
using namespace std;

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

int n, el, m, op, x;
int h[200001], p[200001], nr[200001];

void Insert(int x);
void Delete(int x);

int main()
{
    is >> m;
    while ( m-- )
    {
        is >> op;
        if ( op == 3 )
            os << nr[h[1]] << "\n";
        else
        {
            is >> x;
            if ( op == 1 )
                Insert(x);
            else
                Delete(p[x]);
            /*for ( int i = 1; i <= n; ++i )
                os << h[i] << " ";
            os << "\n";
            for ( int i = 1; i <= n; ++i )
                os << nr[h[i]] << " ";
            os << "\n";
            for ( int i = 1; i <= n; ++i )
                os << p[h[i]] << " ";
            os << "\n";
            os << "\n";*/
        }
    }
    is.close();
    os.close();
    return 0;
}

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

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