Cod sursa(job #3124190)

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

class mheap : public std::priority_queue<int, std::vector<int>, std::greater<int>>
{
public:
    bool _delete(const int& value) {
          auto it = std::find(this->c.begin(), this->c.end(), value);

          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;
int poz[200005];
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);
                poz[j++] = x;
                break;
            }
            case 2: {
                std::cin >> x;
                heap._delete(poz[x]);
                break;
            }
            case 3: std::cout << heap.top() << '\n';
        }
    }

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