Cod sursa(job #2254799)

Utilizator zdavid112zIon David-Gabriel zdavid112z Data 5 octombrie 2018 23:07:51
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.5 kb
#include <bits/stdc++.h>

using namespace std;

struct rec
{
    int ind, val;
    bool operator<(const rec& o) const
    {
        return val < o.val;
    }
} h[200005];
int lh; /// ultima pos ocupata
int del[200005];

void hpush(const rec& val)
{
    lh++;
    int nod = lh;
    h[nod] = val;
    while(nod != 1 && val < h[nod / 2])
    {
        swap(h[nod], h[nod / 2]);
        nod /= 2;
    }
}

void hpop()
{
    h[1] = h[lh];
    lh--;
    int nod = 1;
    while(nod * 2 + 1 <= lh && (h[nod * 2] < h[nod] || h[nod * 2 + 1] < h[nod]))
    {
        if(h[nod * 2] < h[nod * 2 + 1])
        {
            swap(h[nod * 2], h[nod]);
            nod = nod * 2;
        }
        else
        {
            swap(h[nod * 2 + 1], h[nod]);
            nod = nod * 2 + 1;
        }
    }
    if(nod * 2 <= lh && h[nod * 2] < h[nod])
        swap(h[nod], h[nod * 2]);
}

int main()
{
    freopen("heapuri.in", "r", stdin);
    freopen("heapuri.out", "w", stdout);
    int n, c, x, elc = 1;
    scanf("%d", &n);
    while(n--)
    {
        scanf("%d", &c);
        if(c == 1)
        {
            scanf("%d", &x);
            rec r;
            r.val = x;
            r.ind = elc++;
            hpush(r);
        }
        else if(c == 2)
        {
            scanf("%d", &x);
            del[x] = 1;
        }
        else
        {
            while(del[h[1].ind])
                hpop();
            printf("%d\n", h[1].val);
        }
    }
    return 0;
}