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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const ll MOD = 998244353;

ll n, k;
vector<pair<ll, ll>> v;
vector<ll> sum;
ll dp[200005];

int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n >> k;
  v.resize(k);
  sum.resize(k);
  for (auto &i : v) {
    cin >> i.first >> i.second;
  }
  dp[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      if (i - v[j].first > 0) {
        sum[j] += dp[i - v[j].first];
        sum[j] = (sum[j] + MOD) % MOD;
      }
      dp[i] = (dp[i] + sum[j]) % MOD;
      if (i - v[j].second > 0) {
        sum[j] -= dp[i - v[j].second];
        sum[j] = (sum[j] + MOD) % MOD;
      }
    }
  }
  cout << dp[n] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const ll MOD = 998244353;

ll n, k;
bool check[200005];
ll dp[200005];

int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; j++) {
      check[j] = true;
    }
  }
  dp[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (int j = 1; j <= n && i - j > 0; j++) {
      if (check[j]) {
        dp[i] = (dp[i] + dp[i - j]) % MOD;
      }
    }
  }
  cout << dp[n] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF long long(int) 1e18 + 1
#define MAX 200001
#define rep(i, n, m) for (int i = n; i < m; i++)

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  set<int> s;
  vector<pair<int, int>> d(k);
  vector<long long> bsum(k);
  rep(i, 0, k) {
    int x, y;
    cin >> x >> y;
    d[i].first = x;
    d[i].second = y;
    bsum[i] = 0;
    rep(i, x, y + 1) s.insert(i);
  }
  vector<long long> dp(n + 2, 0);
  for (auto a : s)
    dp[a + 1]++;
  rep(i, 1, n + 1) {
    rep(j, 0, k) {
      if (i - d[j].first >= 0)
        bsum[j] += dp[i - d[j].first];
      if (i - d[j].second > 0)
        bsum[j] -= dp[i - d[j].second - 1];
      dp[i] += bsum[j];
      dp[i] %= 998244353;
    }
  }
  cout << dp[n] << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define INF long long(int) 1e18 + 1
#define MAX 200001
#define rep(i, n, m) for (int i = n; i < m; i++)

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  set<int> s;
  vector<pair<int, int>> d(k);
  rep(i, 0, k) {
    int x, y;
    cin >> x >> y;
    d[i].first = x;
    d[i].second = y;
    rep(i, x, y + 1) s.insert(i);
  }
  vector<long long> dp(n + 2, 0);
  for (auto a : s)
    dp[a + 1]++;
  rep(i, 1, n + 1) {
    for (auto a : s) {
      if (i - a >= 0)
        dp[i] += dp[i - a];
      dp[i] %= 998244353;
    }
  }
  cout << dp[n] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
#include <stdio.h>
#define int long long
using namespace std;
const int N = 5e5, M = 15, MOD = 998244353;
int a[N];
int f[N];
int x[M], y[M];
int n, m;
int qzh[N];
inline int modbez(int a, int b) {
  register int res = a % b;
  if (res < 0)
    return res + b;
  else
    return res;
}
void DP() {
  f[n] = 1;
  for (int i = n; i >= 1; i--) {
    for (int j = 1; j <= m; j++) {
      f[i] = modbez(f[i] + qzh[i + x[j]] - qzh[i + y[j] + 1], MOD);
    }
    qzh[i] = modbez(qzh[i + 1] + f[i], MOD);
  }
  cout << f[1] % MOD << endl;
}
void common_DP() {
  f[n] = 1;
  for (int i = n; i >= 1; i--)
    for (int j = 1; j <= m; j++)
      for (int k = x[j]; k <= y[j]; k++) {
        if (i + k <= n) {
          f[i] = (f[i] + f[i + k]) % MOD;
        }
      }
  cout << f[1] << endl;
}
signed main() {
  memset(f, 0, sizeof(f));
  memset(a, 0, sizeof(a));
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> x[i] >> y[i];
  }
  if (n < 1e4)
    common_DP();
  else
    DP();
}


Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <stdio.h>
#define int long long
using namespace std;
const int N = 5e5, M = 15, MOD = 998244353;
int a[N];
int f[N];
int x[M], y[M];
int n, m;
int qzh[N];
inline int modbez(int a, int b) {
  register int res = a % b;
  if (res < 0)
    return res + b;
  else
    return res;
}
void DP() {
  f[n] = 1;
  for (int i = n; i >= 1; i--) {
    for (int j = 1; j <= m; j++) {
      f[i] = modbez(f[i] + qzh[i + x[j]] - qzh[i + y[j] + 1], MOD);
    }
    qzh[i] = modbez(qzh[i + 1] + f[i], MOD);
  }
  cout << f[1] % MOD << endl;
}
void common_DP() {
  f[n] = 1;
  for (int i = n; i >= 1; i--)
    for (int j = 1; j <= m; j++)
      for (int k = x[j]; k <= y[j]; k++) {
        if (i + k <= n) {
          f[i] = (f[i] + f[i + k]) % MOD;
        }
      }
  cout << f[1] << endl;
}
signed main() {
  memset(f, 0, sizeof(f));
  memset(a, 0, sizeof(a));
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> x[i] >> y[i];
  }
  if (n > 1e4)
    common_DP();
  else
    DP();
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>
using LL = long long;
using namespace std;
int mod = 998244353;
int main() {
  LL N, K;
  cin >> N >> K;
  vector<LL> Li(K), Ri(K);
  vector<bool> S_has(N + 1, false);
  for (int i = 0; i < K; i++) {
    cin >> Li[i] >> Ri[i];
    for (int j = Li[i]; j <= Ri[i]; j++) {
      S_has[j] = true;
    }
  }
  vector<LL> Dp(N + 1, 0);
  Dp[1] = 1;

  for (int i = 2; i <= N; i++) {
    Dp[i] += Dp[i - 1];
    for (int k = 0; k < K; k++) {
      if (i - Li[k] > 0) {
        Dp[i] += Dp[i - Li[k]];
      }
      if (i - Ri[k] - 1 > 0) {
        Dp[i] -= Dp[i - Ri[k] - 1];
      }
    }
    while (Dp[i] < 0) {
      Dp[i] += mod;
    }
    Dp[i] = Dp[i] % mod;
  }
  LL ans = Dp[N] - Dp[N - 1];
  while (ans < 0) {
    ans += mod;
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using LL = long long;
using namespace std;
int mod = 998244353;
int main() {
  LL N, K;
  cin >> N >> K;
  vector<LL> Li(K), Ri(K);
  vector<bool> S_has(N + 1, false);
  for (int i = 0; i < K; i++) {
    cin >> Li[i] >> Ri[i];
    for (int j = Li[i]; j <= Ri[i]; j++) {
      S_has[j] = true;
    }
  }
  vector<LL> Dp(N + 1, 0);
  Dp[1] = 1;

  for (int i = 1; i < N; i++) {
    for (int j = 1; j <= N; j++) {
      if (S_has[j] == true && i + j <= N) {
        Dp[i + j] = (Dp[i + j] + Dp[i]) % mod;
      }
    }
  }
  cout << Dp[N] << endl;

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

#include <bits/stdc++.h>
#include <cmath>
#include <limits>

using namespace std;
typedef long long LL;

static long long INF = (1LL << 62);
const LL C = 998244353;

LL N, K;
// dp[i] はマスiに行ける個数(をCで割ったもの)
LL dp[200004] = {0};
LL sdp[200004] = {0}; // dpの累積和

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> K;
  vector<int> L, R;
  for (int i = 0; i < K; i++) {
    int l, r;
    cin >> l >> r;
    L.push_back(l);
    R.push_back(r);
  }
  sort(L.begin(), L.end());
  sort(R.begin(), R.end());

  dp[0] = 0;
  dp[1] = 1;
  sdp[0] = 0;
  sdp[1] = 1;

  for (int i = 2; i <= N; i++) {
    for (int j = 0; j < K; j++) {
      LL l = max(0, i - R[j] - 1);
      LL r = max(0, i - L[j]);
      dp[i] += (sdp[r] - sdp[l]);
      dp[i] %= C;
      if (dp[i] < 0)
        dp[i] += C;
    }
    sdp[i] = sdp[i - 1] + dp[i];
    sdp[i] %= C;
  }
  printf("%lld\\n", dp[N]);
  return 0;
}


Error: TLE

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

#include <bits/stdc++.h>
#include <cmath>
#include <limits>

using namespace std;
typedef long long LL;
using Graph = vector<vector<int>>;

static long long INF = (1LL << 62);
const LL C = 998244353;

LL N, K;
LL dp[200004] = {0};
int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> K;
  vector<LL> D;
  for (int i = 0; i < K; i++) {
    LL l, r;
    cin >> l >> r;
    for (LL j = l; j <= r; j++) {
      D.push_back(j);
    }
  }
  sort(D.begin(), D.end());
  dp[1] = 1;
  if (D[0] == 1) {
    dp[2] = 1;
  } else {
    dp[2] = 0;
  }

  for (int i = 3; i <= N; i++) {
    for (int j = 0; j < D.size() && i - D[j] >= 1; j++) {
      dp[i] += dp[i - D[j]];
      dp[i] %= C;
    }
    dp[i] %= C;
  }
  printf("%lld\\n", dp[N]);
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 998244353;
int sum[200010], dp[200010];
pair<int, int> a[15];
signed main() {
  int n, k, i, j;
  cin >> n >> k;
  for (i = 1; i <= k; i++)
    cin >> a[i].first >> a[i].second;
  sort(a + 1, a + k + 1);
  dp[1] = 1;
  for (i = 2; i <= n; i++) {
    for (j = 1; j <= k; j++)
      sum[j] = (sum[j] + mod + mod - dp[max(0ll, i - a[j].second - 1)] +
                dp[max(0ll, i - a[j].first)]) %
               mod;
    for (j = 1; j <= k; j++)
      dp[i] = (dp[i] + sum[j]) % mod;
  }
  cout << dp[n] % mod;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 998244353;
int pos[100010], sum[100010], dp[100010];
pair<int, int> a[15];
signed main() {
  int n, k, i, j, p;
  cin >> n >> k;
  for (i = 1; i <= k; i++) {
    cin >> a[i].first >> a[i].second;
    for (j = a[i].first; j <= a[i].second; j++)
      pos[j] = i;
  }
  sort(a + 1, a + k + 1);
  dp[1] = 1;
  if (pos[1])
    sum[pos[1]] = (sum[pos[1]] + dp[1]) % mod;
  for (i = 2; i <= n; i++) {
    for (j = 1; j <= k; j++)
      dp[i] = (dp[i] + sum[j]) % mod;
    if (pos[i])
      sum[pos[i]] = (sum[pos[i]] + dp[i]) % mod;
  }
  cout << dp[n];
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int n, k, m = 0;
int l[10], r[10], a[200001];
const int M = 998244353;

int main() {
  cin >> n >> k;

  for (int i = 0; i < k; i++)
    cin >> l[i] >> r[i];

  a[1] = 1;
  a[2] = -1;

  for (int i = 1; i <= n; i++) {
    m = (m + a[i]) % M;

    for (int j = 0; j < k; j++) {
      if (i + l[j] <= n)
        a[i + l[j]] = (a[i + l[j]] + m) % M;

      if (i + r[j] + 1 <= n)
        a[i + r[j] + 1] = (M + a[i + r[j] + 1] - m) % M;
    }
  }

  cout << m << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int n, k;
int l[10], r[10], a[200001];
const int M = 998244353;

int main() {

  cin >> n >> k;

  for (int i = 0; i < k; i++)
    cin >> l[i] >> r[i];

  fill(a, a + n + 1, 0);
  a[1] = 1;

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < k; j++) {
      if (i + l[j] <= n) {
        for (int k = l[j]; k <= r[j]; k++) {
          if (i + k <= n)
            a[i + k] = (a[i + k] + a[i]) % M;
        }
      }
    }
  }

  cout << a[n] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse4")
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define int ll
#define all(x) x.begin(), x.end()
#define trav(i, a) for (auto &i : a)
inline int in() {
  int x;
  scanf("%lld", &x);
  return x;
}
const int mod = 998244353;
const int N = 3e5 + 4;
int tree[N], n, k, r[11], l[11];
void add(int x, int val) {
  while (x <= n) {
    tree[x] += val;
    tree[x] %= mod;
    tree[x] += mod;
    tree[x] %= mod;
    x += (x & -x);
  }
}
int sum(int x) {
  int ans = 0;
  while (x >= 1) {
    ans += tree[x];
    ans %= mod;
    ans += mod;
    ans %= mod;
    x -= (x & -x);
  }
  return ans;
}

int32_t main() {
  n = in();
  k = in();
  for (int i = 1; i <= k; i++)
    l[i] = in(), r[i] = in();
  add(1, 1);
  add(2, -1);
  for (int i = 1; i <= n; i++) {
    int val = sum(i);
    if (1) {
      for (int j = 1; j <= k; j++) {
        add(i + l[j], val);
        add(i + r[j] + 1, -val);
      }
    }
  }
  cout << sum(n) % mod;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse4")
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define int ll
#define all(x) x.begin(), x.end()
#define trav(i, a) for (auto &i : a)
inline int in() {
  int x;
  scanf("%lld", &x);
  return x;
}
const int mod = 998244353;
const int N = 4e5 + 8;
int pre[N], a[N];
int l[11], r[11];
int32_t main() {
  int n = in();
  int k = in();
  for (int i = 1; i <= k; i++)
    l[i] = in(), r[i] = in();
  a[1] = 1;
  for (int i = 1; i <= n; i++) {
    if (a[i] == 0 and i != 1)
      continue;
    for (int j = 1; j <= k; j++) {
      pre[i + l[j]] += a[i];
      pre[i + r[j] + 1] -= a[i];
    }
    for (int i = 1; i <= n; i++) {
      pre[i] += pre[i - 1];
      pre[i] %= mod;
    }

    for (int i = 1; i <= n; i++) {
      a[i] += pre[i];
      a[i] %= mod;
      pre[i] = 0;
    }
    // for(int i=1;i<=n;i++)cout<<a[i]<<" ";
    // cout<<"\\n";
  }
  cout << a[n] % mod;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ll = long long int;
const ll MOD = 998244353;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  for (int i = 0; i < k; i++)
    cin >> l[i] >> r[i];

  vector<int> isexist_s(n + 5, 0);
  for (int i = 0; i < k; i++) {
    isexist_s[l[i]]++;
    isexist_s[r[i] + 1]--;
  }
  for (int i = 1; i < n + 5; i++)
    isexist_s[i] += isexist_s[i - 1];

  vector<int> move;
  for (int i = 1; i <= n; i++) {
    if (isexist_s[i])
      move.push_back(i);
  }

  vector<ll> memo(n + 1, 0);
  vector<ll> submemo(k, 0);
  memo[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      if (i - r[j] - 1 >= 1)
        submemo[j] -= memo[i - r[j] - 1];
      if (i - l[j] >= 1)
        submemo[j] += memo[i - l[j]];

      memo[i] += submemo[j];
      memo[i] %= MOD;
    }
  }

  cout << memo[n] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using ll = long long int;
const ll MOD = 998244353;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  for (int i = 0; i < k; i++)
    cin >> l[i] >> r[i];

  vector<int> isexist_s(n + 5, 0);
  for (int i = 0; i < k; i++) {
    isexist_s[l[i]]++;
    isexist_s[r[i] + 1]--;
  }
  for (int i = 1; i < n + 5; i++)
    isexist_s[i] += isexist_s[i - 1];

  vector<int> move;
  for (int i = 1; i <= n; i++) {
    if (isexist_s[i])
      move.push_back(i);
  }

  vector<ll> memo(n + 1, 0);
  memo[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < move.size() && i - move[j] >= 0; j++) {
      memo[i] += memo[i - move[j]];
      memo[i] %= MOD;
    }
  }

  cout << memo[n] << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <iostream>
typedef long long ll;
typedef unsigned int ui;
#define infin (ll)(998244353)
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int l, r;
  vector<ll> dp(n + 1, 0); // 0 to n
  vector<pair<int, int>> v;
  for (int j = 0; j < k; j++) {
    cin >> l >> r;
    v.push_back({l, r});
  }
  dp[0] = 1;
  ;
  dp[1] = 1;
  sort(v.begin(), v.end());
  auto z = v.begin();
  if ((*z).first == 1)
    dp[2] = 1;
  else
    dp[2] = 0;
  for (int i = 3; i <= n; i++) {
    dp[i] = dp[i - 1];
    for (auto x : v) {
      if (i > x.first)
        dp[i] += dp[i - x.first];
      else
        break;
      if (i - 1 > x.second) {
        dp[i] -= dp[i - 1 - x.second];
        if (dp[i] < 0)
          dp[i] += infin;
      }
    }
    dp[i] = (dp[i]) % infin;
  }
  cout << dp[n] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
typedef long long ll;
typedef unsigned int ui;
#define infin (ll)(998244353)
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  int l, r;
  vector<ll> dp(n + 1, 0); // 0 to n
  vector<pair<int, int>> v;
  for (int j = 0; j < k; j++) {
    cin >> l >> r;
    v.push_back({l, r});
  }
  dp[0] = 1;
  ;
  dp[1] = 1;
  sort(v.begin(), v.end());
  for (int i = 2; i <= n; i++) {
    for (auto x : v) {
      int e = 0;
      for (int j = x.first; j <= x.second; j++) {
        if (j >= i) {
          e = 1;
          break;
        } else {
          dp[i] = (dp[i] + dp[i - j]) % infin;
        }
      }
      if (e == 1)
        break;
    }
  }
  cout << dp[n] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

const int MOD = 998244353;

int main() {
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> ar(k);
  for (int i = 0; i < k; i++) {
    cin >> ar[i].first >> ar[i].second;
  }

  vector<ll> dp(n + 1);
  dp[1] = 1;

  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      if (i <= ar[j].first) {
        continue;
      } else if (i <= ar[j].second) {
        dp[i] += dp[i - ar[j].first];
      } else {
        dp[i] += dp[i - ar[j].first] - dp[i - ar[j].second - 1];
      }
      dp[i] = (dp[i] + MOD) % MOD;
    }
    if (i != n)
      dp[i] += dp[i - 1];
  }
  cout << dp[n] << '\\n';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = long double;

const int MOD = 998244353;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> st;
  for (int i = 0; i < k; i++) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; j++) {
      st.push_back(j);
    }
  }

  vector<int> dp(n + 1);
  dp[1] = 1;

  for (int i = 2; i <= n; i++) {
    for (auto x : st) {
      if (i - x < 0)
        continue;
      dp[i] = (dp[i] + dp[i - x]) % MOD;
    }
  }

  cout << dp[n] << '\\n';
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define nl '\\n'

using namespace std;

typedef long long ll;

void solve() {
  int N, K;
  cin >> N >> K;
  vector<int> L(K), R(K);
  for (int i = 0; i < K; i++)
    cin >> L[i] >> R[i];
  const int MOD = 998244353;

  vector<int> dp(N + 1);
  dp[1] = 1, dp[2] = -1;
  for (int i = 1; i <= N; i++) {
    dp[i] = ((dp[i] + dp[i - 1]) % MOD + MOD) % MOD;
    for (int j = 0; j < K; j++) {
      if (i + L[j] <= N)
        dp[i + L[j]] = ((dp[i + L[j]] + dp[i]) % MOD + MOD) % MOD;
      if (i + R[j] + 1 <= N)
        dp[i + R[j] + 1] = ((dp[i + R[j] + 1] - dp[i]) % MOD + MOD) % MOD;
    }
  }

  cout << dp[N];
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  // int t;
  // cin >> t;
  // while (t--) {
  solve();
  cout << nl;
  // }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define nl '\\n'

using namespace std;

typedef long long ll;

void solve() {
  int N, K;
  cin >> N >> K;
  vector<pair<int, int>> range(K);
  for (int i = 0; i < K; i++)
    cin >> range[i].first >> range[i].second;
  const int MOD = 998244353;

  vector<int> dp(N + 1);
  dp[1] = 1;
  for (int i = 2; i <= N; i++) {
    for (int j = 0; j < K; j++) {
      for (int k = range[j].first; k <= range[j].second; k++) {
        if (k >= i)
          break;
        dp[i] = (dp[i] + dp[i - k]) % MOD;
      }
    }
  }

  cout << dp[N];
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  // int t;
  // cin >> t;
  // while (t--) {
  solve();
  cout << nl;
  // }

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
#define mod 998244353
typedef long long ll;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % mod + mod) % mod) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime mod
  mint inv() const { return pow(mod - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  vector<mint> dp(n + 1);
  vector<mint> dpsum(n + 1);
  rep(i, k) cin >> l[i] >> r[i];

  dp[1] = 1;
  dpsum[1] = 1;

  for (int i = 2; i <= n; i++) {
    rep(j, k) {
      int li = i - r[j];
      int ri = i - l[j];
      if (ri < 0)
        continue;
      li = max(li, 1);
      dp[i] += dpsum[ri] - dpsum[li - 1];
    }
    dpsum[i] = dpsum[i - 1] + dp[i];
  }

  cout << dp[n] << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, k;
  cin >> n >> k;
  set<int> s;
  vector<ll> dp(n + 1, 0);
  int l, r;
  rep(i, k) {
    cin >> l >> r;
    for (int i = l; i <= r; i++)
      s.insert(i);
  }
  dp[1] = 1;

  for (int i = 2; i <= n; i++) {
    for (auto itr = s.begin(); itr != s.end(); ++itr) {
      if (i - *itr <= 0)
        break;
      dp[i] += dp[i - *itr];
    }
  }

  cout << dp[n] % mod << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

// D - Leaping Tak
#include <bits/stdc++.h>

using namespace std;

#define MOD 998244353

void fen_update(vector<long long> &tree, int i, long long delta) {
  for (; i < tree.size(); i |= i + 1)
    tree[i] = (tree[i] + delta) % MOD;
}

long long fen_get(const vector<long long> &tree, int i) {
  long long sum = 0;
  for (; i > 0; i &= i - 1)
    sum = (sum + tree[i - 1]) % MOD;
  return sum;
}

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> s(k);
  for (int i = 0; i < k; i++)
    cin >> s[i].first >> s[i].second;
  sort(s.begin(), s.end());
  vector<long long> t(n);
  fen_update(t, 0, 1);
  for (int i = 1; i < n; i++)
    for (auto x : s) {
      if (i - x.first < 0)
        break;
      int r = max(0, i - x.first), l = max(0, i - x.second);
      long long d = (fen_get(t, r + 1) - fen_get(t, l) + MOD) % MOD;
      fen_update(t, i, d);
    }

  cout << (fen_get(t, n) - fen_get(t, n - 1) + MOD) % MOD << endl;

  return 0;
}


Error: TLE

Fixed submission:

// D - Leaping Tak
#include <bits/stdc++.h>

using namespace std;

#define MOD 998244353

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> s;
  for (int i = 0; i < k; i++) {
    int l, r;
    cin >> l >> r;
    for (int x = l; x <= r; x++)
      s.push_back(x);
  }
  sort(s.begin(), s.end());

  vector<long long> dp(n + 1);
  dp[1] = 1;
  for (int i = 1; i <= n; i++) {
    for (auto x : s) {
      if (i - x < 0)
        break;
      dp[i] = (dp[i] + dp[i - x]) % MOD;
    }
  }

  cout << dp[n] << endl;

  return 0;
}