Cod sursa(job #1512600)

Utilizator Narcys01Ciovnicu Narcis Narcys01 Data 28 octombrie 2015 12:34:48
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.27 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

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

int n, N, L;
int h[200005], a[200005], p[200005];

void Insrt(int k)
{
    while (k > 1 && a[h[k]] < a[h[k/2]])
    {
        swap(h[k], h[k/2]);
        p[h[k]] = k;
        p[h[k/2]] = k / 2;
        k = k / 2;
    }
}

void Elim(int x)
{
    int y = 0;
    while (x != y)
    {
        y = x;
        if (y * 2 <= L && a[h[x]] > a[h[y*2]])
            x = y * 2;
        if (y * 2 + 1 <= L && a[h[x]] > a[h[y*2+1]])
            x = y * 2 + 1;
        swap(h[x], h[y]);
        p[h[x]] = x;
        p[h[y]] = y;
    }
}

int main()
{
    int x, y;
    fin >> n;
    N = 0;
    while (n--)
    {
        fin >> x;
        if (x == 1)
        {
            fin >> y;
            L++;
            N++;
            a[N] = y;
            h[L] = N;
            p[N] = L;
            Insrt(L);
        }
        else if (x == 2)
        {
            fin >> y;
            a[y] = -1;
            Insrt(p[y]);
            p[h[1]] = 0;
            h[1] = h[L--];
            p[h[1]] = 1;
            Elim(1);
        }
        else
            fout << a[h[1]] << "\n";
    }
    return 0;
}