Cod sursa(job #3124184)

Utilizator zzzzzZazaazaza zzzzz Data 27 aprilie 2023 10:17:08
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.56 kb
#include <iostream>
#include <algorithm>
#include <queue>

struct _p { int x, y; };
inline bool operator==(const _p& a, const _p& b) { return a.x == b.x && a.y == b.y; }
inline bool operator!=(const _p& a, const _p& b) { return !(a == b); }
inline bool operator<(const _p& a, const _p& b) { return a.x < b.x; }
inline bool operator>(const _p& a, const _p& b) { return a.x > b.x; }

class mheap : public std::priority_queue<_p, std::vector<_p>, std::greater<_p>>
{
public:
    bool _delete(const int& value) {
          auto it = this->c.begin();
          for(; it != this->c.end(); ++it) if(it->y == value) break;

          if (it == this->c.end()) {
              return false;
          }

          if (it == this->c.begin()) {
              this->pop();
          }
          else {
              this->c.erase(it);
              std::make_heap(this->c.begin(), this->c.end(), this->comp);
         }
         return true;
    }
};

int n, c, x, j = 1;
mheap heap;

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

    std::cin >> n;
    for(int i = 1; i <= n; ++i) {
        std::cin >> c;
        switch(c) {
            case 1: {
                std::cin >> x;
                heap.push({x, j++});
                break;
            }
            case 2: {
                std::cin >> x;
                heap._delete(x);
                break;
            }
            case 3: std::cout << heap.top().x << '\n';
        }
    }

    fclose(stdin);
    fclose(stdout);
    return 0;
}