Problem p02289 (C++)

Buggy submission (status: Time Limit Exceeded):
// #define NDEBUG
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

using int32 = std::int_fast32_t;
using int64 = std::int_fast64_t;
using uint32 = std::uint_fast32_t;
using uint64 = std::uint_fast64_t;
/*
二重連鎖木によるpairing heapの実装例
*/
template <class T, bool P = false> class pairing_heap {
  static constexpr uint64 ALLOCSIZE = (uint64)1 << 10;
  struct node_t {
    T data;
    node_t *left, *right;
  };
  node_t *root;
  static node_t *pool;
  static node_t *merge(node_t *x, node_t *y) {
    if (P ^ (x->data < y->data))
      std::swap(x, y);
    x->right = y->left;
    y->left = x;
    return y;
  }

public:
  pairing_heap() : root(nullptr) {}
  T pop() {
    assert(root);
    node_t *n = nullptr, *x = root->left, *y;
    root->right = pool;
    pool = root;
    while (x) {
      y = x;
      if (x->right) {
        x = x->right->right;
        y = merge(y, y->right);
      } else {
        x = nullptr;
      }
      y->right = n;
      n = y;
    }
    if (!n) {
      root = nullptr;
      return pool->data;
    }
    root = n;
    n = n->right;
    while (n) {
      x = n;
      n = n->right;
      root = merge(root, x);
    }
    return pool->data;
  }
  void push(const T &data) {
    if (!pool) {
      pool = (node_t *)malloc(ALLOCSIZE * sizeof(node_t));
      uint32 i = ALLOCSIZE;
      pool[i - 1].left = nullptr;
      while (--i) {
        pool[i - 1].left = pool + i;
      }
    }
    node_t *top = pool;
    pool = pool->left;
    top->data = data;
    top->left = nullptr;
    root = root ? merge(root, top) : top;
  }
  pairing_heap &operator+=(pairing_heap &other) {
    root = merge(root, other.root);
    other.root = nullptr;
    return *this;
  }
};
template <class T, bool P>
typename pairing_heap<T, P>::node_t *pairing_heap<T, P>::pool = nullptr;

template <typename T> void uscan(T &d) {
  d = 0;
  int c = 0;
  while (c < '0' || '9' < c)
    c = fgetc(stdin);
  while ('0' <= c && c <= '9') {
    d = (d << 3) + (d << 1) + c - '0';
    c = fgetc(stdin);
  }
}
void sscan(std::string &s) {
  s = std::string();
  int c = 0;
  while (c < 'a' || 'z' < c)
    c = fgetc(stdin);
  while ('a' <= c && c <= 'z') {
    s.push_back(c);
    c = fgetc(stdin);
  }
}

int main(void) {
  std::string s;
  uint32 k;
  pairing_heap<uint32, true> Q;
  sscan(s);
  while (s[2] != 'd') {
    if (s[0] == 'e') {
      printf("%u\n", Q.pop());
    } else {
      uscan(k);
      Q.push(k);
    }
    sscan(s);
  }
  return 0;
}


Fixed submission:
// #define NDEBUG
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <array>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>

using int32 = std::int_fast32_t;
using int64 = std::int_fast64_t;
using uint32 = std::uint_fast32_t;
using uint64 = std::uint_fast64_t;
/*
二重連鎖木によるpairing heapの実装例
*/
template <class T, bool P = false> class pairing_heap {
  static constexpr uint64 ALLOCSIZE = (uint64)1 << 10;
  struct node_t {
    T data;
    node_t *left, *right;
  };
  node_t *root;
  static node_t *pool;
  static node_t *merge(node_t *x, node_t *y) {
    if (P ^ (x->data < y->data))
      std::swap(x, y);
    x->right = y->left;
    y->left = x;
    return y;
  }

public:
  pairing_heap() : root(nullptr) {}
  T pop() {
    assert(root);
    node_t *n = nullptr, *x = root->left, *y;
    root->left = pool;
    pool = root;
    while (x) {
      y = x;
      if (x->right) {
        x = x->right->right;
        y = merge(y, y->right);
      } else {
        x = nullptr;
      }
      y->right = n;
      n = y;
    }
    if (!n) {
      root = nullptr;
      return pool->data;
    }
    root = n;
    n = n->right;
    while (n) {
      x = n;
      n = n->right;
      root = merge(root, x);
    }
    return pool->data;
  }
  void push(const T &data) {
    if (!pool) {
      pool = (node_t *)malloc(ALLOCSIZE * sizeof(node_t));
      uint32 i = ALLOCSIZE;
      pool[i - 1].left = nullptr;
      while (--i) {
        pool[i - 1].left = pool + i;
      }
    }
    node_t *top = pool;
    pool = pool->left;
    top->data = data;
    top->left = nullptr;
    root = root ? merge(root, top) : top;
  }
  pairing_heap &operator+=(pairing_heap &other) {
    root = merge(root, other.root);
    other.root = nullptr;
    return *this;
  }
};
template <class T, bool P>
typename pairing_heap<T, P>::node_t *pairing_heap<T, P>::pool = nullptr;

template <typename T> void uscan(T &d) {
  d = 0;
  int c = 0;
  while (c < '0' || '9' < c)
    c = fgetc(stdin);
  while ('0' <= c && c <= '9') {
    d = (d << 3) + (d << 1) + c - '0';
    c = fgetc(stdin);
  }
}
void sscan(std::string &s) {
  s = std::string();
  int c = 0;
  while (c < 'a' || 'z' < c)
    c = fgetc(stdin);
  while ('a' <= c && c <= 'z') {
    s.push_back(c);
    c = fgetc(stdin);
  }
}

int main(void) {
  std::string s;
  uint32 k;
  pairing_heap<uint32, true> Q;
  sscan(s);
  while (s[2] != 'd') {
    if (s[0] == 'e') {
      printf("%u\n", Q.pop());
    } else {
      uscan(k);
      Q.push(k);
    }
    sscan(s);
  }
  return 0;
}