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

// Tonight's the night and it is going to happen again and again.
// this solution was copied from youtube solution but now i understand this
// fully as of this date
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define T                                                                      \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define mod 998244353
#define inf 1000000000000000000
#define mem(name, val) memset(name, val, sizeof(name))
#define f(n) for (ll i = 0; i < n; i++)
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define endl "\\n"
ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n, k;
  cin >> n >> k;
  ll l[k], r[k];
  f(k) { cin >> l[i] >> r[i]; }
  ll dp[n + 1];
  mem(dp, 0);
  ll dpsum[n + 1];
  mem(dpsum, 0);
  dpsum[1] = 1;
  dp[1] = 1;
  for (ll i = 2; i <= n; i++) {
    for (ll 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];
      if (dp[i] < 0) {
        dp[i] += mod;
      } else {
        dp[i] %= mod;
      }
    }
    dpsum[i] = (dpsum[i - 1] + dp[i]) % mod;
  }
  // for(auto t:dp){cout<<t<<" ";}
  cout << dp[n] % mod << endl;
}

Error: TLE

Fixed submission:

// Tonight's the night and it is going to happen again and again.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mp make_pair
#define pb push_back
#define T                                                                      \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define mod 998244353
#define inf 1000000000000000000
#define mem(name, val) memset(name, val, sizeof(name))
#define f(n) for (ll i = 0; i < n; i++)
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define endl "\\n"
ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  }
  return gcd(b, a % b);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll n, l, r, k;
  cin >> n >> k;
  set<ll> s;
  f(k) {
    cin >> l >> r;
    for (int i = l; i <= r; i++) {
      s.insert(i);
    }
  }
  ll wt = n - 1;
  ll dp[n + 3];
  mem(dp, 0);
  dp[0] = 1;
  for (ll i = 1; i <= wt; i++) {
    for (auto it = s.begin(); it != s.end(); it++) {
      // cout<<*it<<endl;
      if (*it <= i) {
        dp[i] += dp[i - (*it)];
        dp[i] %= mod;
      } else {
        break;
      }
    }
  }
  // for(auto t:dp){cout<<t<<endl;}
  cout << dp[wt] % mod;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  long long N, K;
  long long l, r;
  long long ans;
  long long m = 998244353;
  std::cin >> N >> K;
  vector<vector<long long>> kumiawase(K + 1, vector<long long>(N, 0));
  kumiawase[0][0] = 1;
  vector<long long> n_k_list(K, 0);

  vector<vector<long long>> list;
  for (int i = 0; i < K; i++) {
    std::cin >> l >> r;
    list.push_back({l, r});
    n_k_list[i] = r - l + 1; // 各連続区間の数
  }

  // for(int i = 0; i < list.size(); i++){
  //     std::cout << list[i] << " ";
  // }
  // std::cout << "\\n";
  // 動的計画法で解く。最悪NNになりそうだけど..
  // NNになった。。
  // Kこのグループごとに行を設ける。

  for (long long i = 1; i < N; i++) {
    //    std::cout << i << "\\n";
    for (long long j = 0; j < K; j++) {
      // Kごとにループを回す。
      // iよりうしろのn_kこの数の合計を出す。
      if (i <= n_k_list[j]) {
        kumiawase[j + 1][i] = kumiawase[0][i - 1] + kumiawase[j + 1][i - 1];
        kumiawase[j + 1][i] = kumiawase[j + 1][i] % m;
      } else {
        kumiawase[j + 1][i] = kumiawase[0][i - 1] + kumiawase[j + 1][i - 1] -
                              kumiawase[0][i - n_k_list[j] - 1];
        kumiawase[j + 1][i] = kumiawase[j + 1][i] % m;
      }
      // 列埋め完了
      // 最後に0行目を算出する。
      if (i - list[j][0] >= 0) {
        // レフト前のインデックスが欄外じゃなければそのいっこ右の値を足す。
        kumiawase[0][i] =
            kumiawase[0][i] + kumiawase[j + 1][i - list[j][0] + 1];
        kumiawase[0][i] = kumiawase[0][i] % m;
      } else {
      }
    }
    //        std::cout << kumiawase[i] << " ";
  }

  if (kumiawase[0][N - 1] < 0) {
    ans = kumiawase[0][N - 1] + m;
  } else {
    ans = kumiawase[0][N - 1];
  }

  std::cout << ans << "\\n";

  // for(int j = 0; j < K + 1; j++){
  //     for(int i = 0; i < 6; i++){
  //         std::cout << kumiawase[j][i] << " ";
  //     }
  //     std::cout << "\\n";
  // }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  long long N, K;
  long long l, r;
  long long m = 998244353;
  std::cin >> N >> K;
  vector<long long> kumiawase(N, 0);
  kumiawase[0] = 1;

  vector<long long> list(0);
  for (int i = 0; i < K; i++) {
    std::cin >> l >> r;
    for (long long j = l; j <= r; j++) {
      list.push_back(j);
    }
  }
  // for(int i = 0; i < list.size(); i++){
  //     std::cout << list[i] << " ";
  // }
  // std::cout << "\\n";
  // 動的計画法で解く。最悪NNになりそうだけど..

  for (long long i = 1; i < N; i++) {
    //    std::cout << i << "\\n";
    for (long long j = 0; j < list.size(); j++) {
      if (i - list[j] >= 0) {
        kumiawase[i] = kumiawase[i] + kumiawase[i - list[j]];
        kumiawase[i] = kumiawase[i] % m;
      }
    }
    //        std::cout << kumiawase[i] << " ";
  }

  std::cout << kumiawase[N - 1];
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1e9;
const ll MOD = 998244353;
#define rep1(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, k, n) for (ll i = k; i < (n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> dp(n + 1, 0), sum(n + 1, 0), l(k), r(k);
  rep1(i, k) cin >> r[i] >> l[i];
  rep2(i, 1, n + 1) {
    if (i == 1) {
      sum[i] = 1;
      continue;
    }
    rep1(j, k) {
      if (i - r[j] > 0) {
        if (i - l[j] > 0)
          dp[i] += (sum[i - r[j]] - sum[i - l[j] - 1] % MOD + MOD) % MOD;
        else
          dp[i] += sum[i - r[j]];
      }
    }
    dp[i] %= MOD;
    sum[i] = (sum[i - 1] + dp[i]) % MOD;
  }
  cout << dp[n] << endl;
  // printf("%.12f", ans);
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1e9;
const ll MOD = 998244353;
#define rep1(i, n) for (ll i = 0; i < (n); i++)
#define rep2(i, k, n) for (ll i = k; i < (n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll n, k;
  cin >> n >> k;
  map<ll, ll> m;
  vector<ll> d;
  rep1(i, k) {
    ll l, r;
    cin >> l >> r;
    for (ll i = l; i < (r + 1); i++)
      m[i]++;
  }
  for (auto x : m)
    d.push_back(x.first);
  vector<ll> dp(3 * n, 0);
  dp[1] = 1;
  for (ll i = 1; i < (n); i++) {
    for (ll j = 0; j < (d.size()); j++) {
      dp[i + d[j]] += dp[i];
      dp[i + d[j]] %= MOD;
    }
  }
  cout << dp[n] << endl;
  // printf("%.12f", ans);
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

#define MOD 998244353LL

using namespace std;

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

  vector<pair<long long, long long>> d(K);
  for (int i = 0; i < K; ++i) {
    cin >> d[i].first >> d[i].second;
  }

  vector<long long> dp(N, 0);
  vector<long long> dp_sum(N, 0);
  dp[0] = 1;
  dp_sum[0] = 1;
  for (int i = 1; i < N; ++i) {
    for (int j = 0; j < K; ++j) {
      if (i - d[j].second <= 0 && i - d[j].first >= 0) {
        dp[i] += dp_sum[i - d[j].first];
      } else if (i - d[j].second > 0) {
        dp[i] += (dp_sum[i - d[j].first] - dp_sum[i - d[j].second - 1]);
      }
      dp[i] %= MOD;
    }
    dp_sum[i] = dp_sum[i - 1] + dp[i];
  }
  cout << dp[N - 1] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>

#define MOD 998244353LL

using namespace std;

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

  vector<pair<long long, long long>> d(K);
  for (int i = 0; i < K; ++i) {
    cin >> d[i].first >> d[i].second;
  }

  vector<long long> dp(N, 0);
  dp[0] = 1;
  for (int i = 1; i < N; ++i) {
    for (int j = 0; j < K; ++j) {
      for (int k = d[j].first; k <= d[j].second && i - k >= 0; ++k) {
        dp[i] += dp[i - k];
        dp[i] %= MOD;
      }
    }
  }
  cout << dp[N - 1] << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int f[200010], l[20], r[20], qzh[200010];

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

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

  f[1] = qzh[1] = 1;

  for (int i = 2; i <= n; i++) {
    for (int j = 1; j <= k; j++) {
      f[i] += (qzh[max(i - l[j], 0ll)] - qzh[max(i - r[j] - 1, 0ll)] +
               998244353 + 998244353) %
              998244353;
      f[i] %= 998244353;
    }
    qzh[i] = qzh[i - 1] + f[i];
    qzh[i] %= 998244353;
  }

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


Error: TLE

Fixed submission:

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

inline int read() {
  int s = 0, w = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = s * 10 + ch - '0', ch = getchar();
  return s * w;
}

inline bool read(int &a) {
  int s = 0, w = 1;
  char ch = getchar();
  if (ch == EOF) {
    return false;
  }
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    s = s * 10 + ch - '0', ch = getchar();
  a = s * w;
  return true;
}

inline void write(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
}

inline void writeln(int x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
  puts("");
}

const int mod = 998244353;

int l[20], r[20];
set<int> s;
int b[200010];

int dp[200010];

int a[200010];

signed main() {
  int n, k;
  read(n), read(k);

  for (int i = 1; i <= k; i++) {
    read(l[i]), read(r[i]);
    for (int j = l[i]; j <= r[i]; j++)
      b[j] = 1;
  }

  int cnt = 0;

  for (int i = 1; i <= n; i++)
    if (b[i] == 1)
      a[++cnt] = i;

  dp[1] = 1;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= cnt; j++) {
      if (dp[i - a[j]] > 0 && i - a[j] >= 0)
        dp[i] = (dp[i] + dp[i - a[j]]) % mod;
      if (i - a[j] < 0)
        break;
    }
  }

  writeln(dp[n]);
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> L(K), R(K);
  rep(i, 0, K) cin >> L[i] >> R[i];
  vector<ll> dp(N + 1, 0);
  dp[0] = 1;
  dp[1] = -1;
  rep(i, 0, N) {
    dp[i] = ((dp[i] % MOD) + MOD) % MOD;
    dp[i + 1] = (dp[i + 1] + dp[i]) % MOD;
    rep(j, 0, K) {
      if (i + L[j] < N)
        dp[i + L[j]] += dp[i];
      if (i + R[j] + 1 < N)
        dp[i + R[j] + 1] -= dp[i];
    }
  }
  cout << dp[N - 1] << endl;
}

Error: 0

Fixed submission:

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> L(K), R(K);
  rep(i, 0, K) cin >> L[i] >> R[i];
  vector<ll> dp(N + 1, 0);
  dp[0] = 1;
  dp[1] = -1;
  rep(i, 0, N) {
    dp[i] = ((dp[i] % MOD) + MOD) % MOD;
    dp[i + 1] = (dp[i + 1] + dp[i]) % MOD;
    rep(j, 0, K) {
      dp[i + L[j]] += dp[i];
      dp[i + R[j] + 1] -= dp[i];
    }
  }
  cout << dp[N - 1] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef std::pair<int, int> pair2;
typedef std::pair<int, pair2> pair3;
const ll INF = 1LL << 60;
const ll NUM = 10e8 + 7;

int main() {
  ll N, K;
  cin >> N >> K;
  ll L[K], R[K];
  for (ll i = 0; i < K; i++) {
    cin >> L[i] >> R[i];
  }
  vector<ll> dp(N + 2, 0);
  vector<ll> sumdp(N + 2, 0);
  dp[0] = 0;
  dp[1] = 1;
  sumdp[0] = 0;
  sumdp[1] = 1;
  for (ll i = 1; i < N + 1; i++) {

    for (ll j = 0; j < K; j++) {
      dp[i] = dp[i] % 998244353;
      // cout<<i<<endl;
      if (i - L[j] <= 0) {
        continue;
      }
      if (i - R[j] - 1 <= 0) {
        dp[i] = dp[i] + (sumdp[i - L[j]] - sumdp[0]);
        // cout<<i<<" "<<dp[i]<<endl;
        continue;
      }
      dp[i] = dp[i] + (sumdp[i - L[j]] - sumdp[i - R[j] - 1]);
      //      if(i+L[i]<=N)dp[i+L[j]]=dp[i+L[j]]+dp[i];
      //      if(i+R[i]<=N)dp[i+R[j]+1]=dp[i+R[j]+1]-dp[i];
    }
    dp[i] = dp[i] % 998244353;
    if (dp[i] < 0)
      dp[i] = dp[i] + 998244353;
    sumdp[i] = sumdp[i - 1] + dp[i];
    sumdp[i] = sumdp[i] % 998244353;
  }

  // for(ll i=1;i<N+1;i++)
  // cout<<dp[i]<<" "<<sumdp[i]<<endl;
  cout << dp[N] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef std::pair<int, int> pair2;
typedef std::pair<int, pair2> pair3;
const ll INF = 1LL << 60;
const ll NUM = 10e8 + 7;

int main() {
  ll N, K;
  cin >> N >> K;
  ll L[K], R[K];
  for (ll i = 0; i < K; i++) {
    cin >> L[i] >> R[i];
  }

  vector<ll> dp(N + 1, 0);
  dp[1] = 1;

  for (ll i = 1; i < N; i++) {            // dpの数
    for (ll j = 0; j < K; j++) {          // L[j],K[j]を使用
      for (ll m = L[j]; m <= R[j]; m++) { // lが飛べる数
        if (i + m <= N) {
          dp[i + m] = dp[i + m] + dp[i];
          dp[i + m] = dp[i + m] % 998244353;
        }
      }
    }
  }

  cout << dp[N] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
#define ll long long
using namespace std;
constexpr auto mod = 998244353;
ll dp[200000 * 2 + 5];
vector<pair<ll, ll>> v;
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    int a, b;
    cin >> a >> b;
    v.emplace_back(make_pair(a, b));
  }
  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++) {
      dp[i + v[j].first] = dp[i + v[j].first] + dp[i];
      dp[i + v[j].second + 1] = dp[i + v[j].second + 1] - dp[i];
    }
  }
  cout << dp[n] << endl;
  // getchar();
  // getchar();
  return 0;
}

Error: 0

Fixed submission:

// 差分dp
//  #include<iostream>
//  #include<algorithm>
//  #include<vector>
//  #include<utility>
//  using namespace std;
//  constexpr auto mod=998244353;
//  vector<pair<int,int>> p;
//  bool sieve[200000+5];
//  vector<int> step;
//  int dp[200000+5];
//  int main(){
//      int n,k;
//      cin>>n>>k;
//      for(int i=1;i<=k;i++){
//          int a,b;
//          cin>>a>>b;
//          for(int j=a;j<=b;j++){
//              if(!sieve[j]){
//                  step.push_back(j);
//                  sieve[j]=true;
//              }
//          }
//      }
//      dp[1]=1;
//      for(int i=1;i<=n;i++){
//          for(int j=0;j<step.size();j++){
//              if(i-step[j]>0){
//                  dp[i]=(dp[i]+dp[i-step[j]])%mod;
//              }
//          }
//      }
//      cout<<dp[n]<<endl;
//      getchar();
//      getchar();
//      return 0;
//  }
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
#define ll long long
using namespace std;
constexpr auto mod = 998244353;
ll dp[200000 + 5];
vector<pair<ll, ll>> v;
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    int a, b;
    cin >> a >> b;
    v.emplace_back(make_pair(a, b));
  }
  dp[1] = 1;
  dp[2] = -1;
  for (int i = 1; i <= n; i++) {
    dp[i] = ((dp[i] % mod + dp[i - 1]) % mod + mod) % mod;
    for (int j = 0; j < k; j++) {
      dp[i + v[j].first] = dp[i + v[j].first] + dp[i];
      dp[i + v[j].second + 1] = dp[i + v[j].second + 1] - dp[i];
    }
  }
  cout << dp[n] << endl;
  getchar();
  getchar();
  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++)
#define INF 1e9
#define PI 3.14159265359
#define MOD 998244353
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define F first
#define S second
typedef long long ll;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
// isPrime
// modpow modinv
// getDigit
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  cout << fixed << setprecision(10);
  int n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  rep(i, k) { cin >> l[i] >> r[i]; }
  vector<int> dp(n, 0);
  dp[0] = 1;
  rep(i, n) {
    rep(j, k) {
      int d = l[j];
      int e = r[j];
      // cout << "d" << d << endl;
      if (i + d < n) {
        dp[i + d] += dp[i];
        dp[i + d] = dp[i + d] % MOD;
      }
      if (i + e + 1 < n) {
        dp[i + e + 1] = dp[i + e + 1] - dp[i] + MOD;
        dp[i + e + 1] = dp[i + e + 1] % MOD;
      }
    }
    if (i >= 1) {
      dp[i + 1] = dp[i] + dp[i + 1];
      dp[i + 1] = dp[i + 1] % MOD;
    }
    /*
    cout << "n" << i  << endl;
    rep(k,n){
      cout << dp[k] << endl;
    }
    */
  }
  cout << dp[n - 1] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF 1e9
#define PI 3.14159265359
#define MOD 998244353
#define ALL(v) v.begin(), v.end()
#define ALLR(v) v.rbegin(), v.rend()
#define F first
#define S second
typedef long long ll;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
// isPrime
// modpow modinv
// getDigit
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
int main() {
  cout << fixed << setprecision(10);
  int n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  rep(i, k) { cin >> l[i] >> r[i]; }
  vector<int> dp(n, 0);
  dp[0] = 1;
  rep(i, n) {
    rep(j, k) {
      for (int d = l[j]; d <= r[j]; d++) {
        int next = i + d;
        if (next < n && next >= 0) {
          dp[i + d] += dp[i];
          dp[i + d] = dp[i + d] % MOD;
        }
      }
    }
  }
  cout << dp[n - 1] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(a) (a).begin(), (a).end()
const ll MOD = 998244353;

int main() {
  int N, K, L[10], R[10];
  cin >> N >> K;

  REP(i, K) {
    cin >> L[i] >> R[i];
    R[i]++;
  }

  ll dp[200001] = {};
  dp[0] = 1;
  FOR(i, 1, N) {
    dp[i] += dp[i - 1];
    dp[i] %= MOD;
    REP(j, K) {
      if (i + L[j] <= N) {
        dp[i + L[j]] += dp[i];
        dp[i + L[j]] %= MOD;
      }
      if (i + R[j] <= N) {
        dp[i + R[j]] -= dp[i] - MOD;
        dp[i + R[j]] %= MOD;
      }
    }
  }
  cout << dp[N] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(a) (a).begin(), (a).end()
const ll MOD = 998244353;

int main() {
  int N, K, L[10], R[10];
  cin >> N >> K;

  REP(i, K) {
    cin >> L[i] >> R[i];
    R[i]++;
  }

  ll dp[200001] = {};
  dp[0] = 1;
  FOR(i, 1, N) {
    dp[i] += dp[i - 1];
    dp[i] %= MOD;
    REP(j, K) {
      dp[i + L[j]] += dp[i];
      dp[i + L[j]] %= MOD;
      dp[i + R[j]] -= dp[i];
    }
  }
  cout << dp[N] << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll long long
#define all(x) (x).begin(), (x).end()
#define print(x)                                                               \\
  if (x)                                                                       \\
    cout << "YES\\n";                                                           \\
  else                                                                         \\
    cout << "NO\\n";
#define f(i, x, y) for (ll i = x; i <= y; i++)
#define rf(i, x, y) for (ll i = x; i >= y; i--)
#define pb push_back
#define F first
#define S second
#define mod 998244353
using namespace std;
ll dp[200000 + 1];
unsigned ll pre[200000 + 1];
int main() {
  // #ifndef HP
  //   freopen("in.txt", "r", stdin);
  //   freopen("out.txt", "w", stdout);
  // #endif

  fastio;
  int t = 1;
  // cin >> t;
  while (t--) {
    ll n, k;
    cin >> n >> k;
    vector<pair<ll, ll>> v;
    f(i, 0, k - 1) {
      ll p, q;
      cin >> p >> q;
      v.pb({p, q});
    }
    dp[0] = 0;
    dp[1] = 1;
    pre[0] = 0;
    pre[1] = 1;
    for (ll i = 2; i <= n; i++) {
      for (ll j = 0; j < v.size(); j++) {
        {
          ll x = max(0LL, i - v[j].F);
          ll y = max(0LL, i - v[j].S - 1);
          dp[i] = (dp[i] + pre[x] - pre[y]) % mod;
        }
      }
      pre[i] = (pre[i - 1] + dp[i]);
    }
    cout << dp[n] << endl;
  }
}


Error: TLE

Fixed submission:

using namespace std;
#include <bits/stdc++.h>
#define FASTIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll long long
#define debug(x) cout << #x << " = " << x << endl
#define MOD 998244353
int dp[1000001];
void solve() {
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> v(k);
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    v[i] = {a, b};
  }
  sort(v.begin(), v.end());
  dp[1] = 1;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      if (v[j].first > i)
        break;
      else {
        for (int x = v[j].first; x <= v[j].second; x++) {
          if (x > i)
            break;
          dp[i] = (dp[i] + dp[i - x]) % MOD;
        }
      }
    }
  }
  cout << dp[n] << endl;
}
int main() {

  FASTIO
  long long int t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}