Cod sursa(job #2792289)

Utilizator Alex_tz307Lorintz Alexandru Alex_tz307 Data 1 noiembrie 2021 13:12:36
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.5 kb
#include <bits/stdc++.h>

using namespace std;

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

struct Heap {
  int cnt = 0;
  vector<int> h{0}, entry{0}, pos;

  Heap(int n) {
    pos.resize(n + 1);
  }

  void swapNodes(int u, int v) {
    swap(h[u], h[v]);
    swap(pos[entry[u]], pos[entry[v]]);
    swap(entry[u], entry[v]);
  }

  void Insert(int x) {
    entry.emplace_back(++cnt);
    pos[cnt] = h.size();
    h.emplace_back(x);
    int v = h.size() - 1;
    while (1 < v && h[v] < h[v >> 1]) {
      swapNodes(v, v >> 1);
      v >>= 1;
    }
  }

  void Erase(int k) {
    int v = pos[k];
    swapNodes(v, h.size() - 1);
    h.pop_back();
    entry.pop_back();
    while ((v << 1) < (int)h.size()) {
      int son = v << 1;
      if ((son | 1) < (int)h.size() && h[son | 1] < h[son]) {
        son |= 1;
      }
      if (h[v] <= h[son]) {
        break;
      }
      swapNodes(v, son);
      v = son;
    }
  }

  void EraseMin() {

  }

  int getMin() {
    return h[1];
  }
};

void TestCase() {
  int n;
  fin >> n;
  Heap h(n);
  for (int i = 1; i <= n; ++i) {
    int op;
    fin >> op;
    if (op <= 2) {
      int k;
      fin >> k;
      if (op == 1) {
        h.Insert(k);
      } else {
        h.Erase(k);
      }
    } else {
      fout << h.getMin() << '\n';
    }
  }
}

int main() {
  int tests = 1;
  for (int tc = 1; tc <= tests; ++tc) {
    TestCase();
  }
  fin.close();
  fout.close();
  return 0;
}