Problem p02289 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

struct node {
  node *p, *l, *r;
  int cnt;
  void Init(node *p_) {
    p = p_;
    l = r = nullptr;
    cnt = 0;
  }
};

int it;
node pool[1000000];

node *new_node(node *p) {
  pool[it].Init(p);
  return &pool[it++];
}

class uint_Trie {
  node *root;
  int siz;

public:
  uint_Trie() : root(new_node(nullptr)) {}
  int size() { return root->cnt; }
  void insert(unsigned val) {
    // if (find(val)) return; // multiset ???????¶????
    node *t = root;
    for (int i = 30; i >= 0; i--) {
      t->cnt++;
      if (val & (1 << i)) {
        if (t->r == nullptr) {
          t->r = new_node(t);
        }
        t = t->r;
      } else {
        if (t->l == nullptr) {
          t->l = new_node(t);
        }
        t = t->l;
      }
    }
    t->cnt++;
  }
  void erase(unsigned val) {
    node *t = root, *ne;
    for (int i = 30; i >= 0; i--) {
      t->cnt--;
      if (val & (1 << i)) {
        if (t->r == nullptr)
          return;
        t = t->r;
      } else {
        if (t->l == nullptr)
          return;
        t = t->l;
      }
    }
    t->cnt--;
    while (t != root && t->cnt == 0) {
      ne = t->p;
      (t == ne->l ? ne->l : ne->r) = nullptr;
      t = ne;
    }
  }
  bool find(unsigned val) {
    node *t = root;
    for (int i = 30; i >= 0; i--) {
      if (val & (1 << i)) {
        if (t->r == nullptr)
          return false;
        t = t->r;
      } else {
        if (t->l == nullptr)
          return false;
        t = t->l;
      }
    }
    return true;
  }
  unsigned max() {
    assert(root->cnt >= 1);
    node *t = root;
    unsigned res = 0;
    for (int i = 30; i >= 0; i--) {
      assert(t->l != nullptr || t->r != nullptr);
      if (t->r != nullptr) {
        res |= 1 << i;
        t = t->r;
      } else {
        t = t->l;
      }
    }
    return res;
  }
  unsigned min() {
    assert(root->cnt >= 1);
    node *t = root;
    unsigned res = 0;
    for (int i = 30; i >= 0; i--) {
      assert(t->l != nullptr || t->r != nullptr);
      if (t->l != nullptr) {
        t = t->l;
      } else {
        res |= 1 << i;
        t = t->r;
      }
    }
    return res;
  }
};

int main() {
  cin.sync_with_stdio(false);
  uint_Trie ut;
  string com;
  int k;
  while (cin >> com, com != "end") {
    if (com == "insert") {
      cin >> k;
      ut.insert(k);
    } else {
      int res = ut.max();
      printf("%d\n", res);
      ut.erase(res);
    }
  }
  return 0;
}

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

struct node {
  node *p, *l, *r;
  int cnt;
  void Init(node *p_) {
    p = p_;
    l = r = nullptr;
    cnt = 0;
  }
};

int it;
node pool[2000000];

node *new_node(node *p) {
  pool[it].Init(p);
  return &pool[it++];
}

class uint_Trie {
  node *root;
  int siz;

public:
  uint_Trie() : root(new_node(nullptr)) {}
  int size() { return root->cnt; }
  void insert(unsigned val) {
    // if (find(val)) return; // multiset ???????¶????
    node *t = root;
    for (int i = 30; i >= 0; i--) {
      t->cnt++;
      if (val & (1 << i)) {
        if (t->r == nullptr) {
          t->r = new_node(t);
        }
        t = t->r;
      } else {
        if (t->l == nullptr) {
          t->l = new_node(t);
        }
        t = t->l;
      }
    }
    t->cnt++;
  }
  void erase(unsigned val) {
    node *t = root, *ne;
    for (int i = 30; i >= 0; i--) {
      t->cnt--;
      if (val & (1 << i)) {
        if (t->r == nullptr)
          return;
        t = t->r;
      } else {
        if (t->l == nullptr)
          return;
        t = t->l;
      }
    }
    t->cnt--;
    while (t != root && t->cnt == 0) {
      ne = t->p;
      (t == ne->l ? ne->l : ne->r) = nullptr;
      t = ne;
    }
  }
  bool find(unsigned val) {
    node *t = root;
    for (int i = 30; i >= 0; i--) {
      if (val & (1 << i)) {
        if (t->r == nullptr)
          return false;
        t = t->r;
      } else {
        if (t->l == nullptr)
          return false;
        t = t->l;
      }
    }
    return true;
  }
  unsigned max() {
    assert(root->cnt >= 1);
    node *t = root;
    unsigned res = 0;
    for (int i = 30; i >= 0; i--) {
      assert(t->l != nullptr || t->r != nullptr);
      if (t->r != nullptr) {
        res |= 1 << i;
        t = t->r;
      } else {
        t = t->l;
      }
    }
    return res;
  }
  unsigned min() {
    assert(root->cnt >= 1);
    node *t = root;
    unsigned res = 0;
    for (int i = 30; i >= 0; i--) {
      assert(t->l != nullptr || t->r != nullptr);
      if (t->l != nullptr) {
        t = t->l;
      } else {
        res |= 1 << i;
        t = t->r;
      }
    }
    return res;
  }
};

int main() {
  cin.sync_with_stdio(false);
  uint_Trie ut;
  string com;
  int k;
  while (cin >> com, com != "end") {
    if (com == "insert") {
      cin >> k;
      ut.insert(k);
    } else {
      int res = ut.max();
      printf("%d\n", res);
      ut.erase(res);
    }
  }
  return 0;
}