alexjercan/bugnet huggingface dataset:
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;
using ll = long long;
const ll mod = 998244353;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  rep(i, k) { cin >> l[i] >> r[i]; }
  vector<int> dp(n + 10);
  dp[1] = 1;
  for (int i = 1; i < n; i++) {
    dp[i] += dp[i - 1];
    dp[i] %= mod;
    rep(j, k) {
      int num = min(i + r[j] + 1, (int)(n + 1));
      int nu = min(i + l[j], (int)(n + 1));
      dp[nu] += dp[i];
      dp[nu] %= mod;
      dp[num] -= dp[i];
      if (dp[num] < 0)
        dp[num] += mod;
    }
  }
  dp[n] %= mod;
  cout << dp[n] << endl;
  return 0;
}


Error: 0

Fixed submission:

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

using namespace std;
using ll = long long;
const ll mod = 998244353;
int main() {
  ll n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  rep(i, k) { cin >> l[i] >> r[i]; }
  vector<int> dp(n + 10);
  dp[1] = 1;
  for (int i = 1; i < n; i++) {
    dp[i] += dp[i - 1];
    dp[i] %= mod;
    rep(j, k) {
      int num = min(i + r[j] + 1, (int)(n + 1));
      dp[i + l[j]] += dp[i];
      dp[i + l[j]] %= mod;
      dp[num] -= dp[i];
      if (dp[num] < 0)
        dp[num] += mod;
    }
  }
  dp[n] %= mod;
  cout << dp[n] << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
ll MOD = 998244353;
void add(int &a, int b) {
  a = (a + b) % MOD;
  a = (a + MOD) % MOD;
}
int main() {
  int n, k;
  cin >> n >> k;
  vector<P> v(k);
  for (int i = 0; i < k; i++) {
    int l, r;
    cin >> l >> r;
    r++;
    v[i] = P(l, r);
  }
  vector<int> dp(n + 1, 0);
  vector<int> num(n + 1, 0);
  dp[1] = 1;
  for (int i = 1; i <= n; i++) {
    add(num[i], num[i - 1]);
    if (i > 1)
      dp[i] = num[i];
    for (int j = 0; j < k; j++) {
      int l = v[j].first;
      int r = v[j].second;
      if (i + l <= n)
        add(num[i + l], dp[i]);
      if (i + r <= n)
        add(num[i + r], -dp[i]);
    }
    // for(int i = 1; i <= n; i++)
    //{
    //     cout << dp[i] << " ";
    // }
    // cout << endl;
    // for(int i = 1; i <= n; i++)
    //{
    //     cout << num[i] << " ";
    // }
    // cout << endl;
    // cout << endl;
  }
  cout << dp[n] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll MOD = 998244353;
void add(ll &a, ll b) {
  a = (a + b) % MOD;
  a = (a + MOD) % MOD;
}
int main() {
  int n, k;
  cin >> n >> k;
  set<int> s;
  vector<ll> dp(n + 1, 0);
  dp[1] = 1;
  for (int i = 0; i < k; i++) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; j++) {
      s.insert(j);
    }
  }
  for (int i = 1; i < n; i++) {
    if (dp[i] == 0)
      continue;
    for (int ad : s) {
      if (i + ad <= n) {
        dp[i + ad] = (dp[i + ad] + dp[i]) % MOD;
      }
    }
    // for(int d : dp)
    // {
    //     cout << d << endl;
    // }
    // cout << endl;
  }
  cout << (dp[n] + MOD) % MOD << endl;
}
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;
using ll = long long;

int mod = 998244353;

int main(void) {
  int n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  rep(i, k) { cin >> l[i] >> r[i]; }

  vector<int> dp(n + 1);
  vector<int> dpsum(n + 1);
  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 < 1) {
        continue;
      }
      li = max(li, 1);
      dp[i] += (dpsum[ri] - dpsum[li - 1]) % mod;
      dp[i] %= mod;
    }
    dpsum[i] = (dpsum[i - 1] + dp[i]) % mod;
    dpsum[i] %= mod;
  }
  int ans = (dp[n] + mod) % mod;
  cout << ans << 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;
using ll = long long;

int mod = 998244353;

int calcA(vector<int> s, vector<int> a, int n) {
  int res = 0;
  rep(i, s.size()) {
    if (n - s[i] >= 0) {
      res += a[n - s[i]];
      res %= mod;
    }
  }
  return res;
}

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

  vector<int> s;
  rep(i, k) {
    int l, r;
    cin >> l >> r;
    for (int i = l; i <= r; i++) {
      s.push_back(i);
    }
  }
  sort(s.begin(), s.end());
  // rep(i,s.size()) cout << s[i] << " "; cout << endl;

  vector<int> a(n, 0);
  a[0] = 1;
  for (int i = 1; i < n; i++) {
    a[i] = calcA(s, a, i);
  }
  // rep(i,a.size()) cout << a[i] << " "; cout << endl;
  cout << a[n - 1] << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
***AUTHOR***
*ADVAITVD99*
*/
#include <bits/stdc++.h>

using namespace std;

int main() {
  // clock_t tim;
  // tim = clock();
  const long long MOD = 998244353;
  {
    int n, k;
    cin >> n >> k;
    vector<pair<int, int>> coins;
    for (int i = 0; i < k; i++) {
      int L, R;
      cin >> L >> R;
      R++;
      coins.push_back({L, R});
    }
    // sort(coins.begin(),coins.end());
    long long dp[n];
    fill(dp, dp + n, 0);
    dp[0] = 1;
    dp[1] = -1;
    for (int i = 0; i < n; i++) {
      if (i > 0) {
        dp[i] = (dp[i] + dp[i - 1]) % MOD;
      }
      for (int j = 0; j < k; j++) {
        int l, r;
        l = coins[j].first;
        r = coins[j].second;
        if (i + l < n) {
          dp[i + l] = (dp[i + l] + dp[i]) % MOD;
        }
        if (i + r < n) {
          dp[i + r] = (dp[i + r] - dp[i]);
        }
      }
    }
    dp[n - 1] = dp[n - 1] % MOD;
    if (dp[n - 1] < 0) {
      dp[n - 1] += MOD;
    }
    cout << dp[n - 1] << endl;
  }

  // tim = clock() - tim;
  // double time_taken = ((double)tim)/CLOCKS_PER_SEC;
  // cout<< fixed << setprecision(3) << time_taken << "seconds" <<endl;

  return 0;
}


Error: TLE

Fixed submission:

/*
***AUTHOR***
*ADVAITVD99*
*/
#include <bits/stdc++.h>

using namespace std;

int main() {
  clock_t tim;
  tim = clock();
  const long long MOD = 998244353;
  {
    int n, k;
    cin >> n >> k;
    vector<int> coins;
    for (int i = 0; i < k; i++) {
      int L, R;
      cin >> L >> R;
      for (int i = L; i <= R; i++) {
        coins.push_back(i);
      }
    }
    sort(coins.begin(), coins.end());
    long long dp[n + 1];
    fill(dp, dp + n + 1, 0);
    dp[1] = 1;
    for (int i = 2; i < n + 1; i++) {
      for (int j = 0; j < coins.size() && i - coins[j] >= 1; j++) {
        dp[i] = (dp[i] + dp[i - coins[j]]) % MOD;
      }
    }
    cout << dp[n] << endl;
  }

  // tim = clock() - tim;
  // double time_taken = ((double)tim)/CLOCKS_PER_SEC;
  // cout<< fixed << setprecision(3) << time_taken << "seconds" <<endl;

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

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

using i64 = long long;
using u64 = unsigned long long;

const int mod = 998244353;

void __print() { cerr << "]\\n"; }

template <typename T, typename... V> void __print(T t, V... v) {
  cerr << t;
  if (sizeof...(v))
    cerr << ", ";
  __print(v...);
}

#define dbg(x...) cerr << "[" << #x << "] = [", __print(x)
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(NULL)

i64 dp[300000];
i64 q_sum[1000000];

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  vector<pair<int, int>> v;
  while (k--) {
    int l, r;
    scanf("%d%d", &l, &r);
    v.push_back({l, r});
  }
  dp[1] = 1;
  for (int i = 1; i <= n; i++) {
    q_sum[i] += q_sum[i - 1];
    q_sum[i] %= mod;
    dp[i] += q_sum[i];
    dp[i] %= mod;
    for (auto p : v) {
      int x, y;
      tie(x, y) = p;
      q_sum[i + x] += dp[i];
      q_sum[i + x] %= mod;
      q_sum[i + y + 1] -= dp[i];
      q_sum[i + y + 1] %= mod;
    }
  }
  // for(int i = 1; i <= n; i++) dbg(dp[i]);
  // for(int i = 1; i <= n; i++) dbg(q_sum[i]);
  dp[n] %= mod;
  dp[n] += mod;
  dp[n] %= mod;
  printf("%lld\\n", dp[n]);
}


Error: TLE

Fixed submission:

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

using i64 = long long;
using u64 = unsigned long long;

const int mod = 998244353;

void __print() { cerr << "]\\n"; }

template <typename T, typename... V> void __print(T t, V... v) {
  cerr << t;
  if (sizeof...(v))
    cerr << ", ";
  __print(v...);
}

#define dbg(x...) cerr << "[" << #x << "] = [", __print(x)
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(NULL)

i64 dp[300000];

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  vector<int> steps;
  while (k--) {
    int l, r;
    scanf("%d%d", &l, &r);
    for (int i = l; i <= r; i++)
      steps.push_back(i);
  }
  dp[1] = 1;
  sort(steps.begin(), steps.end());
  int sz = steps.size();
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < sz; j++) {
      if (i + steps[j] > n)
        break;
      dp[i + steps[j]] += dp[i];
      dp[i + steps[j]] %= mod;
    }
  }
  printf("%lld\\n", dp[n]);
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const int N = 3e5 + 5;
const int mod = 998244353;

int n;
int k;
long long dp[N];
long long cm[N];
pair<int, int> pp[N];

int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= k; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    pp[i] = {l, r};
  }
  dp[n] = 1;
  cm[n] = 1;
  for (int i = n - 1; i >= 1; i--) {
    for (int j = 1; j <= k; j++) {
      dp[i] += cm[i + pp[j].first] + mod + mod;
      dp[i] -= cm[i + pp[j].second + 1];
      dp[i] %= mod;
    }
    // cout << dp[i] << " ";
    cm[i] = dp[i] + cm[i + 1];
    cm[i] %= mod;
  }
  printf("%lld\\n", dp[1]);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 5;
const int mod = 998244353;

int n;
int k;
int mem[N];
bool did[N];
vector<int> v;

int go(int poi) {
  if (poi == n)
    return 1;
  if (poi > n)
    return 0;
  int &ret = mem[poi];
  if (~mem[poi])
    return mem[poi];
  ret = 0;
  for (int i = 0; i < v.size(); i++) {
    if (v[i] + poi <= n) {
      ret = ret + go(poi + v[i]);
      ret %= mod;
    } else {
      break;
    }
  }
  return ret;
}

int main() {
  scanf("%d%d", &n, &k);
  memset(mem, -1, sizeof mem);
  for (int i = 1; i <= k; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    for (int i = l; i <= r; i++)
      v.push_back(i);
  }
  sort(v.begin(), v.end());
  for (int i = n; i > 1; i--)
    go(i);
  printf("%d\\n", go(1));
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> A;
  for (int i = 0; i < k; ++i) {
    int l, r;
    cin >> l >> r;
    A.push_back({l, r});
  }
  sort(A.begin(), A.end());
  vector<long long> Dp(n + 1, 0);
  Dp[0] = 1;
  Dp[1] = -1;
  for (int i = 0; i < n; ++i) {
    if (i > 0)
      Dp[i] = (Dp[i] + Dp[i - 1]) % MOD;
    for (int j = 0; j < k; ++j) {
      int l = A[j].first, r = A[j].second;
      if (i + l < n)
        Dp[i + l] = (Dp[i + l] + Dp[i]) % MOD;
      if (i + r < n) {
        Dp[i + r + 1] = (Dp[i + r + 1] - Dp[i]) % MOD;
        if (Dp[i + r + 1] < 0)
          Dp[i + r + 1] += MOD;
      }
    }
  }
  cout << Dp[n - 1] << "\\n";
}

Error: TLE

Fixed submission:

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> A;
  for (int i = 0; i < k; ++i) {
    int l, r;
    cin >> l >> r;
    A.push_back({l, r});
  }
  sort(A.begin(), A.end());
  vector<int> jump;
  for (int i = 0; i < A.size(); ++i) {
    for (int j = A[i].first; j <= A[i].second; ++j) {
      jump.push_back(j);
    }
  }
  vector<long long> Dp(n, 0);
  Dp[0] = 1;
  for (int i = 0; i < n; ++i) {
    for (int x : jump) {
      if (i + x < n)
        Dp[i + x] = (Dp[i + x] + Dp[i]) % MOD;
      else
        break;
    }
  }
  cout << Dp[n - 1] << "\\n";
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
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];
  }
  sort(l.begin(), l.end());
  sort(r.begin(), r.end());
  vector<int64_t> a(n + 1, 0);
  a[1] = 1;
  a[2] = -1;
  int64_t dpsum = 0;
  for (int i = 1; i < n; i++) {
    dpsum += a[i];
    dpsum %= 998244353;
    for (int j = 0; j < k; j++) {
      if (i + l[j] <= n) {
        a[i + l[j]] += dpsum;
        a[i + l[j]] %= 998244353;
      }
      if (i + r[j] + 1 <= n) {
        a[i + r[j] + 1] -= dpsum;
        a[i + r[j] + 1] %= 998244353;
      }
    }
  }
  uint64_t sum = accumulate(a.begin(), a.end(), 0LL) % 998244353;
  cout << sum << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  uint64_t n;
  int k;
  cin >> n >> k;
  vector<uint64_t> l(k);
  vector<uint64_t> r(k);
  for (int i = 0; i < k; i++) {
    cin >> l[i] >> r[i];
  }
  sort(l.begin(), l.end());
  sort(r.begin(), r.end());
  vector<uint64_t> a(n + 1, 0);
  a[1] = 1;
  bool bo;
  for (int i = l[0]; i <= n; i++) {
    bo = true;
    for (int j = 0; j < k; j++) {
      for (int d = l[j]; d <= r[j]; d++) {
        if (d < i) {
          a[i] += a[i - d];
          a[i] %= 998244353;
        } else {
          bo = false;
          break;
        }
      }
      if (!bo) {
        break;
      }
    }
  }
  cout << a[n] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define ll long long
#define pb push_back
#define all(x) (x).begin(), (x).end()
using namespace std;

int main() {
  ll i, j, n, k;
  cin >> n >> k;
  vector<ll> l(n), r(n);
  for (i = 0; i < n; i++)
    cin >> l[i] >> r[i];
  vector<ll> dp(n + 2, 0), dpsum(n + 2, 0);
  dp[1] = 1;
  dpsum[1] = 1;
  ll mod = 998244353;
  for (i = 2; i <= n; i++) {
    for (j = 0; j < k; j++) {
      ll li = i - r[j];
      ll ri = i - l[j];
      if (ri < 0)
        continue;
      li = max(li, 1ll);
      dp[i] += dpsum[ri] - dpsum[li - 1];
      dp[i] = (dp[i] + mod) % mod;
    }
    dpsum[i] = dpsum[i - 1] + dp[i];
    dpsum[i] %= mod;
  }
  cout << dp[n];
}


Error: TLE

Fixed submission:

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

const int N = (int)2e5 + 10;
ll dp[N];
vector<pair<int, int>> vec;

ll solve(int curr, int tar) {

  if (curr > tar) {
    return 0;
  }

  if (curr == tar) {
    return 1;
  }
  if (dp[curr] != -1) {
    return dp[curr];
  }
  ll res = 0;
  for (int i = 0; i < (int)vec.size(); i++) {

    for (int j = vec[i].first; j <= vec[i].second; j++) {
      res += solve(curr + j, tar);
      res %= mod;
    }
  }
  dp[curr] = (res + mod) % mod;
  return dp[curr];
}

int main() {
  int n, k;
  cin >> n >> k;
  memset(dp, -1, sizeof(dp));
  unordered_set<int> st;
  for (int i = 0; i < k; i++) {

    int l, r;
    cin >> l >> r;
    vec.push_back({l, r});
  }
  ll res = solve(1, n);
  res = (res + mod) % mod;
  cout << res << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <set>

#define N_MAX (2 * 100000)
#define MOD (998244353)

unsigned int dp[N_MAX];

int main() {
  int N, K;
  std::cin >> N >> K;

  int L[10], R[10];
  for (int i = 0; i < K; i++) {
    std::cin >> L[i] >> R[i];
  }
  dp[0] = 1;
  for (int i = 1; i < N; i++) {
    dp[i] = dp[i - 1];
    for (int k = 0; k < K; k++) {
      if (i - L[k] >= 0 && i - R[k] - 1 >= 0) {
        dp[i] += (MOD + dp[i - L[k]] - dp[i - R[k] - 1]) % MOD;
      } else if (i - L[k] >= 0) {
        dp[i] += dp[i - L[k]];
      }
      dp[i] %= MOD;
    }
  }
  std::cout << (MOD + dp[N - 1] - dp[N - 2]) % MOD << std::endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <set>

#define N_MAX (2 * 1000)
#define MOD (998244353)

unsigned int dp[N_MAX];

int main() {
  int N, K;
  std::cin >> N >> K;

  int L[10], R[10];
  for (int i = 0; i < K; i++) {
    std::cin >> L[i] >> R[i];
  }
  dp[0] = 1;
  for (int i = 1; i < N; i++) {
    dp[i] = dp[i - 1];
    for (int k = 0; k < K; k++) {
      if (i - L[k] >= 0 && i - R[k] - 1 >= 0) {
        dp[i] += (MOD + dp[i - L[k]] - dp[i - R[k] - 1]) % MOD;
      } else if (i - L[k] >= 0) {
        dp[i] += dp[i - L[k]];
      }
      dp[i] %= MOD;
    }
  }
  std::cout << (MOD + dp[N - 1] - dp[N - 2]) % MOD << std::endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

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

const long long MOD = 998244353;
long long N, K, l[11], r[11], memo[200010], prefix[400010];

void Input() {
  cin >> N >> K;
  for (long long i = 1; i <= K; i++) {
    cin >> l[i] >> r[i];
  }
}

void Solve() {
  memset(prefix, 0, sizeof prefix);
  prefix[N] = 1;
  memo[N] = 1;
  for (long long i = N - 1; i >= 1; i--) {
    long long total = 0;
    for (long long j = 1; j <= K; j++) {
      long long nl = l[j], nr = r[j];
      total += (prefix[i + nl] - prefix[i + nr + 1] + MOD) % MOD;
      total = total % MOD;
    }
    memo[i] = total;
    prefix[i] = (prefix[i + 1] + total) % MOD;
  }
  cout << memo[1] << '\\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  Input();
  Solve();
}

Error: 0

Fixed submission:

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

const long long MOD = 998244353;
long long N, K, l[11], r[11], memo[200010], prefix[400010];

void Input() {
  cin >> N >> K;
  for (long long i = 1; i <= K; i++) {
    cin >> l[i] >> r[i];
  }
}

void Solve() {
  memset(prefix, 0, sizeof prefix);
  prefix[N] = 1;
  memo[N] = 1;
  for (long long i = N - 1; i >= 1; i--) {
    long long total = 0;
    for (long long j = 1; j <= K; j++) {
      long long nl = l[i], nr = r[i];
      total += (prefix[i + nl] - prefix[i + nr + 1]) % MOD;
      total = total % MOD;
    }
    memo[i] = total;
    prefix[i] = (prefix[i + 1] + total) % MOD;
  }
  cout << memo[1] << '\\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  Input();
  Solve();
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <boost/dynamic_bitset.hpp>
using namespace std;
using u64 = uint64_t;
using s64 = int64_t;

int main() {
  u64 N, K;
  cin >> N >> K;

  u64 mod = 998244353;
  vector<pair<u64, u64>> S(K);

  for (u64 i = 0; i < K; i++) {
    u64 L, R;
    cin >> L >> R;
    S[i] = make_pair(L, R);
  }

  vector<u64> sum(N);
  sum[0] = 1;

  for (s64 i = 1; i < N; i++) {
    u64 dp = 0;

    for (const auto &lr : S) {
      s64 R = i - lr.first;
      if (R >= 0) {
        dp = (dp + sum[R]) % mod;
        s64 L = i - lr.second - 1;
        if (L >= 0) {
          dp = (dp >= sum[L]) ? dp - sum[L] : dp + mod - sum[L];
        }
      }
    }

    sum[i] = (sum[i - 1] + dp) % mod;
  }

  u64 ans = (sum[N - 1] >= sum[N - 2]) ? sum[N - 1] - sum[N - 2]
                                       : sum[N - 1] + mod - sum[N - 2];
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <boost/dynamic_bitset.hpp>
using namespace std;
using u64 = uint64_t;
using s64 = int64_t;

int main() {
  u64 N, K;
  cin >> N >> K;

  u64 mod = 998244353;
  deque<u64> S;
  vector<u64> dp(N);
  dp[0] = 1;

  for (u64 i = 0; i < K; i++) {
    u64 L, R;
    cin >> L >> R;
    for (u64 j = L; j <= R; j++) {
      S.push_back(j);
    }
  }

  for (u64 i = 0; i < N; i++) {
    u64 c = S.size();
    for (u64 k = 0; k < c; k++) {
      auto d = S.front();
      S.pop_front();
      if (i + d < N) {
        dp[i + d] = (dp[i + d] + dp[i]) % mod;
        S.push_back(d);
      }
    }
  }

  cout << dp[N - 1] << endl;
  return 0;
}
Problem p02549 (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++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1001001001;
const int MOD = 998244353;

ll dp[200005];
vector<P> opt;

ll solve(int n) {
  if (dp[n] != -1)
    return dp[n];
  if (n == 0)
    return dp[n] = 1;
  else if (n == 1) {
    int ret = 0;
    for (auto el : opt) {
      if (el.first == 1)
        ret = 1;
    }
    return dp[n] = ret;
  } else {
    ll ret = solve(n - 1);
    for (auto el : opt) {
      if (n - el.first >= 0) {
        if (n - el.second >= 0) {
          ret = ret + solve(n - el.first) - solve(n - el.second);
        } else {
          ret = ret + solve(n - el.first);
        }
      }
      ret = (ret + MOD) % MOD;
    }
    return dp[n] = ret;
  }
}

int main() {
  rep(i, 200005) dp[i] = -1;

  int n, k;
  cin >> n >> k;
  rep(i, k) {
    int l, r;
    cin >> l >> r;
    opt.emplace_back(l, r + 1);
  }
  cout << solve(n - 1) << endl;
  // rep(i, 5) cout << dp[i] << endl;

  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++)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1001001001;
const int MOD = 998244353;

ll dp[200005];
vector<int> opt;

ll solve(int n) {
  if (dp[n] != -1)
    return dp[n];
  if (n == 0)
    return dp[n] = 1;
  else {
    ll ret = 0;
    for (auto el : opt) {
      if (el <= n) {
        ret = (ret + solve(n - el)) % MOD;
      }
    }
    return dp[n] = ret;
  }
}

int main() {
  rep(i, 200005) dp[i] = -1;

  int n, k;
  cin >> n >> k;
  rep(i, k) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; j++)
      opt.push_back(j);
  }
  cout << solve(n - 1) << endl;
  // rep(i, 5) cout << dp[i] << endl;

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1LL << 60;
ll MOD = 998244353;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> l(k);
  vector<int> r(k);
  for (int i = 0; i < k; i++) {
    cin >> l[i] >> r[i];
  }
  vector<ll> dp(n + 5, 0);
  dp[1] = 1;
  vector<ll> chart(n + 5, 0);
  chart[1] = 1;
  for (int i = 2; i < n + 5; i++) {
    for (int j = 0; j < k; j++) {
      if (i - l[j] > 0) {
        ll left = max(1, min(i - r[j], i - 1)), right = i - l[j];
        dp[i] = max(dp[i], dp[i] + chart[right] - chart[left - 1]) % MOD;
      }
    }
    chart[i] = chart[i - 1] + dp[i];
  }
  cout << dp[n] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1LL << 60;
ll MOD = 998244353;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> l(k);
  vector<int> r(k);
  for (int i = 0; i < k; i++) {
    cin >> l[i] >> r[i];
  }
  vector<ll> dp(n + 5, 0);
  dp[1] = 1;
  for (int i = 1; i < n + 5; i++) {
    for (int j = 0; j < k; j++) {
      for (int s = l[j]; s <= min(r[j], n - i); s++) {
        dp[i + s] = (dp[i + s] + dp[i]) % MOD;
      }
    }
  }
  cout << dp[n] << endl;
  return 0;
}