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

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

using Pi = pair<int, int>;
using Ti = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

using ull = unsigned long long;
struct RollingHash {
  string s;
  ull base, mod;
  vector<ull> hashed, power;
  RollingHash(const string &s) : s(s), base(9973ll), mod(999999937ll) {
    int sz = s.size();
    hashed.resize(sz + 1, 0);
    power.resize(sz + 1, 0);
    power[0] = 1;
    for (int i = 0; i < sz; i++) {
      power[i + 1] = power[i] * base % mod;
      hashed[i + 1] = (hashed[i] * base % mod + s[i]) % mod;
    }
  }
  ull get(int l, int r) {
    return (hashed[r] - hashed[l] * power[r - l] % mod + mod) % mod;
  }
  ull concat(ull h1, ull h2, int len) {
    return (h1 * power[len] % mod + h2) % mod;
  }
  ull rehash(int l, int r, int i, char c) {
    if (i < l || r <= i)
      return get(l, r);
    // cout << r-l << " " << s.substr(l, r-l) << " " << s.substr(l, i-l) <<
    // endl;
    ull h = get(l, i);
    // cout << l << " " << r << " " << i << " " << c <<  " " << h << endl;
    h = (h * base % mod + c) % mod;
    return concat(h, get(i + 1, r), r - (i + 1));
  }
};

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  string s;
  cin >> s;
  RollingHash rh(s);

  int q;
  cin >> q;
  while (q--) {
    int l, r, t;
    cin >> l >> r >> t;
    --l;
    if (rh.get(l, r - t) == rh.get(l + t, r)) {
      cout << "Yes" << endl;
      continue;
    }
    int lb = 0, ub = r - l + 1;
    while (lb + 1 < ub) {
      int mb = (lb + ub) / 2;
      // assert(l+t+mb <= s.size());
      if (l + t + mb <= (int)s.size() &&
          rh.get(l, l + mb) == rh.get(l + t, l + t + mb))
        lb = mb;
      else
        ub = mb;
    }
    // cout << rh.get(l, l+lb) << " " << rh.get(l+t, l+t+lb) << endl;
    // cout << lb << " " << s[l+lb] << " " << s[l+t+lb] << endl;
    // cout << rh.rehash(l, r-t, l+lb, s[l+lb]) << " " << rh.get(l, r-t) <<
    // endl; cout << rh.rehash(l+t, r, l+t+lb, s[l+lb]) <<  " " << rh.get(l+t,
    // r) << endl;
    if ((rh.rehash(l, r - t, l + lb, s[l + t + lb]) ==
         rh.rehash(l + t, r, l + lb, s[l + t + lb])) ||
        (rh.rehash(l, r - t, l + t + lb, s[l + lb]) ==
         rh.rehash(l + t, r, l + t + lb, s[l + lb]))) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template <class T1, class T2> void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <class T1, class T2> void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

using Pi = pair<int, int>;
using Ti = tuple<int, int, int>;
using vint = vector<int>;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

using ull = unsigned long long;
struct RollingHash {
  // string s;
  ull base, mod;
  vector<ull> hashed, power;
  RollingHash(const string &s) : /*s(s),*/ base(9973ll), mod(999999937ll) {
    int sz = s.size();
    hashed.resize(sz + 1, 0);
    power.resize(sz + 1, 0);
    power[0] = 1;
    for (int i = 0; i < sz; i++) {
      power[i + 1] = power[i] * base % mod;
      hashed[i + 1] = (hashed[i] * base % mod + s[i]) % mod;
    }
  }
  ull get(int l, int r) {
    return (hashed[r] - hashed[l] * power[r - l] % mod + mod) % mod;
  }
  ull concat(ull h1, ull h2, int len) {
    return (h1 * power[len] % mod + h2) % mod;
  }
  ull rehash(int l, int r, int i, char c) {
    if (i < l || r <= i)
      return get(l, r);
    // cout << r-l << " " << s.substr(l, r-l) << " " << s.substr(l, i-l) <<
    // endl;
    ull h = get(l, i);
    // cout << l << " " << r << " " << i << " " << c <<  " " << h << endl;
    h = (h * base % mod + c) % mod;
    return concat(h, get(i + 1, r), r - (i + 1));
  }
};

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  string s;
  cin >> s;
  RollingHash rh(s);

  int q;
  cin >> q;
  while (q--) {
    int l, r, t;
    cin >> l >> r >> t;
    --l;
    if (rh.get(l, r - t) == rh.get(l + t, r)) {
      cout << "Yes" << endl;
      continue;
    }
    int lb = 0, ub = r - l + 1;
    while (lb + 1 < ub) {
      int mb = (lb + ub) / 2;
      if (rh.get(l, l + mb) == rh.get(l + t, l + t + mb))
        lb = mb;
      else
        ub = mb;
    }
    // cout << rh.get(l, l+lb) << " " << rh.get(l+t, l+t+lb) << endl;
    // cout << lb << " " << s[l+lb] << " " << s[l+t+lb] << endl;
    // cout << rh.rehash(l, r-t, l+lb, s[l+lb]) << " " << rh.get(l, r-t) <<
    // endl; cout << rh.rehash(l+t, r, l+t+lb, s[l+lb]) <<  " " << rh.get(l+t,
    // r) << endl;
    if ((rh.rehash(l, r - t, l + lb, s[l + t + lb]) ==
         rh.rehash(l + t, r, l + lb, s[l + t + lb])) ||
        (rh.rehash(l, r - t, l + t + lb, s[l + lb]) ==
         rh.rehash(l + t, r, l + t + lb, s[l + lb]))) {
      cout << "Yes" << endl;
    } else {
      cout << "No" << endl;
    }
  }

  return 0;
}