Cod sursa(job #2741928)

Utilizator icnsrNicula Ionut icnsr Data 19 aprilie 2021 19:22:29
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.92 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <unordered_set>
 
using value_t = std::pair<int, unsigned>;
 
const auto compare = [](const value_t& x1, const value_t& x2)
{
        return x1.first > x2.first;
};
 
class LazyHeap
{
public:
        LazyHeap() = default;
 
        __attribute__((noinline)) void push(const int x)
        {
                elements.push_back({x, count});
                std::push_heap(elements.begin(), elements.end(), compare);
                ++count;
        }
 
        void erase(const unsigned k)
        {
                deleted[k] = true;
        }
 
        __attribute__((noinline)) int top()
        {
                while(deleted[elements.front().second])
                {
                        std::pop_heap(elements.begin(), elements.end(), compare);
                        elements.pop_back();
                }
 
                return elements.front().first;
        }
 
        static constexpr unsigned MAX_N = 200000;
 
private:
        std::vector<value_t> elements;
        std::array<bool, MAX_N + 1> deleted;
        unsigned count = 1;
};
 
int main()
{
        std::ifstream fin("heapuri.in");
        std::ofstream fout("heapuri.out");
 
        unsigned N;
        fin >> N;
 
        LazyHeap heap;
        for(unsigned i = 0; i < N; ++i)
        {
                int op;
                fin >> op;
 
                int value;
 
                if(op != 3)
                {
                        fin >> value;
                }
 
                switch(op)
                {
                        case 1:
                                heap.push(value);
                                break;
                        case 2:
                                heap.erase(value);
                                break;
                        case 3:
                                fout << heap.top() << '\n';
                                break;
                }
        }
}