Cod sursa(job #1314976)

Utilizator AdrianaMAdriana Moisil AdrianaM Data 12 ianuarie 2015 15:14:09
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.33 kb
#include <fstream>
using namespace std;

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

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

void Insert();
void Delete(int poz);

int main()
{
    is >> t;
    while ( t-- )
    {
        is >> op;
        if ( op == 3 )
        {
            os << nr[h[1]] << "\n";
            continue;
        }
        is >> a;
        if ( op & 1 )
        {
            nr[++el] = a;
            Insert();
        }
        else
            Delete(p[a]);
    }
    is.close();
    os.close();
    return 0;
}

void Insert()
{
    p[el] = ++n;
    h[n] = el;
    int up = n / 2, down = n;
    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;
        up /= 2;
        down /= 2;
    }
}

void Delete(int poz)
{
    if ( poz == n )
    {
        --n;
        return;
    }
    p[h[n]] = poz;
    h[poz] = h[n--];
    int up = poz, down = poz * 2;
    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;
    }
}