alexjercan/bugnet huggingface dataset:
Problem p01944 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))

using ll = long long;

const ll mod = 1e9 + 7;

using vl = vector<ll>;
using mat = vector<ll>;

mat mul(const mat &a, const mat &b) {
  int n = a.size();
  mat c(n);

  rep(i, n) {
    c[i] = 0;
    rep(j, n) c[i] = (a[j] * b[(i - j + n) % n] + c[i]) % mod;
  }
  return c;
}

vl mul_v(const mat &a, const vl &b) {
  int n = a.size();
  vl ret(n);
  rep(i, n) {
    rep(j, n) {
      ret[i] += (a[(j + i) % n] * b[j]) % mod;
      ret[i] %= mod;
    }
  }

  return ret;
}

mat pow(const mat &a, int n) {
  int N = a.size();
  mat r(N);
  // ??????
  r[0] = 1;

  mat p(a);
  while (n) {
    if (n & 1)
      r = mul(r, p);
    p = mul(p, p);
    n >>= 1;
  }

  return r;
}

int main() {
  int n, m, k;
  scanf(" %d %d %d", &n, &m, &k);

  mat A(n);
  A[0] = 1;

  rep(i, m) {
    int p;
    scanf(" %d", &p);

    ++A[p % n];
  }

  mat P = pow(A, k);
  vl b(n);
  b[0] = 1;

  vl ans = mul_v(P, b);

  rep(i, n) printf("%lld\\n", ans[i]);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))

using ll = long long;

const ll mod = 1e9 + 7;

using vl = vector<ll>;
using mat = vector<vl>;

mat mul(const mat &a, const mat &b) {
  int n = a.size();
  mat c(n, vl(n));
  rep(i, n) rep(j, n) rep(k, n) {
    c[i][j] += (a[i][k] * b[k][j]) % mod;
    c[i][j] %= mod;
  }
  return c;
}

vl mul(const mat &a, const vl &b) {
  int n = a.size();
  vl ret(n);
  rep(i, n) {
    rep(j, n) {
      ret[i] += (a[i][j] * b[j]) % mod;
      ret[i] %= mod;
    }
  }

  return ret;
}

mat pow(const mat &a, int n) {
  int N = a.size();
  mat r(N, vl(N));
  rep(i, N) r[i][i] = 1;

  mat p(a);
  while (n) {
    if (n & 1)
      r = mul(r, p);
    p = mul(p, p);
    n >>= 1;
  }

  return r;
}

int main() {
  int n, m, k;
  scanf(" %d %d %d", &n, &m, &k);

  mat A(n, vl(n));
  rep(i, n) A[i][i] = 1;

  rep(i, m) {
    int p;
    scanf(" %d", &p);

    rep(j, n) {
      int idx = (j + p) % n;
      ++A[idx][j];
    }
  }

  mat P = pow(A, k);
  vl b(n);
  b[0] = 1;

  vl ans = mul(P, b);

  rep(i, n) printf("%lld\\n", ans[i]);
  return 0;
}
Problem p01944 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

typedef vector<ll> vec;
typedef vector<vec> mat;

mat mul(mat &A, mat &B) {
  mat C(A.size(), vec(B[0].size()));
  for (int i = 0; i < A.size(); i++) {
    for (int k = 0; k < B.size(); k++) {
      for (int j = 0; j < B[0].size(); j++) {
        C[i][j] = (C[i][j] + (A[i][k] * B[k][j] % MOD)) % MOD;
      }
    }
  }
  return C;
}

mat pow(mat A, ll n) {
  mat B(A.size(), vec(A.size()));
  for (int i = 0; i < A.size(); i++) {
    B[i][i] = 1;
  }
  while (n > 0) {
    if (n & 1)
      B = mul(B, A);
    A = mul(A, A);
    n >>= 1;
  }
  return B;
}

int n, m, K;
ll p[10001];
ll nec[801];
ll res[30][801];
ll ans[2][801];

int main(void) {
  scanf("%d%d%d", &n, &m, &K);
  mat B(n, vec(n));
  for (int i = 0; i < m; i++) {
    scanf("%d", &p[i]);
  }
  nec[0]++;
  for (int i = 0; i < m; i++) {
    int mo = p[i] % n;
    nec[mo]++;
  }
  for (int i = 0; i < 30; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        if (i > 0)
          res[i][(j + k) % n] += (res[i - 1][j] * res[i - 1][k]) % MOD;
        if (i == 0)
          res[i][j] = nec[j];
        if (res[i][(j + k) % n] >= MOD)
          res[i][(j + k) % n] %= MOD;
      }
    }
  }
  int prev = 0, now = 1;
  ans[0][0] = 1;
  for (int i = 0; i < 30; i++) {
    if (K >> i & 1) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          ans[now][(j + k) % n] += (ans[prev][j] * res[i][k]) % MOD;
          if (ans[now][(j + k) % n] >= MOD)
            ans[now][(j + k) % n] %= MOD;
        }
      }
      swap(now, prev);
      memset(ans[now], 0, sizeof(ans[now]));
    }
  }
  for (int i = 0; i < n; i++) {
    printf("%lld\\n", ans[prev][i]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

typedef vector<ll> vec;
typedef vector<vec> mat;

mat mul(mat &A, mat &B) {
  mat C(A.size(), vec(B[0].size()));
  for (int i = 0; i < A.size(); i++) {
    for (int k = 0; k < B.size(); k++) {
      for (int j = 0; j < B[0].size(); j++) {
        C[i][j] = (C[i][j] + (A[i][k] * B[k][j] % MOD)) % MOD;
      }
    }
  }
  return C;
}

mat pow(mat A, ll n) {
  mat B(A.size(), vec(A.size()));
  for (int i = 0; i < A.size(); i++) {
    B[i][i] = 1;
  }
  while (n > 0) {
    if (n & 1)
      B = mul(B, A);
    A = mul(A, A);
    n >>= 1;
  }
  return B;
}

int n, m, k;
ll p[10001];

int main(void) {
  scanf("%d%d%d", &n, &m, &k);
  mat B(n, vec(n));
  for (int i = 0; i < m; i++) {
    scanf("%d", &p[i]);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      B[i][j] = 0;
    }
    B[i][i] = 1;
  }
  for (int i = 0; i < m; i++) {
    int mo = p[i] % n;
    for (int j = 0; j < n; j++) {
      B[(j + mo) % n][j]++;
    }
  }
  B = pow(B, k);
  for (int i = 0; i < n; i++) {
    printf("%lld\\n", B[i][0]);
  }
  return 0;
}
Problem p01946 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll s, t, d, w[100000], c = 0, ans = 1e11;
  cin >> s >> t >> d;
  for (int i = 0; i < d; i++)
    cin >> w[i], c -= w[i];
  s -= t;
  for (ll i = 0; i < d; i++) {
    s += w[i];
    if (s <= 0)
      ans = min(ans, i + 1);
    else if (c > 0)
      ans = min(ans, i + (s + c - 1) / c * d + 1);
  }
  if (ans == 1e11)
    ans = -1;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  long long s, t, d, w[100000], c = 0;
  cin >> s >> t >> d;
  s -= t;
  for (int i = 0; i < d; i++)
    cin >> w[i];
  while (0 < s && c < 1e9)
    s += w[c++ % d];
  if (c == 1e9)
    c = -1;
  cout << c << endl;
  return 0;
}
Problem p01947 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
bool used[200000];
int ans[101][101], idx, s1, s2;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0}, n;
vector<int> v[20000];
int main() {
  cin >> n;
  r(i, n * n * 2 - 2 * n) {
    cin >> s1 >> s2;
    v[s1].push_back(s2);
    v[s2].push_back(s1);
  }
  r(i, n * n + 1) if (v[i].size() == 2) idx = i;
  ans[0][0] = idx;
  used[idx] = 1;
  r(i, n) r(j, n) if (!ans[i][j]) {
    vector<int> p;
    r(k, 4) {
      int y = i + dy[k];
      int x = j + dx[k];
      if (y < 0 || x < 0 || y >= n || x >= n)
        continue;
      p.push_back(ans[y][x]);
    }
    r(k, n * n + 1) if (v[k].size() == p.size()) {
      if (!used[k]) {
        int f = 0;
        r(l, p.size()) if (p[l]) {
          r(q, v[k].size()) {
            if (v[k][q] == p[l])
              break;
            if (q == v[k].size() - 1)
              f++;
          }
        }
        if (!f) {
          ans[i][j] = k;
          used[k] = 1;
          goto L;
        }
      }
    }
  L:;
  }
  r(i, n) {
    r(j, n) {
      if (j)
        cout << ' ';
      cout << ans[i][j];
    }
    cout << endl;
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
bool used[100000];
int ans[101][101], idx, s1, s2;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0}, n;
vector<int> v[1000];
int main() {
  cin >> n;
  r(i, n * n * 2 - 2 * n) {
    cin >> s1 >> s2;
    v[s1].push_back(s2);
    v[s2].push_back(s1);
  }
  r(i, n * n + 1) if (v[i].size() == 2) idx = i;
  ans[0][0] = idx;
  used[idx] = 1;
  r(i, n) r(j, n) if (!ans[i][j]) {
    vector<int> p;
    r(k, 4) {
      int y = i + dy[k];
      int x = j + dx[k];
      if (y < 0 || x < 0 || y >= n || x >= n)
        continue;
      p.push_back(ans[y][x]);
    }
    r(k, n * n + 1) if (v[k].size() == p.size()) {
      if (!used[k]) {
        int f = 0;
        r(l, p.size()) if (p[l]) {
          r(q, v[k].size()) {
            if (v[k][q] == p[l])
              break;
            if (q == v[k].size() - 1)
              f++;
          }
        }
        if (!f) {
          ans[i][j] = k;
          used[k] = 1;
          goto L;
        }
      }
    }
  L:;
  }
  r(i, n) {
    r(j, n) {
      if (j)
        cout << ' ';
      cout << ans[i][j];
    }
    cout << endl;
  }
}
Problem p01956 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int n, w, h;
int x[101];
int xi[101];

int main(void) {
  scanf("%d%d%d", &n, &h, &w);
  for (int i = 0; i < n; i++) {
    scanf("%d", &x[i]);
  }
  for (int i = 0; i < n; i++) {
    xi[i] = i * w;
    if (i % 2 == 0) {
      xi[i] += x[i];
    } else {
      xi[i] -= x[i];
    }
  }
  int ans = 0;
  xi[n] = (n * w);
  sort(xi, xi + n);
  for (int i = 0; i < n; i++) {
    ans += max(0, xi[i + 1] - xi[i] - w);
  }
  ans += xi[0];
  printf("%d\\n", ans * h);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n, c;

struct BITree {
  ll bit[100005];
  BITree() {}
  void update(int k, ll a) {
    while (k <= 100000) {
      bit[k] += a;
      k += (k & (-k));
    }
  }

  ll sum(int k) {
    ll s = 0;
    while (k > 0) {
      s += bit[k];
      k -= (k & (-k));
    }
    return s;
  }

  ll query(int a, int b) { return sum(b) - sum(a); }
};

BITree seg, seg2;

int b[100005], w[100005];
P vec_l[100005], vec_r[100005];
P vec[100005];

ll solve(int l, int r) {
  int n = r - l;
  if (n <= 1)
    return 0;
  ll ans = 0;
  ans += solve(l, (l + r) / 2);
  ans += solve((l + r) / 2, r);
  int li = 0, ri = 0;
  int size_vl = (l + r) / 2 - l;
  int size_vr = r - (l + r) / 2;
  for (int i = 0; i < size_vl; i++) {
    vec_l[i] = vec[l + i];
  }
  for (int i = 0; i < size_vr; i++) {
    vec_r[i] = vec[(l + r) / 2 + i];
  }
  for (int i = 0; i < size_vl; i++) {
    seg.update(vec_l[i].second, 1);
    seg2.update(vec_l[i].second, vec_l[i].second);
  }
  for (int i = 0; i < n; i++) {
    if (li < size_vl && (ri == size_vr || vec_l[li].first < vec_r[ri].first)) {
      seg.update(vec_l[li].second, -1);
      seg2.update(vec_l[li].second, -vec_l[li].second);
      vec[l + i] = vec_l[li++];
    } else {
      if (ri < size_vr) {
        ans += (ll)seg2.query(0, vec_r[ri].second) * c +
               seg.query(0, vec_r[ri].second) * vec_r[ri].second;
        ans += (ll)seg2.query(vec_r[ri].second, 100000) +
               seg.query(vec_r[ri].second, 100000) * vec_r[ri].second * c;
      }
      vec[l + i] = vec_r[ri++];
    }
  }
  return ans;
}

int main(void) {
  scanf("%d%d", &n, &c);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &b[i], &w[i]);
    b[i]--;
    vec[i] = P(b[i], w[i]);
  }
  printf("%lld\\n", solve(0, n));
  return 0;
}
Problem p01967 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 1001

int capacity[NUM], table[NUM];

int main() {

  int N;
  scanf("%d", &N);

  for (int i = 1; i <= N; i++) {
    scanf("%d", &capacity[i]);
    table[i] = 0;
  }

  int Q;
  scanf("%d", &Q);

  int command, apple_id, num;

  for (int loop = 0; loop < Q; loop++) {
    scanf("%d %d %d", &command, &apple_id, &num);

    if (command == 1) {
      table[apple_id] += num;
      if (table[apple_id] > capacity[apple_id]) {
        printf("%d\\n", apple_id);
        return 0;
      }
    } else {
      table[apple_id] -= num;
      if (table[apple_id] < 0) {
        printf("%d\\n", apple_id);
        return 0;
      }
    }
  }

  printf("0\\n");

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

#define NUM 101

int capacity[NUM], table[NUM];

int main() {

  int N;
  scanf("%d", &N);

  for (int i = 1; i <= N; i++) {
    scanf("%d", &capacity[i]);
    table[i] = 0;
  }

  int Q;
  scanf("%d", &Q);

  int command, apple_id, num;

  for (int loop = 0; loop < Q; loop++) {
    scanf("%d %d %d", &command, &apple_id, &num);

    if (command == 1) {
      table[apple_id] += num;
      if (table[apple_id] > capacity[apple_id]) {
        printf("%d\\n", apple_id);
        return 0;
      }
    } else {
      table[apple_id] -= num;
      if (table[apple_id] < 0) {
        printf("%d\\n", apple_id);
        return 0;
      }
    }
  }

  printf("0\\n");

  return 0;
}
Problem p01970 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long
using namespace std;

bool prime[1000009];

void era() {
  prime[0] = 1;
  prime[1] = 1;
  for (int i = 2; i < 1000009; i++) {
    if (!prime[i]) {
      for (int j = i + i; j < 1000009; j += i)
        prime[j] = 1;
    }
  }
}

int dp[100009][2];

signed main() {
  era();
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    scanf("%ld", &a[i]);
  if (!prime[a[0]]) {
    dp[0][0] = 1;
    dp[0][1] = 1;
  }
  for (int i = 0; i < n; i++) {

    // 1

    if (i + 1 < n && a[i] < a[i + 1] && !prime[a[i + 1]])
      dp[i + 1][0] += dp[i][0];
    if (i + 2 < n && a[i] < a[i + 1] && !prime[a[i + 1]])
      dp[i + 1][1] += dp[i][0];

    // 2

    if (i + 2 < n && a[i] < a[i + 2] && !prime[a[i + 2]])
      dp[i + 2][0] += dp[i][1];
    if (i + 3 < n && a[i] < a[i + 2] && !prime[a[i + 2]])
      dp[i + 2][1] += dp[i][1];

    dp[i + 1][0] %= 1000000007;
    dp[i + 1][1] %= 1000000007;
    dp[i + 2][0] %= 1000000007;
    dp[i + 2][1] %= 1000000007;
  }
  cout << (dp[n - 1][0] + dp[n - 2][1]) % 1000000007 << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;

bool prime[1000009];

void era() {
  prime[0] = 1;
  prime[1] = 1;
  for (int i = 2; i < 1000009; i++) {
    if (!prime[i]) {
      for (int j = i + i; j < 1000009; j += i)
        prime[j] = 1;
    }
  }
}

int dp[100009][2];

signed main() {
  era();
  int n;
  cin >> n;
  int a[n];
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  if (!prime[a[0]]) {
    dp[0][0] = 1;
    dp[0][1] = 1;
  }
  for (int i = 0; i < n; i++) {

    // 1

    if (i + 1 < n && a[i] < a[i + 1] && !prime[a[i + 1]])
      dp[i + 1][0] += dp[i][0];
    if (i + 2 < n && a[i] < a[i + 1] && !prime[a[i + 1]])
      dp[i + 1][1] += dp[i][0];

    // 2

    if (i + 2 < n && a[i] < a[i + 2] && !prime[a[i + 2]])
      dp[i + 2][0] += dp[i][1];
    if (i + 3 < n && a[i] < a[i + 2] && !prime[a[i + 2]])
      dp[i + 2][1] += dp[i][1];

    dp[i + 1][0] %= 1000000007;
    dp[i + 1][1] %= 1000000007;
    dp[i + 2][0] %= 1000000007;
    dp[i + 2][1] %= 1000000007;
  }
  cout << (dp[n - 1][0] + dp[n - 2][1]) % 1000000007 << endl;
}
Problem p01970 (C++) — buggy submission, verdict: Runtime Error

// {{{
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
// }}}

using ll = long long;

const ll MOD = 1000000007;

char prime[1000005];
void init_prime() {
  fill(prime, prime + 1000005, 1);
  prime[0] = prime[1] = 0;
  for (int x = 2; x <= 1000000; ++x) {
    if (prime[x] && x <= 1000) {
      for (int y = x * x; y <= 1000000; y += x)
        prime[y] = 0;
    }
  }
}

ll N, Q[101010];

ll memo[101010];

int main() {
  init_prime();
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  for (int j = 1; j <= N; ++j)
    cin >> Q[j];
  reverse(Q + 1, Q + N + 1);

  fill(memo, memo + N + 1, 0);
  memo[0] = 1;
  memo[1] = prime[Q[1]] ? 1 : 0;

  for (int j = 2; j <= N; ++j) {
    if (prime[Q[j]]) {
      if (Q[j - 1] > Q[j]) {
        memo[j] = (memo[j] + memo[j - 1]) % MOD;
      }

      if (Q[j - 1] > 1 && (j < 3 || (prime[Q[j - 2]] && Q[j - 2] > Q[j]))) {
        memo[j] = (memo[j] + memo[j - 2]) % MOD;
      }
    }
  }

  cout << memo[N] << endl;

  return 0;
}


Error: -11

Fixed submission:

// {{{
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;
// }}}

using ll = long long;

const ll MOD = 1000000007;

char prime[1000005];
void init_prime() {
  fill(prime, prime + 1000005, 1);
  prime[0] = prime[1] = 0;
  for (int x = 2; x <= 1000000; ++x) {
    if (prime[x]) {
      for (int y = x * x; y <= 1000000; y += x)
        prime[y] = 0;
    }
  }
}

ll N, Q[101010];

ll memo[101010];

int main() {
  init_prime();
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  for (int j = 1; j <= N; ++j)
    cin >> Q[j];
  reverse(Q + 1, Q + N + 1);

  fill(memo, memo + N + 1, 0);
  memo[0] = 1;
  memo[1] = prime[Q[1]] ? 1 : 0;

  for (int j = 2; j <= N; ++j) {
    if (prime[Q[j]]) {
      if (Q[j - 1] > Q[j]) {
        memo[j] = (memo[j] + memo[j - 1]) % MOD;
      }

      if (Q[j - 1] > 1 && (j < 3 || (prime[Q[j - 2]] && Q[j - 2] > Q[j]))) {
        memo[j] = (memo[j] + memo[j - 2]) % MOD;
      }
    }
  }

  cout << memo[N] << endl;

  return 0;
}
Problem p01973 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)

using ull = unsigned long long;
const ull b = 1000000007;

ull f(string s) {
  ull h = 0;
  rep(i, s.size()) h = b * h + s[i];
  return h;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  string s;
  cin >> s;
  int n = s.size();
  int q;
  cin >> q;
  vector<unordered_set<ull>> hashs(n + 1);
  rep(i, q) {
    string t;
    cin >> t;
    int m = t.size();
    if (m > n)
      continue;
    hashs[m].insert(f(t));
  }

  vector<int> l(n, -1);
  ull bb = 1;
  for (int len = 1; len <= n; len++) {
    bb *= b;
    if (hashs[len].empty())
      continue;
    ull h = f(s.substr(0, len));
    int r = len - 1;
    while (true) {
      if (hashs[len].count(h) && l[r] == -1) {
        l[r] = r - len + 1;
      }
      r++;
      if (r >= n)
        break;
      h = b * h - bb * s[r - len] + s[r];
    }
  }

  int cnt = 0;
  int r = -1;
  rep(i, n) {
    if (r < l[i]) {
      cnt++;
      r = i;
    }
  }

  cout << cnt << endl;
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)

using ull = unsigned long long;
const ull b = 1000000007;

ull f(string s) {
  ull h = 0;
  rep(i, s.size()) h = b * h + s[i];
  return h;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  string s;
  cin >> s;
  int n = s.size();
  int q;
  cin >> q;
  vector<unordered_set<ull>> hashs(n + 1);
  rep(i, q) {
    string t;
    cin >> t;
    int m = t.size();
    if (m > n)
      continue;
    hashs[m].insert(f(t));
  }

  vector<int> l(n, -1);
  ull bb = 1;
  for (int len = 1; len <= n; len++) {
    bb *= b;
    ull h = f(s.substr(0, len));
    int r = len - 1;
    while (true) {
      if (hashs[len].count(h) && l[r] == -1) {
        l[r] = r - len + 1;
      }
      r++;
      if (r >= n)
        break;
      h = b * h - bb * s[r - len] + s[r];
    }
  }

  int cnt = 0;
  int r = -1;
  rep(i, n) {
    if (r < l[i]) {
      cnt++;
      r = i;
    }
  }

  cout << cnt << endl;
}
Problem p01973 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb emplace_back

#define B (ll)(101)
#define M (ll)(1LL << 30)

ll h(string s) {
  ll ret = 0;
  rep(i, s.size()) {
    ret *= B;
    ret += s[i] - 'a' + 1;
    ret %= M;
  }

  return ret;
}

int main() {
  string s;
  cin >> s;

  int n;
  cin >> n;
  vector<string> vs(n);
  rep(i, n) cin >> vs[i];

  map<ll, vector<ll>> hashes;
  rep(i, vs.size()) {
    int l = vs[i].size();
    hashes[l].pb(h(vs[i]));
  }

  vector<ll> rhash(s.size() + 1, 0);
  rhash[1] = s[0] - 'a' + 1;
  for (int i = 1; i <= s.size(); i++) {
    rhash[i] = (rhash[i - 1] * B + (s[i - 1] - 'a' + 1)) % M;
  }

  vector<int> p(s.size(), 0);
  p[0] = 1;
  for (int i = 1; i < s.size(); i++)
    p[i] = (p[i - 1] * B) % M;

  vector<int> lside(s.size(), -1);
  for (auto emp : hashes) {
    int l = emp.first;
    sort(all(emp.second));

    for (int i = 0; i + l - 1 < s.size(); i++) {
      ll hash = (rhash[i + l] - rhash[i] * p[l] % M + M) % M;
      if (*lower_bound(all(emp.second), hash) == hash)
        lside[i + l - 1] = max(lside[i + l - 1], i);
    }
  }

  int used = -1;
  int ans = 0;
  rep(i, lside.size()) {
    if (lside[i] == -1)
      continue;
    if (lside[i] > used) {
      used = i;
      ans++;
    }
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb emplace_back
#define INF (1e9 + 1)

#define B (ll)(101)
#define M (ll)(1LL << 50)

ll h(string s) {
  ll ret = 0;
  rep(i, s.size()) {
    ret *= B;
    ret += s[i] - 'a' + 1;
    ret %= M;
  }

  return ret;
}

int main() {
  string s;
  cin >> s;

  int n;
  cin >> n;
  vector<string> vs(n);
  rep(i, n) cin >> vs[i];

  map<ll, vector<ll>> hashes;
  rep(i, vs.size()) {
    int l = vs[i].size();
    hashes[l].pb(h(vs[i]));
  }

  vector<ll> rhash(s.size() + 1, 0);
  rhash[1] = s[0] - 'a' + 1;
  for (int i = 1; i <= s.size(); i++) {
    rhash[i + 1] = (rhash[i] * B + (s[i] - 'a' + 1)) % M;
  }

  vector<int> p(s.size(), 0);
  p[0] = 1;
  for (int i = 1; i < s.size(); i++)
    p[i] = (p[i - 1] * B) % M;

  vector<int> lside(s.size(), -INF);
  for (auto emp : hashes) {
    int l = emp.first;
    sort(all(emp.second));

    for (int i = 0; i + l - 1 < s.size(); i++) {
      ll hash = (rhash[i + l] - rhash[i] * p[l] % M + M) % M;
      if (*lower_bound(all(emp.second), hash) == hash)
        lside[i + l - 1] = max(lside[i + l - 1], i);
    }
  }

  int used = -1;
  int ans = 0;
  rep(i, lside.size()) {
    if (lside[i] == -INF)
      continue;
    if (lside[i] > used) {
      used = i;
      ans++;
    }
  }
  cout << ans << endl;
}