Cod sursa(job #2924128)

Utilizator IvanAndreiIvan Andrei IvanAndrei Data 26 septembrie 2022 07:46:22
Problema Heapuri Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.37 kb
#include <fstream>

using namespace std;

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

const int max_size = 2e5 + 1;

int h[max_size], a[max_size], poz[max_size], n, l;

void push (int x)
{
    while (x / 2 && a[h[x]] < a[h[x / 2]])
    {
        swap(h[x], h[x / 2]);
        poz[h[x]] = x;
        poz[h[x / 2]] = x / 2;
        x /= 2;
    }
}

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

int main ()
{
    int q;
    in >> q;
    while (q--)
    {
        int x, y;
        in >> x;
        if (x < 3)
        {
            in >> y;
        }
        if (x == 1)
        {
            l++;
            n++;
            a[n] = y;
            h[l] = n;
            poz[n] = l;
            push(l);
        }
        if (x == 2)
        {
            a[y]= -1;
            push(poz[y]);
            poz[h[1]] = 0;
            h[1] = h[l--];
            poz[h[1]] = 1;
            if (l > 1)
            {
                pop(1);
            }
        }
        if (x == 3)
        {
            out << a[h[1]] << '\n';
        }
    }
    in.close();
    out.close();
    return 0;
}