Problem p02701 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <array>
#include <random>
#include <unistd.h>
#include <vector>

struct IO {
  static const int bufsize = 1 << 25;
  char ibuf[bufsize], obuf[bufsize];
  char *ip, *op;
  IO() : ip(ibuf), op(obuf) {
    for (int t = 0, k = 0;
         (k = read(STDIN_FILENO, ibuf + t, sizeof(ibuf) - t)) > 0; t += k)
      ;
  }
  ~IO() {
    for (int t = 0, k = 0;
         (k = write(STDOUT_FILENO, obuf + t, op - obuf - t)) > 0; t += k)
      ;
  }
  long long scan_int() {
    long long x = 0;
    bool neg = false;
    for (; *ip < '+'; ip++)
      ;
    if (*ip == '-') {
      neg = true;
      ip++;
    } else if (*ip == '+')
      ip++;
    for (; *ip >= '0'; ip++)
      x = 10 * x + *ip - '0';
    if (neg)
      x = -x;
    return x;
  }
  void put_int(long long x, char c = 0) {
    static char tmp[20];
    if (x == 0)
      *op++ = '0';
    else {
      int i;
      if (x < 0) {
        *op++ = '-';
        x = -x;
      }
      for (i = 0; x; i++) {
        tmp[i] = x % 10;
        x /= 10;
      }
      for (i--; i >= 0; i--)
        *op++ = tmp[i] + '0';
    }
    if (c)
      *op++ = c;
  }
  void put_double(double x, char c = 0) {
    unsigned y;
    const int mask = (1 << 24) - 1;
    put_int(x);
    *op++ = '.';
    x = x - (int)x;
    if (x < 0)
      x = -x;
    y = x * (1 << 24);

    for (int i = 0; i < 7; i++) {
      y *= 10;
      *op++ = '0' + (y >> 24);
      y &= mask;
    }
    if (c)
      *op++ = c;
  }
  inline char scan_char() { return *ip++; }
  inline void put_char(char c) { *op++ = c; }
  inline char *scan_string() {
    char *s = ip;
    while (*ip != '\\n' && *ip != ' ')
      ip++;
    *ip++ = '\\0';
    return s;
  }
  inline void put_string(const char *s, char c = 0) {
    while (*s)
      *op++ = *s++;
    if (c)
      *op++ = c;
  }
} io;

using u8 = unsigned char;
using u32 = unsigned int;
using u64 = unsigned long long int;

template <typename T, typename Hash, int K = 3> class hash_set {
  int n;
  std::vector<u64> s;
  std::vector<T> e;
  std::array<Hash, K> hashes;

  void reallocate() {
    ++n;
    std::vector<u64> t(1 << (n - 6));
    for (auto x : e) {
      for (int i = 0; i < K; ++i) {
        u32 j = hashes[i](x, n);
        t[j / 64] |= 1ULL << (j % 64);
      }
    }
    s = std::move(t);
  }

public:
  hash_set() : n(10), s(1 << (n - 6)) {}

  bool test(const T &a) const {
    for (int i = 0; i < K; ++i) {
      u32 j = hashes[i](a, n);
      if (!(s[j / 64] & (1ULL << (j % 64))))
        return false;
    }
    return true;
  }

  void set(const T &a) {
    if (test(a))
      return;
    if (32 * e.size() > (1U << n))
      reallocate();
    for (int i = 0; i < K; ++i) {
      u32 j = hashes[i](a, n);
      s[j / 64] |= 1ULL << (j % 64);
    }
    e.push_back(a);
  }

  u32 size() const { return e.size(); }
};

struct hash_u64 {
  /*
    static constexpr u64 r[] = { 12218524806659064575ULL,
    7972127133017346707ULL, 6268032797154397753ULL }; static int i; int ind;
  */
  u64 r;
  hash_u64() {
    //    ind = i++;
    std::random_device rd;
    r = ((u64)rd() << 32) | rd();
  }
  u32 operator()(u64 a, int n) const {
    //    return a * r[ind] >> (64 - n);
    return a * r >> (64 - n);
  }
};

// int hash_u64::i = 0;

int main() {
  int n = io.scan_int();
  hash_set<u64, hash_u64, 10> h;
  io.scan_string();
  for (int i = 0; i < n; i++) {
    u64 x = 0;
    for (char *s = io.scan_string(); *s; ++s)
      x <<= 5, x ^= *s - 'a' + 1;
    h.set(x);
    //  io.put_int(x, '\\n');
  }
  io.put_int(h.size(), '\\n');
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <random>
#include <unistd.h>
#include <vector>

struct IO {
  static const int bufsize = 1 << 25;
  char ibuf[bufsize], obuf[bufsize];
  char *ip, *op;
  IO() : ip(ibuf), op(obuf) {
    for (int t = 0, k = 0;
         (k = read(STDIN_FILENO, ibuf + t, sizeof(ibuf) - t)) > 0; t += k)
      ;
  }
  ~IO() {
    for (int t = 0, k = 0;
         (k = write(STDOUT_FILENO, obuf + t, op - obuf - t)) > 0; t += k)
      ;
  }
  long long scan_int() {
    long long x = 0;
    bool neg = false;
    for (; *ip < '+'; ip++)
      ;
    if (*ip == '-') {
      neg = true;
      ip++;
    } else if (*ip == '+')
      ip++;
    for (; *ip >= '0'; ip++)
      x = 10 * x + *ip - '0';
    if (neg)
      x = -x;
    return x;
  }
  void put_int(long long x, char c = 0) {
    static char tmp[20];
    if (x == 0)
      *op++ = '0';
    else {
      int i;
      if (x < 0) {
        *op++ = '-';
        x = -x;
      }
      for (i = 0; x; i++) {
        tmp[i] = x % 10;
        x /= 10;
      }
      for (i--; i >= 0; i--)
        *op++ = tmp[i] + '0';
    }
    if (c)
      *op++ = c;
  }
  void put_double(double x, char c = 0) {
    unsigned y;
    const int mask = (1 << 24) - 1;
    put_int(x);
    *op++ = '.';
    x = x - (int)x;
    if (x < 0)
      x = -x;
    y = x * (1 << 24);

    for (int i = 0; i < 7; i++) {
      y *= 10;
      *op++ = '0' + (y >> 24);
      y &= mask;
    }
    if (c)
      *op++ = c;
  }
  inline char scan_char() { return *ip++; }
  inline void put_char(char c) { *op++ = c; }
  inline char *scan_string() {
    char *s = ip;
    while (*ip != '\\n' && *ip != ' ')
      ip++;
    *ip++ = '\\0';
    return s;
  }
  inline void put_string(const char *s, char c = 0) {
    while (*s)
      *op++ = *s++;
    if (c)
      *op++ = c;
  }
} io;

using u8 = unsigned char;
using u32 = unsigned int;
using u64 = unsigned long long int;

template <typename T, typename Hash, int K = 3> class hash_set {
  int n;
  std::vector<u64> s;
  std::vector<T> e;
  std::array<Hash, K> hashes;

  void reallocate() {
    ++n;
    std::vector<u64> t(1 << (n - 6));
    for (auto x : e) {
      for (int i = 0; i < K; ++i) {
        u32 j = hashes[i](x, n);
        s[j / 64] |= 1ULL << (j % 64);
      }
    }
    s = std::move(t);
  }

public:
  hash_set() : n(10), s(1 << (n - 6)) {}

  bool test(const T &a) const {
    for (int i = 0; i < K; ++i) {
      u32 j = hashes[i](a, n);
      if (!(s[j / 64] & (1ULL << (j % 64))))
        return false;
    }
    return true;
  }

  void set(const T &a) {
    if (10 * e.size() > (1U << n))
      reallocate();
    bool b = false;
    for (int i = 0; i < K; ++i) {
      u32 j = hashes[i](a, n);
      if (!(s[j / 64] & (1ULL << (j % 64))))
        s[j / 64] |= 1ULL << (j % 64), b = true;
    }
    if (b)
      e.push_back(a);
  }

  u32 size() const { return e.size(); }
};

struct hash_u64 {
  u64 r;
  hash_u64() {
    std::random_device rd;
    r = ((u64)rd() << 32) | rd();
  }
  u32 operator()(u64 a, int n) const { return a * r >> (64 - n); }
};

int main() {
  int n = io.scan_int();
  hash_set<u64, hash_u64> h;
  io.scan_string();
  for (int i = 0; i < n; i++) {
    u64 x = 0;
    for (char *s = io.scan_string(); *s; ++s)
      x <<= 5, x ^= *s - 'a' + 1;
    h.set(x);
    //  io.put_int(x, '\\n');
  }
  io.put_int(h.size(), '\\n');
  return 0;
}