Cod sursa(job #3124005)

Utilizator LucaMuresanMuresan Luca Valentin LucaMuresan Data 26 aprilie 2023 16:27:38
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.59 kb
#include <bits/stdc++.h>

using namespace std;

const int NMAX = 2e5;
int a[NMAX + 5];

struct heap
{
private:
    int n;
    int h[NMAX + 5];
public:
    heap() {}
    inline void up (int p)
    {
        while (p > 1 & a[h[p]] < a[h[(p >> 1)]])
        {
            swap(h[p], h[(p >> 1)]);
            p >>= 1;
        }
    }
    inline void down (int p)
    {
        while ((p << 1) <= n)
        {
            int q = p;
            if (a[h[(p << 1)]] < a[h[q]])
                q = (p << 1);
            if ((p << 1) < n && a[h[(p << 1)|1]] < a[h[q]])
                q = (p << 1)|1;
            if (p == q) break;
            swap(h[p], h[q]);
            p = q;
        }
    }

    inline void push (int x)
    {
        h[++n] = x;
        up(n);
    }
    inline void pop()
    {
        h[1] = h[n--];
        up(1);
        down(1);
    }
    inline int top()
    {
        return h[1];
    }
};

bitset<NMAX + 5>sters;
heap h;

int main()
{
    freopen("heapuri.in", "r", stdin);
    freopen("heapuri.out", "w", stdout);

    int q;
    scanf("%d", &q);
    int t = 0;
    while (q--)
    {
        int op;
        scanf("%d", &op);
        if (op == 1)
        {
            t++;
            scanf("%d", &a[t]);
            h.push(t);
        }
        else if (op == 2)
        {
            int x;
            scanf("%d", &x);
            sters[x] = true;
        }
        else
        {
            while (sters[h.top()])
                h.pop();
            printf("%d\n", a[h.top()]);
        }
    }

    return 0;
}