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

/**
 * @brief atcoder
 * @author yao
 */
#include <algorithm>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <utility>
#define ft first
#define sd second
#ifdef DBG
#define dbg_pri(x...) fprintf(stderr, x)
#else
#define dbg_pri(x...) 0
#define NDEBUG
#endif // DBG
#include <cassert>

typedef unsigned int uint;
typedef long long int lli;
typedef unsigned long long int ulli;

#define N 262144
#define M 1048576
#define P 0xe6006a2cf4c46aedull
#define Q 0xa53f309b5c65e11dull

char buf[N + M];
char *ss[N];
int nss[N];
ulli lu[M * 26];
int nlu;
ulli hashs[N];
int cnt[128];

int main() {
  int n;
  scanf("%d", &n);
  char *p = buf;
  for (int i = 0; i < n; ++i) {
    scanf("%s", p), nss[i] = strlen(p);
    ss[i] = p, p += nss[i] + 1;
  }
  for (int i = 0; i < n; ++i)
    dbg_pri("poi 3: %s\\n", ss[i]);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < nss[i]; ++j)
      ++cnt[ss[i][j]];
    ulli x = 0;
    int tn = nlu;
    for (int j = nss[i] - 1; j >= 0; --j) {
      x = (unsigned __int128)x * Q % P;
      for (int k = 'a'; k <= 'z'; ++k)
        if (cnt[k]) {
          lu[tn++] = x ^ k;
          dbg_pri("poi 1: %c %s %016llx\\n", (char)k, ss[i] + j + 1, x ^ k);
        }
      x ^= ss[i][j];
      --cnt[ss[i][j]];
    }
    hashs[i] = x;
    dbg_pri("poi 2: %s %016llx\\n", ss[i], x);
    std::sort(lu + nlu, lu + tn);
    nlu = std::unique(lu + nlu, lu + tn) - lu;
  }
  std::sort(lu, lu + nlu);
  lli ans = 0;
  for (int i = 0; i < n; ++i)
    ans += std::upper_bound(lu, lu + nlu, hashs[i]) -
           std::lower_bound(lu, lu + nlu, hashs[i]);
  ans -= n;
  printf("%lld\\n", ans);
  return 0;
}


Error: -11

Fixed submission:

/**
 * @brief atcoder
 * @author yao
 */
#include <algorithm>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <utility>
#define ft first
#define sd second
#ifdef DBG
#define dbg_pri(x...) fprintf(stderr, x)
#else
#define dbg_pri(x...) 0
#define NDEBUG
#endif // DBG
#include <cassert>

typedef unsigned int uint;
typedef long long int lli;
typedef unsigned long long int ulli;

#define N 1048576
#define P 0xe6006a2cf4c46aedull
#define Q 0xa53f309b5c65e11dull

char buf[N];
char *ss[N];
int nss[N];
ulli lu[N * 26];
int nlu;
ulli hashs[N];
int cnt[128];

int main() {
  int n;
  scanf("%d", &n);
  char *p = buf;
  for (int i = 0; i < n; ++i) {
    scanf("%s", p), nss[i] = strlen(p);
    ss[i] = p, p += nss[i] + 1;
  }
  for (int i = 0; i < n; ++i)
    dbg_pri("poi 3: %s\\n", ss[i]);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < nss[i]; ++j)
      ++cnt[ss[i][j]];
    ulli x = 0;
    int tn = nlu;
    for (int j = nss[i] - 1; j >= 0; --j) {
      x = (unsigned __int128)x * Q % P;
      for (int k = 'a'; k <= 'z'; ++k)
        if (cnt[k]) {
          lu[tn++] = x ^ k;
          dbg_pri("poi 1: %c %s %016llx\\n", (char)k, ss[i] + j + 1, x ^ k);
        }
      x ^= ss[i][j];
      --cnt[ss[i][j]];
    }
    hashs[i] = x;
    dbg_pri("poi 2: %s %016llx\\n", ss[i], x);
    std::sort(lu + nlu, lu + tn);
    nlu = std::unique(lu + nlu, lu + tn) - lu;
  }
  std::sort(lu, lu + nlu);
  lli ans = 0;
  for (int i = 0; i < n; ++i)
    ans += std::upper_bound(lu, lu + nlu, hashs[i]) -
           std::lower_bound(lu, lu + nlu, hashs[i]);
  ans -= n;
  printf("%lld\\n", ans);
  return 0;
}