Cod sursa(job #2721476)

Utilizator preda.andreiPreda Andrei preda.andrei Data 11 martie 2021 20:46:16
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.02 kb
#include <fstream>
#include <queue>
#include <unordered_set>

using namespace std;

struct Elem {
    int value;
    int index;

    bool operator>(const Elem& other) const {
        return value > other.value;
    }
};

template <typename T>
using MinHeap = priority_queue<T, vector<T>, greater<T>>;

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

    MinHeap<Elem> heap;
    unordered_set<int> invalid;
    int next_index = 1;

    int n;
    fin >> n;

    for (int i = 0; i < n; i += 1) {
        int type;
        fin >> type;

        if (type == 1) {
            int value;
            fin >> value;
            heap.push(Elem{value, next_index});
            next_index += 1;
        } else if (type == 2) {
            int index;
            fin >> index;
            invalid.insert(index);
        } else {
            while (invalid.find(heap.top().index) != invalid.end()) {
                heap.pop();
            }
            fout << heap.top().value << "\n";
        }
    }
    return 0;
}