Problem p02289 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

template <typename T> struct node {
  T val;
  vector<node<T> *> chi;
  node() : chi() {}
  node(int v) : val(v), chi() {}
  void Init(int v) { val = v; }
  void erase() {
    for (auto p : chi) {
      p->erase();
      delete p;
    }
  }
};

template <typename T> node<T> *merge(node<T> *a, node<T> *b) {
  if (a == nullptr)
    return b;
  if (b == nullptr)
    return a;
  if (a->val <= b->val)
    swap(a, b);
  a->chi.push_back(b);
  return a;
}

template <typename T> node<T> *sub_merge(const vector<node<T> *> &p) {
  int n = p.size();
  vector<node<T> *> tmp;
  for (int i = 0; i < n; i += 2) {
    if (i + 1 < n) {
      tmp.push_back(merge(p[i], p[i + 1]));
    } else {
      tmp.push_back(p[i]);
    }
  }
  reverse(tmp.begin(), tmp.end());
  n = tmp.size();
  node<T> *res = tmp[0];
  for (int i = 1; i < n; i++) {
    res = merge(res, tmp[i]);
  }
  return res;
}

node<int> *new_node(int val) { return new node<int>(val); }

class Priority_Queue {
  node<int> *root;

public:
  Priority_Queue() : root(nullptr) {}
  Priority_Queue(int val) : root(new_node(val)) {}
  void push(int val) { meld(new_node(val)); }
  int top() { return root->val; }
  void meld(node<int> *p) { root = merge(root, p); }
  void pop() {
    if (root->chi.empty()) {
      delete root;
      root = nullptr;
      return;
    }
    auto t = root;
    root = sub_merge(root->chi);
    delete t;
  }
  ~Priority_Queue() {
    if (root != nullptr) {
      root->erase();
      delete root;
    }
  }
};

int main() {
  cin.sync_with_stdio(false);
  int val;
  string S;
  Priority_Queue pq;

  while (cin >> S, S != "end") {
    if (S == "extract") {
      printf("%d\\n", pq.top());
      pq.pop();
    } else {
      cin >> val;
      pq.push(val);
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

template <typename T> struct node {
  T val;
  vector<node<T> *> chi;
  node() : chi() {}
  node(int v) : val(v), chi() {}
  void Init(int v) { val = v; }
};

template <typename T> node<T> *merge(node<T> *a, node<T> *b) {
  if (a == nullptr)
    return b;
  if (b == nullptr)
    return a;
  if (a->val <= b->val)
    swap(a, b);
  a->chi.push_back(b);
  return a;
}

template <typename T> node<T> *sub_merge(const vector<node<T> *> &p) {
  int n = p.size();
  vector<node<T> *> tmp;
  for (int i = 0; i < n; i += 2) {
    if (i + 1 < n) {
      tmp.push_back(merge(p[i], p[i + 1]));
    } else {
      tmp.push_back(p[i]);
    }
  }
  reverse(tmp.begin(), tmp.end());
  n = tmp.size();
  node<T> *res = tmp[0];
  for (int i = 1; i < n; i++) {
    res = merge(res, tmp[i]);
  }
  return res;
}

node<int> pool[2000000];
int it;

node<int> *new_node(int val) {
  pool[it].Init(val);
  return &pool[it++];
}

class Priority_Queue {
  node<int> *root;

public:
  Priority_Queue() : root(nullptr) {}
  Priority_Queue(int val) : root(new_node(val)) {}
  void push(int val) { meld(new_node(val)); }
  int top() { return root->val; }
  void meld(node<int> *p) { root = merge(root, p); }
  void pop() {
    if (root->chi.empty()) {
      root = nullptr;
      return;
    }
    root = sub_merge(root->chi);
  }
};

int main() {
  cin.sync_with_stdio(false);
  int val;
  string S;
  Priority_Queue pq;

  while (cin >> S, S != "end") {
    if (S == "extract") {
      printf("%d\\n", pq.top());
      pq.pop();
    } else {
      cin >> val;
      pq.push(val);
    }
  }
  return 0;
}