Problem p02913 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define popcnt(x) __builtin_popcount(x)
#define inf 0x3f3f3f3f
#define watch(x) cout << (#x) << " is " << (x) << endl
#define rand() (rand() << 15 | rand())
using namespace std;
typedef long long ll;
#define EPS 1e-9
#define PI acos(-1.0)
const int MOD = 1e9 + 9;
const int N = 5e5 + 4;
const int p = 31;
const int m = 1e9 + 9;
int preH[N];
string s;
int n;
void compute_hash(string const &s) {
  long long hash_value = 0;
  long long p_pow = 1;
  int indx = 0;
  for (char c : s) {
    hash_value = (hash_value + (c - 'a' + 1) * p_pow) % m;
    p_pow = (p_pow * p) % m;
    preH[indx++] = hash_value;
  }
}

int power(int b, int p) {
  if (p == 0)
    return 1;
  int res = power(b, p / 2);
  res = 1LL * res * res % MOD;
  if (p & 1) {
    res = 1LL * b * res % MOD;
  }
  return res;
}

int modInv(int x) { return power(x, MOD - 2); }

int mul(int a, int b, int m) { return (1LL * (a % m) * (b % m)) % m; }

int add(int a, int b, int m) { return (1LL * (a % m) + (b % m)) % m; }

long long get_hash(int l, int r) {
  long long ret = (preH[r] - (l ? preH[l - 1] : 0) + MOD) % MOD;
  return mul(ret, modInv(power(p, l)), MOD);
}

bool valid(int sz) {
  unordered_map<int, bool> mp;
  for (int i = n - sz; i >= 0; --i) {
    int cur = get_hash(i, i + sz - 1);
    mp[cur] = true;
    int j = i - sz;
    if (j < 0)
      return false;
    int nxt = get_hash(j, j + sz - 1);
    if (mp.count(nxt)) {
      return true;
    }
  }
  return false;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  cin >> s;
  compute_hash(s);
  int l = 1, r = n, ans = 0;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (valid(mid)) {
      ans = mid, l = mid + 1;
    } else {
      r = mid - 1;
    }
  }

  cout << ans << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define popcnt(x) __builtin_popcount(x)
#define inf 0x3f3f3f3f
#define watch(x) cout << (#x) << " is " << (x) << endl
#define rand() (rand() << 15 | rand())
using namespace std;
typedef long long ll;
#define EPS 1e-9
#define PI acos(-1.0)
const int MOD = 1e9 + 9;
const int N = 5e5 + 4;
const int p = 31;
const int m = 1e9 + 9;
int preH[N];
string s;
int n;
void compute_hash(string const &s) {
  long long hash_value = 0;
  long long p_pow = 1;
  int indx = 0;
  for (char c : s) {
    hash_value = (hash_value + (c - 'a' + 1) * p_pow) % m;
    p_pow = (p_pow * p) % m;
    preH[indx++] = hash_value;
  }
}

int power(int b, int p) {
  if (p == 0)
    return 1;
  int res = power(b, p / 2);
  res = 1LL * res * res % MOD;
  if (p & 1) {
    res = 1LL * b * res % MOD;
  }
  return res;
}

int modInv(int x) { return power(x, MOD - 2); }

int mul(int a, int b, int m) { return (1LL * (a % m) * (b % m)) % m; }

int add(int a, int b, int m) { return (1LL * (a % m) + (b % m)) % m; }

long long get_hash(int l, int r) {
  long long ret = (preH[r] - (l ? preH[l - 1] : 0) + MOD) % MOD;
  return mul(ret, modInv(power(p, l)), MOD);
}

bool valid(int sz) {
  unordered_map<int, bool> mp;
  for (int i = n - sz; i >= 0; --i) {
    int cur = get_hash(i, i + sz - 1);
    mp[cur] = true;
    int j = i - sz;
    if (j < 0)
      return false;
    int nxt = get_hash(j, j + sz - 1);
    if (mp.count(nxt)) {
      return true;
    }
  }
  return false;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  cin >> s;
  compute_hash(s);
  int l = 1, r = n, ans = 0;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (valid(mid)) {
      ans = mid, l = mid + 1;
    } else {
      r = mid - 1;
    }
  }

  cout << ans << "\n";
}