Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i, n) for (auto &i : n)

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
void init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

#ifdef DEBUG
template <class T, class N> void verr(const T &a, const N &n) {
  rep(i, n) cerr << a[i] << " ";
  cerr << "\\n" << flush;
}
ll dbgt = 1;
void err() { cerr << "passed " << dbgt++ << "\\n" << flush; }
template <class H, class... T> void err(H &&h, T &&...t) {
  cerr << h << (sizeof...(t) ? " " : "\\n") << flush;
  if (sizeof...(t) > 0)
    err(forward<T>(t)...);
}
#endif

const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
using ull = unsigned long long;
struct RollingHash {
  const ull mod = (1ll << 61) - 1, mask30 = (1ll << 30) - 1,
            mask31 = (1ll << 31) - 1;
  const ull base = 1009, positiviser = mod * 5;
  ull sn;
  vector<ull> hash, power;

  RollingHash() {}
  RollingHash(const string &s) {
    sn = s.size();
    hash.assign(sn + 1, 0);
    power.assign(sn + 1, 0);
    power[0] = 1;
    for (int i = 0; i < sn; i++) {
      power[i + 1] = calc_mod(mul(power[i], base));
      hash[i + 1] = calc_mod(mul(hash[i], base) + s[i]);
    }
  }

  inline ull mul(ull a, ull b) {
    ull au = a >> 31, ad = a & mask31;
    ull bu = b >> 31, bd = b & mask31;
    ull mid = ad * bu + au * bd;

    return ((au * bu) << 1) + ad * bd + ((mid & mask30) << 31) + (mid >> 30);
  }

  inline ull calc_mod(ull a) {
    a = (a & mod) + (a >> 61);
    if (a >= mod)
      a -= mod;
    return a;
  }

  // S[b1:b1+len), S[b2:b2+len)が同じか判定
  inline bool match(int begin1, int begin2, int len) {
    ull h1 = calc_mod(hash[begin1 + len] + positiviser -
                      mul(hash[begin1], power[len]));
    ull h2 = calc_mod(hash[begin2 + len] + positiviser -
                      mul(hash[begin2], power[len]));

    return h1 == h2;
  }
  inline bool match(ull h1, ull h2) { return h1 == h2; }

  // S[b:b+len)のハッシュを返す
  inline ull get(int begin, int len) {
    return calc_mod(hash[begin + len] + positiviser -
                    mul(hash[begin], power[len]));
  }
};

int main() {
  init();
  ll N;
  cin >> N;
  vc<string> S(N), RS(N);
  vc<ll> SN(N);
  rep(i, N) cin >> S[i];

  rep(i, N) SN[i] = S[i].size();
  RS = S;
  rep(i, N) reverse(all(RS[i]));

  vvc<bool> A(N, vc<bool>(26));
  map<pair<ull, ll>, ll> mp;

  vc<bool> U(1000005);

  vc<RollingHash> vrh;
  set<ull> st;
  rep(i, N) {
    RollingHash rh(RS[i]);
    vrh.emplace_back(rh);
    U[SN[i] - 1] = true;
    st.emplace(rh.get(0, SN[i] - 1));
  }

  rep(i, N) {
    RollingHash *rh = &vrh[i];
    rep(j, SN[i]) {
      A[i][S[i][j] - 'a'] = true;
      rep(k, 26) {
        if (A[i][k] and U[SN[i] - j - 1] and
            st.find(rh->get(0, SN[i] - j - 1)) != st.end())
          mp[{rh->get(0, SN[i] - j - 1), k}]++;
      }
    }
  }

  ll ans = 0;
  rep(i, N) {
    RollingHash *rh = &vrh[i];
    ll t = mp[{rh->get(0, SN[i] - 1), S[i][0] - 'a'}];
    ans += t - 1;
    // err(i, t);
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template <class T> using vc = vector<T>;
template <class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i, n) for (auto &i : n)

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
void init() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

#ifdef DEBUG
template <class T, class N> void verr(const T &a, const N &n) {
  rep(i, n) cerr << a[i] << " ";
  cerr << "\\n" << flush;
}
ll dbgt = 1;
void err() { cerr << "passed " << dbgt++ << "\\n" << flush; }
template <class H, class... T> void err(H &&h, T &&...t) {
  cerr << h << (sizeof...(t) ? " " : "\\n") << flush;
  if (sizeof...(t) > 0)
    err(forward<T>(t)...);
}
#endif

const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
using ull = unsigned long long;
struct RollingHash {
  const ull mod = (1ll << 61) - 1, mask30 = (1ll << 30) - 1,
            mask31 = (1ll << 31) - 1;
  const ull base = 1009, positiviser = mod * 5;
  ull sn;
  vector<ull> hash, power;

  RollingHash() {}
  RollingHash(const string &s) {
    sn = s.size();
    hash.assign(sn + 1, 0);
    power.assign(sn + 1, 0);
    power[0] = 1;
    for (int i = 0; i < sn; i++) {
      power[i + 1] = calc_mod(mul(power[i], base));
      hash[i + 1] = calc_mod(mul(hash[i], base) + s[i]);
    }
  }

  inline ull mul(ull a, ull b) {
    ull au = a >> 31, ad = a & mask31;
    ull bu = b >> 31, bd = b & mask31;
    ull mid = ad * bu + au * bd;

    return ((au * bu) << 1) + ad * bd + ((mid & mask30) << 31) + (mid >> 30);
  }

  inline ull calc_mod(ull a) {
    a = (a & mod) + (a >> 61);
    if (a >= mod)
      a -= mod;
    return a;
  }

  // S[b1:b1+len), S[b2:b2+len)が同じか判定
  inline bool match(int begin1, int begin2, int len) {
    ull h1 = calc_mod(hash[begin1 + len] + positiviser -
                      mul(hash[begin1], power[len]));
    ull h2 = calc_mod(hash[begin2 + len] + positiviser -
                      mul(hash[begin2], power[len]));

    return h1 == h2;
  }
  inline bool match(ull h1, ull h2) { return h1 == h2; }

  // S[b:b+len)のハッシュを返す
  inline ull get(int begin, int len) {
    return calc_mod(hash[begin + len] + positiviser -
                    mul(hash[begin], power[len]));
  }
};

int main() {
  init();
  ll N;
  cin >> N;
  vc<string> S(N), RS(N);
  vc<ll> SN(N);
  rep(i, N) cin >> S[i];

  rep(i, N) SN[i] = S[i].size();
  RS = S;
  rep(i, N) reverse(all(RS[i]));

  vvc<bool> A(N, vc<bool>(26));
  map<pair<ull, ll>, ll> mp;

  vc<bool> U(1000005);

  vc<RollingHash> vrh;
  rep(i, N) {
    RollingHash rh(RS[i]);
    vrh.emplace_back(rh);
    U[SN[i] - 1] = true;
  }

  rep(i, N) {
    RollingHash *rh = &vrh[i];
    rep(j, SN[i]) {
      A[i][S[i][j] - 'a'] = true;
      rep(k, 26) {
        if (A[i][k] and U[SN[i] - j - 1])
          mp[{rh->get(0, SN[i] - j - 1), k}]++;
      }
    }
  }

  ll ans = 0;
  rep(i, N) {
    RollingHash *rh = &vrh[i];
    ll t = mp[{rh->get(0, SN[i] - 1), S[i][0] - 'a'}];
    ans += t - 1;
    // err(i, t);
  }

  cout << ans << endl;
}