Pagini recente » Cod sursa (job #1940627) | Cod sursa (job #3202864) | Cod sursa (job #1682408) | Cod sursa (job #2469246) | Cod sursa (job #2741928)
#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;
}
}
}