Cod sursa(job #2684643)

Utilizator retrogradLucian Bicsi retrograd Data 14 decembrie 2020 13:55:08
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1 kb
#include <bits/stdc++.h>

using namespace std;

struct SkewHeap {
  struct Node { int key, l = -1, r = -1, p = -1; };
  vector<Node> T;
  int root = -1;

  int merge(int a, int b) {
    if (b == -1 || a == -1) return a + b + 1;
    if (T[a].key > T[b].key) swap(a, b);
    int &l = T[a].l, &r = T[a].r;
    swap(l, r); l = merge(l, b); 
    if (l != -1) T[l].p = a;
    return a;
  }
  void Push(int key) { 
    T.push_back(Node{key}); 
    root = merge(root, T.size() - 1);
  }
  void Pop(int x) { 
    int nx = merge(T[x].l, T[x].r); 
    if (x == root) root = nx;
    else {
      int p = T[x].p;
      T[p].l == x ? T[p].l = nx : T[p].r = nx;
    }
  }
  int Min() { return T[root].key; }
};

int main() {
  ifstream cin("heapuri.in");
  ofstream cout("heapuri.out");
  int n; cin >> n;
  SkewHeap H;
  for (int i = 0; i < n; ++i) {
    int typ; cin >> typ;
    if (typ == 1) { int x; cin >> x; H.Push(x); }
    if (typ == 2) { int x; cin >> x; H.Pop(--x); }
    if (typ == 3) { cout << H.Min() << '\n'; }
  }
  return 0;
}