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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;
using ll = long long;
using P = pair<int, int>;

template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  for (int i = 0; i < v.size(); i++) {
    if (i)
      os << ", ";
    os << v[i];
  }
  os << "}";
  return os;
}

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

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

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < k; j++) {
      dp[i] += acc[max(0, i - l[j] + 1)] - acc[max(0, i - r[j])];
      dp[i] = (dp[i] + mod) % mod;
      acc[i + 1] = acc[i] + dp[i];
      acc[i + 1] %= mod;
    }
  }
  cout << dp[n - 1] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;
using ll = long long;
using P = pair<int, int>;

template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  for (int i = 0; i < v.size(); i++) {
    if (i)
      os << ", ";
    os << v[i];
  }
  os << "}";
  return os;
}

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

  vector<ll> f(n, 0);
  f[0] = 1;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k; j++) {
      for (int s = l[j]; s <= r[j]; s++) {
        if (i + s >= n)
          continue;
        f[i + s] += f[i];
        f[i + s] %= 998244353;
      }
    }
  }
  cout << f[n - 1] << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

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

int N, K;
int L[15], R[15];
long long imos[202020];
long long now = 1;

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

  for (int i = 1; i <= N; i++) {
    now += imos[i];
    now %= 998244353LL;
    for (int j = 0; j < K; j++) {
      if (i + L[j] < 202020) {
        imos[i + L[j]] += now;
      }
      if (i + R[j] + 1 < 202020) {
        imos[i + R[j] + 1] -= now;
      }
    }
    if (i == N) {
      while (imos[N] < 0LL) {
        imos[N] += 998244353LL;
      }
      cout << imos[N] % 998244353LL << endl;
    }
  }
}

Error: 0

Fixed submission:

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

int N, K;
int L[15], R[15];
long long imos[202020];
long long now = 1;

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

  for (int i = 1; i <= N; i++) {
    now += imos[i];
    now %= 998244353LL;
    for (int j = 0; j < K; j++) {
      if (i + L[j] < 202020) {
        imos[i + L[j]] += now;
      }
      if (i + R[j] < 202020) {
        imos[i + R[j] + 1] -= now;
      }
    }
    if (i == N) {
      while (imos[N] < 0LL) {
        imos[N] += 998244353LL;
      }
      cout << imos[N] % 998244353LL << endl;
    }
  }
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define V vector
typedef long long ll;
typedef unsigned long long ull;

typedef V<int> vi;
typedef V<ll> vll;
typedef V<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define f(i, a) for (int i = 0; i < a; i++)
#define fll(i, a) for (ll i = 0; i < a; i++)
#define forab(i, a, b) for (int i = a; i < b; i++)
#define prec(x) cout << fixed << setprecision(x)
#define ff first
#define ss second

#define pb push_back
#define mp make_pair
#define numberofdigits(x) floor(log10(x)) + 1
#define all(x) (x).begin(), (x).end()

const ll mod = 998244353;

ll binpow(ll a, long long b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a);
    a = (a * a);
    b >>= 1;
  }
  return res;
}
const int N = 1e6 + 5;

void solve() {

  int n, k;
  cin >> n >> k;

  int dp[n + 5], pref[n + 5];
  memset(dp, 0, sizeof(dp));
  memset(pref, 0, sizeof(pref));
  dp[1] = 1, pref[1] = 1;

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

  for (int i = 2; i <= n; i++) {

    for (int j = 0; j < k; j++) {

      if (i <= l[j])
        continue;
      int L = l[j];
      int R = min(r[j], i - 1);

      dp[i] =
          (dp[i] % mod + ((pref[i - L] - pref[i - R - 1]) % mod + mod) % mod) %
          mod;
    }

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

  cout << dp[n] << "\\n";
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t = 1;
  // cin >> t;

  while (t--)
    solve();
  return 0;
}


Error: TLE

Fixed submission:

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

#define V vector
typedef long long ll;
typedef unsigned long long ull;

typedef V<int> vi;
typedef V<ll> vll;
typedef V<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define f(i, a) for (int i = 0; i < a; i++)
#define fll(i, a) for (ll i = 0; i < a; i++)
#define forab(i, a, b) for (int i = a; i < b; i++)
#define prec(x) cout << fixed << setprecision(x)
#define ff first
#define ss second

#define pb push_back
#define mp make_pair
#define numberofdigits(x) floor(log10(x)) + 1
#define all(x) (x).begin(), (x).end()

const ll mod = 998244353;

ll binpow(ll a, long long b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a);
    a = (a * a);
    b >>= 1;
  }
  return res;
}
const int N = 1e6 + 5;

void solve() {

  int n, k;
  cin >> n >> k;

  vector<int> a;

  for (int i = 0; i < k; i++) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; j++)
      a.pb(j);
  }

  ll dp[n + 1];
  memset(dp, 0, sizeof(dp));
  int sz = a.size();
  dp[0] = 1, dp[1] = 1;

  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < sz; j++) {

      if (i - a[j] >= 1)
        dp[i] = (dp[i] % mod + dp[i - a[j]] % mod) % mod;
    }
  }

  cout << dp[n] << "\\n";
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t = 1;
  // cin >> t;

  while (t--)
    solve();
  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++)
typedef long long ll;
using namespace std;
int const MOD = 998244353;

int dp[300000];

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

  vector<pair<int, int>> lr;
  rep(i, k) {
    int l, r;
    cin >> l >> r;
    lr.push_back({l, r + 1});
  }

  dp[0] = 1;
  dp[1] = -1;
  rep(i, n) {
    if (i > 0)
      dp[i] = (dp[i] + dp[i - 1]) % MOD;
    rep(j, k) {
      int l, r;
      tie(l, r) = lr[j];
      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] + MOD) % MOD;
    }
  }

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


Error: TLE

Fixed submission:

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

int dp[300000];

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

  vector<int> dist;
  rep(i, k) {
    int l, r;
    cin >> l >> r;
    for (int i = l; i <= r; i++) {
      dist.push_back(i);
    }
  }

  sort(dist.begin(), dist.end());

  dp[0] = 1;
  rep(i, n) {
    if (i + dist[0] > n)
      break;
    for (int d : dist) {
      if (i + d > n)
        break;
      dp[i + d] = (dp[i + d] + dp[i]) % MOD;
    }
  }

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

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> pll;
typedef vector<pair<ll, ll>> vpll;
#define kill                                                                   \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define pb push_back
#define fo(i, n) for (ll i = 0; i < n; i++)
#define rev(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(c) c.begin(), c.end()
#define go(c, it) for (auto it = (c).begin(); it != (c).end(); it++)
#define fi first
#define se second
#define MOD 998244353
#define Boost()                                                                \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
#define MOD1 1000000007
ll power(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % MOD;
    }
    a = (a * a) % MOD;
    b >>= 1LL;
  }
  return res;
}
ll fermat_inv(ll y) { return power(y, MOD - 2); }
ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }
#define oo LLONG_MAX
const ll dx[] = {-1, 0, 1, 0, -1, 1, 1, -1};
const ll dy[] = {0, 1, 0, -1, 1, 1, -1, -1};

void solve() {
  ll n, k;
  cin >> n >> k;
  vl l(k), r(k);
  fo(i, k) { cin >> l[i] >> r[i]; }
  ll ans = 0;
  ll dp[n + 1] = {0};
  dp[1] = 1;
  dp[2] = -1;
  for (ll i = 1; i <= n; i++) {
    dp[i] = (dp[i] + dp[i - 1]) % MOD;
    fo(j, k) {
      ll left = l[j];
      ll right = r[j] + 1;
      if (i + left <= n)
        dp[i + left] = (dp[i + left] + dp[i]) % MOD;
      if (i + right <= n)
        dp[i + right] = (dp[i + right] - dp[i] + MOD) % MOD;
    }
  }
  cout << dp[n];
}

int main() {
  Boost();
  ll T = 1;
  // cin>>T;
  while (T--) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long

using namespace std;

struct Node {
  int data;
  struct Node *left;
  struct Node *right;
  Node(int x) {
    data = x;
    left = NULL;
    right = NULL;
  }
};
#define Node struct Node

ll modpow(ll a, ll n) {
  ll m = 1e9 + 7;
  if (n == 0)
    return 1;
  ll y = modpow(a, n / 2) % m;
  if (n % 2 == 0)
    return (y * y) % m;
  return (((y * y) % m) * a) % m;
}

ll dp[200005];
ll pro(int i, vector<int> &v) {
  if (i == 1)
    return 1;
  if (i <= 0)
    return 0;
  if (dp[i] != -1)
    return dp[i];
  ll m = 998244353;
  ll ans = 0;
  for (int j = 0; j < v.size(); j++) {
    if (v[j] >= i)
      break;
    ans += pro(i - v[j], v);
    ans = ans % m;
  }
  return dp[i] = ans;
}

int main() {
  int t = 1;
  // cin>>t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    vector<int> v;
    for (int i = 0; i < k; i++) {
      int x, y;
      cin >> x >> y;
      for (ll j = x; j <= y; j++)
        v.push_back(j);
    }
    sort(v.begin(), v.end());
    memset(dp, -1, sizeof(dp));
    cout << pro(n, v) << endl;
  }
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

const ll mod = 998244353;

int main() {
  int n;
  cin >> n;
  int k;
  cin >> k;
  vector<pair<int, int>> dic(k);
  rep(i, k) cin >> dic[i].first >> dic[i].second;
  vector<ll> dp(n), sdp(n + 1);
  dp[0] = 1;
  sdp[1] = 1;
  for (int i = 1; i < n; i++) {
    for (auto p : dic) {
      int l = max(0, i - p.second);
      int r = max(0, i - p.first + 1);
      (dp[i] += (mod + sdp[r] - sdp[l]) % mod) %= mod;
    }
    (sdp[i + 1] = sdp[i] + dp[i]) %= mod;
  }
  cout << dp[n - 1] << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

const ll mod = 998244353;
int n;
vector<int> v;

int main() {
  cin >> n;
  int k;
  cin >> k;
  vector<int> dic(n + 1);
  rep(i, k) {
    int l, r;
    cin >> l >> r;
    dic[l]++;
    if (r + 1 <= n)
      dic[r + 1]--;
  }
  rep(i, n) { dic[i + 1] += dic[i]; }
  for (int i = 1; i <= n; i++) {
    if (dic[i] > 0)
      v.push_back(i);
  }
  vector<ll> dp(n + 1);
  dp[1] = 1;
  for (int i = 1; i < n; i++) {
    rep(j, v.size()) {
      if (i + v[j] <= n)
        (dp[i + v[j]] += dp[i]) %= mod;
      else
        break;
    }
  }
  cout << dp[n] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h> //AtCoder
#include <iostream>      //Codeforces
#include <vector>        //Codeforces

#define int long long
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repeq(i, a, b) for (int i = a; i <= b; i++)
#define Vi vector<int>
#define VVi vector<Vi>
#define Vs vector<string>
#define Vc vector<char>
#define cYES cout << "YES" << endl
#define cNO cout << "NO" << endl
#define cYes cout << "Yes" << endl
#define cNo cout << "No" << endl
#define cyes cout << "yes" << endl
#define cno cout << "no" << endl

using namespace std;
/****************************** START ******************************/

signed main() {
  int N, K;
  cin >> N >> K;
  Vi L(K), R(K);
  map<int, int> S, a, f;
  rep(i, 0, K) { cin >> L.at(i) >> R.at(i); }
  int mod = 998244353;

  f[1] = 1;
  f[2] = -1;
  repeq(i, 1, N) {
    f[i] = (f[i] % mod + mod) % mod;
    f[i + 1] += f[i];
    rep(j, 0, K) {
      if (i + L.at(j) <= N) {
        f[i + L.at(j)] += f[i];
      }
      if (i + R.at(j) + 1 <= N) {
        f[i + R.at(j) + 1] -= f[i];
      }
    }
  }

  cout << f[N];
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h> //AtCoder
#include <iostream>      //Codeforces
#include <vector>        //Codeforces

#define int long long
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repeq(i, a, b) for (int i = a; i <= b; i++)
#define Vi vector<int>
#define VVi vector<Vi>
#define Vs vector<string>
#define Vc vector<char>
#define cYES cout << "YES" << endl
#define cNO cout << "NO" << endl
#define cYes cout << "Yes" << endl
#define cNo cout << "No" << endl
#define cyes cout << "yes" << endl
#define cno cout << "no" << endl

using namespace std;
/****************************** START ******************************/

signed main() {
  int N, K;
  cin >> N >> K;
  Vi L(K), R(K);
  map<int, int> S, ans;
  rep(i, 0, K) { cin >> L.at(i) >> R.at(i); }
  int mod = 998244353;

  repeq(i, 1, N) {
    rep(j, 0, K) {
      if (i >= L.at(j) && i <= R.at(j)) {
        S[i] = 1;
      }
    }
  }
  repeq(j, 1, N) {
    if (S[j]) {
      if (1 + j <= N) {
        ans[1 + j] = 1;
      }
    }
  }
  repeq(i, 2, N) {
    repeq(j, 1, N) {
      if (S[j]) {
        if (i + j <= N) {
          ans[i + j] += ans[i];
          ans[i + j] %= mod;
        }
      }
    }
  }
  cout << ans[N];
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, k;
  cin >> n >> k;
  const int MOD = 998244353;
  vector<int> dp(n + 10, 0), a(n + 10, 0);
  vector<int> left(k), right(k);
  rep(i, 0, k) cin >> left[i] >> right[i];
  dp[1] = 1;
  a[1] = 1;
  rep(i, 2, n + 10) {
    rep(j, 0, k) {
      int r = i - left[j];
      int l = max(i - right[j], 1);
      if (r < 0)
        continue;
      dp[i] = (dp[i] + a[r] - a[l - 1]) % MOD;
      if (dp[i] < 0)
        dp[i] += MOD;
    }
    a[i] = (a[i - 1] + dp[i]) % MOD;
  }
  cout << dp[n] << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, k, mod = 998244353;
  cin >> n >> k;
  vector<int> s;
  rep(i, 0, k) {
    int l, r;
    cin >> l >> r;
    rep(j, l, r + 1) s.push_back(j);
  }
  sort(s.begin(), s.end());
  vector<bool> seen(n + 1, false);
  vector<ll> dp(n + 1);
  dp[1] = 1;
  seen[1] = 1;
  bool f = false;
  rep(i, 1, n + 1) {
    if (seen[i] == 0)
      continue;
    rep(j, 0, s.size()) {
      if (i + s[j] > n) {
        break;
        if (j == 0)
          f = true;
      }
      dp[i + s[j]] += dp[i];
      dp[i + s[j]] %= mod;
      seen[i + s[j]] = 1;
    }
    if (f)
      break;
  }
  cout << dp[n] << endl;
  // rep(i,1,n+1) cout << i << " " << dp[i] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#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 = 1e9;
const ll MOD = 998244353;
int main() {
  int N, K;
  cin >> N >> K;
  vector<int> L(K), R(K);
  REP(i, K) { cin >> L[i] >> R[i]; }
  vector<int> Wa(N + 100);
  vector<int> dp(N + 100);
  dp[1] = 1;
  Wa[1] = 1;
  for (int i = 1; i <= N; i++) {
    ll num = 0;

    REP(j, K) {
      int l = max(0, i - R[j] - 1);
      int r = i - L[j];
      if (r < 0)
        continue;
      dp[i] += Wa[r] - Wa[l];
      while (dp[i] < 0) {
        dp[i] += MOD;
      }
      dp[i] %= MOD;
    }
    Wa[i] = dp[i] + Wa[i - 1];
    while (Wa[i] < 0) {
      Wa[i] += MOD;
    }
    Wa[i] %= MOD;
  }
  cout << dp[N] << endl;
}


Error: 0

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 = 1e9;
const ll MOD = 998244353;
int dp[2 * 10 + 100];
int main() {
  int N, K;
  cin >> N >> K;
  vector<int> L(K), R(K);
  REP(i, K) { cin >> L[i] >> R[i]; }
  vector<int> Wa(N + 100);
  dp[1] = 1;
  Wa[1] = 1;
  for (int i = 1; i <= N; i++) {
    ll num = 0;

    REP(j, K) {
      int l = max(0, i - R[j] - 1);
      int r = i - L[j];
      if (r < 0)
        continue;
      dp[i] += Wa[r] - Wa[l];
      while (dp[i] < 0) {
        dp[i] += MOD;
      }
      dp[i] %= MOD;
    }
    Wa[i] = dp[i] + Wa[i - 1];
    while (Wa[i] < 0) {
      Wa[i] += MOD;
    }
    Wa[i] %= MOD;
  }
  cout << dp[N] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2E5 + 5;
const int mod = 998244353;

int dp[N];

void solve() {

  int n, k;
  scanf("%lld %lld", &n, &k);
  pair<int, int> p[15];

  for (int i = 0; i < k; i++) {
    int x, y;
    scanf("%lld %lld", &x, &y);
    p[i] = make_pair(x, y);
  }

  dp[1] = 1;

  for (int i = 2; i <= n; i++) {

    dp[i] = 0;

    for (int j = 0; j < k; j++) {

      int l = max(1LL, i - p[j].second);
      int r = i - p[j].first;

      if (l <= r) {
        dp[i] += dp[r] - dp[l - 1];
        dp[i] %= mod;
        dp[i] += mod;
        dp[i] %= mod;
      }
    }

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

  printf("%lld\\n", ((dp[n] - dp[n - 1]) + mod) % mod);
}

int32_t main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;
const int mod = 998244353;
const int maxn = 2e5 + 7;
ll dp[maxn];
vector<int> v;
int main(void) {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; j++) {
      v.push_back(j);
    }
  }
  dp[0] = 1;
  int len = v.size();
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < len; j++) {
      if (i - v[j] >= 0)
        dp[i] = (dp[i] + dp[i - v[j]]) % mod;
    }
  }
  cout << dp[n - 1];
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#define ll long long
#define N 200005
#define mod 998244353
#define rep(i, l, r) for (int i = l; i <= r; ++i)
using namespace std;
int t;
int n, k;
int l[11], r[11];
int f[N], s[N];
int main() {
  scanf("%d%d", &n, &k);
  rep(i, 1, k) { scanf("%d%d", &l[i], &r[i]); }
  s[1] = f[1] = 1;
  rep(i, 2, n) {
    rep(j, 1, k) {
      if (i > l[j]) {
        if (i > r[j])
          f[i] = ((f[i] + s[i - l[j]] - s[i - r[j] - 1]) % mod + mod) % mod;
        else
          f[i] = (f[i] + s[i - l[j]]) % mod;
      }
    }
    // printf("%d %d\\n", i, f[i]);
    s[i] = (s[i - 1] + f[i]) % mod;
  }
  printf("%d", f[n]);
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#define ll long long
#define N 100005
#define mod 998244353
#define rep(i, l, r) for (int i = l; i <= r; ++i)
using namespace std;
int t;
int n, k;
int l[11], r[11];
int f[N], s[N];
int main() {
  scanf("%d%d", &n, &k);
  rep(i, 1, k) { scanf("%d%d", &l[i], &r[i]); }
  s[0] = f[0] = 1;
  rep(i, 1, n) {
    rep(j, 1, k) {
      if (i >= l[j]) {
        if (i > r[j])
          f[i] += s[i - l[j]] - s[i - r[j] - 1];
        else
          f[i] += s[i - l[j]];
      }
    }
    // printf("%d %d\\n", i, f[i]);
    f[i] = (f[i] % mod + mod) % mod;
    s[i] = (s[i - 1] + f[i]) % mod;
  }
  printf("%d", f[n - 1]);
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\\n'
#define Rapido                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0)
const int maxS = 2e5 + 10;
int MOD = 998244353;
int DP[2 * maxS];

void code_idhr_h() {
  int N, K;
  cin >> N >> K;
  vector<int> L(K + 1), R(K + 1);
  for (int i = 1; i <= K; i++)
    cin >> L[i] >> R[i];
  DP[1] = 1;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= K; j++) {
      DP[i + L[j]] = (DP[i + L[j]] + DP[i]) % MOD;
      DP[i + R[j] + 1] = (DP[i + R[j] + 1] - DP[i] + MOD) % MOD;
    }
    DP[i + 1] = (DP[i + 1] + DP[i]) % MOD;
  }
  cout << (DP[N] - DP[N - 1] + MOD) % MOD;
}

int32_t main() {
  Rapido;
  int T = 1; // cin >> T;
  for (int t = 1; t <= T; t++) {
    code_idhr_h();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\\n'
#define Rapido                                                                 \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0)
const int maxS = 2e5 + 10;
int MOD = 998244353;

int N, K;
int DP[maxS];
set<pair<int, int>> SET;

int go(int pos) {
  if (pos == N)
    return 1;
  if (pos > N)
    return 0;
  if (DP[pos] != -1)
    return DP[pos];
  int ans = 0;
  for (auto it : SET) {
    for (int i = it.first; i <= it.second; i++)
      ans = (ans + go(pos + i)) % MOD;
  }
  return DP[pos] = ans;
}

void code_idhr_h() {
  cin >> N >> K;
  while (K--) {
    int x, y;
    cin >> x >> y;
    SET.insert({x, y});
    // SET.insert(y);
  }
  memset(DP, -1, sizeof DP);
  cout << go(1);
}

int32_t main() {
  Rapido;
  int T = 1; // cin >> T;
  for (int t = 1; t <= T; t++) {
    code_idhr_h();
  }
  return 0;
}