Problem p02289 (C++)

Buggy submission (status: Time Limit Exceeded):
#define _CRT_SECURE_NO_WARNINGS
#include <cassert>
#include <cstddef>
#include <functional>
#include <memory>
#include <type_traits>
#include <utility>

template <class TotallyOrderedSet> class pairing_heap {

public:
  using set_type = TotallyOrderedSet;
  using value_type = typename set_type::value_type;
  using const_reference = const value_type &;
  using size_type = std::size_t;

private:
  struct node_t {
    value_type value;
    node_t *left, *right;
    template <class... Args>
    node_t(Args &&...args) noexcept(
        std::is_nothrow_constructible<value_type, Args...>::value)
        : value(std::forward<Args>(args)...), left(nullptr), right(nullptr) {}
  };
  using pointer = node_t *;
  pointer root;
  size_type s;
  static pointer merge(pointer x, pointer y) noexcept(
      noexcept(set_type::less_equal(std::declval<value_type>(),
                                    std::declval<value_type>()))) {
    if (!set_type::less_equal(x->value, y->value))
      std::swap(x, y);
    y->right = x->left;
    x->left = y;
    return x;
  }
  static pointer mergelist(pointer curr) noexcept(
      noexcept(merge(std::declval<pointer>(), std::declval<pointer>()))) {
    pointer head(nullptr), temp(nullptr), next(nullptr);
    while (curr) {
      next = (curr->right);
      if (next) {
        temp = (next->right);
        curr = merge((curr), (next));
      }
      curr->right = (head);
      head = (curr);
      curr = (temp);
    }
    if (head) {
      curr = (head);
      head = (curr->right);
    }
    while (head) {
      next = (head->right);
      curr = merge((curr), (head));
      head = (next);
    }
    if (curr)
      curr->right = nullptr;
    return (curr);
  }

public:
  pairing_heap() noexcept : root(), s(0) {}

  bool empty() const noexcept { return !root; }
  size_type size() const noexcept { return s; }

  const_reference top() const noexcept {
    assert(!empty());
    return root->value;
  }

  void push(const value_type &x) {
    if (empty())
      root = new node_t(x);
    else
      root = merge((root), new node_t(x));
    ++s;
  }
  void push(value_type &&x) {
    if (empty())
      root = new node_t((x));
    else
      root = merge((root), new node_t((x)));
    ++s;
  }
  template <class... Args> void emplace(Args &&...args) {
    if (empty())
      root = new node_t(std::forward<Args>(args)...);
    else
      root = merge((root), new node_t(std::forward<Args>(args)...));
    ++s;
  }

  void pop() noexcept(noexcept(mergelist(std::declval<pointer>()))) {
    assert(!empty());
    root = mergelist((root->left));
    --s;
  }

  pairing_heap &meld(pairing_heap &other) noexcept(
      noexcept(merge(std::declval<pointer>(), std::declval<pointer>()))) {
    if (!other.empty()) {
      s += other.s;
      other.s = 0;
      root = merge((root), (other.root));
    }
    return *this;
  }
  pairing_heap &operator+=(pairing_heap &other) noexcept(
      noexcept(meld(std::declval<pairing_heap &>()))) {
    return meld(other);
  }
};
#include <algorithm>
#include <limits>
#include <utility>

template <class T> class ordered_set {
public:
  using value_type = T;

  static bool less_equal(const value_type &x, const value_type &y) noexcept(
      noexcept(std::declval<value_type>() >= std::declval<value_type>())) {
    return x >= y;
  }
};

#include <cstdio>
#include <string>
void scan(std::string &s) {
  s.clear();
  int c = fgetc(stdin);
  while (c == ' ' || c == '\n')
    c = fgetc(stdin);
  while (c != ' ' && c != '\n') {
    s.push_back(c);
    c = fgetc(stdin);
  }
}

int main() {
  pairing_heap<ordered_set<int>> Q;
  std::string s;
  int k;
  while (scan(s), s[2] != 'd') {
    if (s[0] == 'i') {
      scanf("%d", &k);
      Q.push(k);
    } else {
      printf("%d\n", Q.top());
      Q.pop();
    }
  }
  return 0;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <cassert>
#include <cstddef>
#include <functional>
#include <memory>
#include <type_traits>
#include <utility>

template <class TotallyOrderedSet> class pairing_heap {

public:
  using set_type = TotallyOrderedSet;
  using value_type = typename set_type::value_type;
  using const_reference = const value_type &;
  using size_type = std::size_t;

private:
  struct node_t {
    value_type value;
    node_t *left, *right;
    template <class... Args>
    node_t(Args &&...args) noexcept(
        std::is_nothrow_constructible<value_type, Args...>::value)
        : value(std::forward<Args>(args)...), left(nullptr), right(nullptr) {}
  };
  using pointer = node_t *;
  pointer root;
  size_type s;
  static pointer merge(pointer x, pointer y) noexcept(
      noexcept(set_type::less_equal(std::declval<value_type>(),
                                    std::declval<value_type>()))) {
    if (!set_type::less_equal(x->value, y->value))
      std::swap(x, y);
    y->right = x->left;
    x->left = y;
    return x;
  }
  static pointer mergelist(pointer curr) noexcept(
      noexcept(merge(std::declval<pointer>(), std::declval<pointer>()))) {
    pointer head(nullptr), temp(nullptr), next(nullptr);
    while (curr) {
      next = (curr->right);
      if (next) {
        temp = (next->right);
        curr = merge((curr), (next));
      } else
        temp = nullptr;
      curr->right = (head);
      head = (curr);
      curr = (temp);
    }
    if (head) {
      curr = (head);
      head = (curr->right);
    }
    while (head) {
      next = (head->right);
      curr = merge((curr), (head));
      head = (next);
    }
    if (curr)
      curr->right = nullptr;
    return (curr);
  }

public:
  pairing_heap() noexcept : root(), s(0) {}

  bool empty() const noexcept { return !root; }
  size_type size() const noexcept { return s; }

  const_reference top() const noexcept {
    assert(!empty());
    return root->value;
  }

  void push(const value_type &x) {
    if (empty())
      root = new node_t(x);
    else
      root = merge((root), new node_t(x));
    ++s;
  }
  void push(value_type &&x) {
    if (empty())
      root = new node_t((x));
    else
      root = merge((root), new node_t((x)));
    ++s;
  }
  template <class... Args> void emplace(Args &&...args) {
    if (empty())
      root = new node_t(std::forward<Args>(args)...);
    else
      root = merge((root), new node_t(std::forward<Args>(args)...));
    ++s;
  }

  void pop() noexcept(noexcept(mergelist(std::declval<pointer>()))) {
    assert(!empty());
    root = mergelist((root->left));
    --s;
  }

  pairing_heap &meld(pairing_heap &other) noexcept(
      noexcept(merge(std::declval<pointer>(), std::declval<pointer>()))) {
    if (!other.empty()) {
      s += other.s;
      other.s = 0;
      root = merge((root), (other.root));
    }
    return *this;
  }
  pairing_heap &operator+=(pairing_heap &other) noexcept(
      noexcept(meld(std::declval<pairing_heap &>()))) {
    return meld(other);
  }
};
#include <algorithm>
#include <limits>
#include <utility>

template <class T> class ordered_set {
public:
  using value_type = T;

  static bool less_equal(const value_type &x, const value_type &y) noexcept(
      noexcept(std::declval<value_type>() >= std::declval<value_type>())) {
    return x >= y;
  }
};

#include <cstdio>
#include <string>
void scan(std::string &s) {
  s.clear();
  int c = fgetc(stdin);
  while (c == ' ' || c == '\n')
    c = fgetc(stdin);
  while (c != ' ' && c != '\n') {
    s.push_back(c);
    c = fgetc(stdin);
  }
}

int main() {
  pairing_heap<ordered_set<int>> Q;
  std::string s;
  int k;
  while (scan(s), s[2] != 'd') {
    if (s[0] == 'i') {
      scanf("%d", &k);
      Q.push(k);
    } else {
      printf("%d\n", Q.top());
      Q.pop();
    }
  }
  return 0;
}