Cod sursa(job #3123992)

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

using namespace std;

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

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

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

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

bitset<NMAX + 5>sters;

int main()
{
    heap h;
    int q;
    in >> q;
    int t = 0;
    while (q--)
    {
        int op;
        in >> op;
        if (op == 1)
        {
            in >> a[++t];
            h.push(t);
        }
        else if (op == 2)
        {
            int x;
            in >> x;
            sters[x] = true;
        }
        else
        {
            while (sters[h.top()])
                h.pop();
            out << a[h.top()] << '\n';
        }
    }

    return 0;
}