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

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

#define trav(a, A) for (auto &a : A)
#define f first
#define s second

typedef long long ll;

ll MOD = 998244353;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
const ll MAXN = 1e+5 + 7;

ll rsq(vector<ll> &tree, ll l, ll r) {
  ll i, j, n = tree.size() / 2;
  l = max(0LL, l);
  r = max(0LL, r);
  ll ret = 0;
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l & 1)
      ret += tree[l++], ret %= MOD;
    if (r & 1)
      ret += tree[--r], ret %= MOD;
  }
  return ret;
}

void upd(vector<ll> &tree, ll p, ll v) {
  ll i, n = tree.size() / 2;
  tree[n + p] = v;
  for (i = (n + p) >> 1; i > 0; i >>= 1)
    tree[i] = tree[i << 1] + tree[i << 1 | 1], tree[i] %= MOD;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll i, j, n, k;
  cin >> n >> k;
  vector<ll> dp(n), tree(2 * n, 0);
  vector<pair<ll, ll>> qr(k);
  trav(p, qr) cin >> p.f >> p.s;
  sort(qr.begin(), qr.end());
  dp[0] = 1;
  tree[n] = 1;
  for (i = n; i > 0; i >>= 1)
    tree[i] = 1;
  for (i = 1; i < n; ++i) {
    for (j = 0; j < k; ++j)
      dp[i] += rsq(tree, i - qr[j].s, i - qr[j].f + 1), dp[i] %= MOD;
    upd(tree, i, dp[i]);
  }
  // trav (a, dp) cout << a << '\\n';
  cout << dp[n - 1] << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

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

#define trav(a, A) for (auto &a : A)
#define f first
#define s second

typedef long long ll;

ll MOD = 998244353;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int INF = 0x3f3f3f3f;
const ll MAXN = 1e+5 + 7;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll i, j, n, k;
  cin >> n >> k;
  vector<ll> dp(n), jp;
  dp[0] = 1;
  for (i = 0; i < k; ++i) {
    ll a, b;
    cin >> a >> b;
    for (j = a; j <= b; ++j)
      jp.push_back(j);
  }
  sort(jp.begin(), jp.end());
  for (i = 0; i < n; ++i)
    for (j = 0; j < jp.size() && i + jp[j] < n; ++j)
      dp[i + jp[j]] += dp[i], dp[i + jp[j]] %= MOD;
  // trav (a, dp) cout << a << '\\n';
  cout << dp[n - 1] << '\\n';

  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 ll = long long;
using namespace std;
const ll mod = 998244353;

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

  vector<ll> dp(2 * N + 5);
  dp[0] = 1;
  dp[1] = -1;
  rep(i, N) {
    rep(j, K) {
      dp[i + L[j]] += dp[i];
      dp[i + L[j]] = (dp[i + L[j]] + mod) % mod;
      dp[i + R[j] + 1] -= dp[i];
    }
    dp[i + 1] += dp[i];
    dp[i + 1] = (dp[i + 1] + 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++)
using ll = long long;
using namespace std;
const ll mod = 998244353;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> p(N + 2);
  rep(i, K) {
    int l, r;
    cin >> l >> r;
    p[l] += 1;
    p[r + 1] -= 1;
  }
  vector<int> S;
  for (int i = 1; i <= N; i++) {
    p[i] += p[i - 1];
    if (p[i] > 0)
      S.push_back(i);
  }
  vector<ll> dp(2 * N + 5);
  dp[0] = 1;
  rep(i, N) {
    for (int j : S) {
      dp[i + j] += dp[i];
      dp[i + j] %= mod;
    }
  }
  cout << dp[N - 1] << endl;

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

#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define irep(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define pb push_back
#define mp make_pair
#define F .first
#define S .second
#define pll pair<ll, ll>
using ll = long long;
using ld = long double;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
using namespace std;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

ll l[20], r[20];
ll dp[220000], sdp[220000];
map<ll, ll> x;
int main() {
  ll n, k, ans = 0;
  cin >> n >> k;

  rep(i, 0, k) { cin >> l[i] >> r[i]; }
  dp[0] = 1, sdp[1] = 1;
  rep(i, 1, n + 1) {
    rep(j, 0, k) {
      ll L = max((ll)0, i - r[j]);
      ll R = max((ll)0, i - l[j] + 1);
      dp[i] = (dp[i] + sdp[R] - sdp[L]) % mod2;
    }
    sdp[i + 1] = sdp[i] + dp[i];
  }
  cout << (mod2 + dp[n - 1]) % mod2;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = ll(a); i < ll(b); i++)
#define irep(i, a, b) for (ll i = ll(a); i >= ll(b); i--)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define pb push_back
#define mp make_pair
#define F .first
#define S .second
#define pll pair<ll, ll>
using ll = long long;
using ld = long double;
const ll INF = 1LL << 60;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
using namespace std;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

ll l[20], r[20];
ll dp[220000];
map<ll, ll> x;
int main() {
  ll n, k, ans = 0;
  cin >> n >> k;

  rep(i, 0, k) {
    cin >> l[i] >> r[i];

    rep(j, l[i], r[i] + 1) {
      x[j] = 1;
      /*
      ll c=j;
      ll k=2;
      bool f=false;
      while (c>=k*k) {
          if(c%k==0){
           c/=k;
          if(c%k!=0)ans++;
            } else {
           k++;
       }*/
    }
  }
  dp[1] = 1;
  rep(i, 1, n + 1) {
    for (auto j : x) {
      ll p = i + j.first;
      if (p > n)
        break;
      if (p <= n)
        dp[p] = (dp[i] + dp[p]) % mod2;
    }
  }
  cout << (mod2 + dp[n]) % mod2;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long LL;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> L(K), R(K);
  vector<LL> Sums(K, 0);
  REP(i, K) { cin >> L[i] >> R[i]; }

  LL ans = 0;
  LL M = 998244353;

  vector<LL> route(N + 1, 0);
  route[1] = 1;

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

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

      if (i - L[k] < 0) {
        continue;
      }

      if (i - L[k] >= 0) {
        int index = i - R[k] - 1;
        if (index >= 0) {
          LL tmp = (Sums[k] - route[index]) % M;
          if (tmp < 0) {
            tmp += M;
          }
          Sums[k] = (route[i - L[k]] + tmp) % M;
        } else {
          Sums[k] = (Sums[k] + route[i - L[k]]) % M;
        }
      }
    }

    for (int k = 0; k < K; k++) {
      route[i] = (route[i] + Sums[k]) % M;
    }
  }

  cout << route[N] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
typedef long long LL;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> L(K), R(K);
  REP(i, K) { cin >> L[i] >> R[i]; }

  LL ans = 0;
  LL M = 998244353;

  vector<LL> route(N + 1, 0);
  route[1] = 1;

  for (int i = 2; i <= N; i++) {
    for (int k = 0; k < K; k++) {
      for (int j = L[k]; j <= R[k]; j++) {
        if (i - j < 0) {
          break;
        }
        route[i] = (route[i] + route[i - j]) % M;
        // cout << "i: " << i << ", val:" << route[i] << endl;
      }
    }
  }

  cout << route[N] << 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)
using ll = long long;
const ll MOD = 998244353;
const int INF = 1001001001;
// const ll INF = 1000000000000000000LL;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<pair<ll, ll>> pr(k);
  rep(i, k) {
    ll a, b;
    cin >> a >> b;
    pr[i].first = a;
    pr[i].second = b;
  }
  sort(pr.begin(), pr.end());

  vector<ll> dp(n);
  vector<ll> dp_sum(n + 1);
  dp[0] = 1;
  dp_sum[0] = 0;
  dp_sum[1] = 1;
  for (int i = 1; i < n; i++) {
    rep(j, k) {
      ll cur_l = pr[j].first;
      ll cur_r = pr[j].second;
      if (i - cur_l < 0)
        break;
      if (i - cur_r < 0)
        cur_r = i;
      // if(i-cur_r>=0){
      // あとで
      dp[i] += dp_sum[i - cur_l + 1] - dp_sum[i - cur_r];
      dp[i] %= MOD;
      // }
      // else{
      //     for(int k=cur_l;k<=cur_r;k++){
      //         if(i-k>=0){
      //             dp[i] += dp[i-k];
      //             dp[i]%=MOD;
      //         }
      //         else
      //             break;
      //     }
      // }
    }
    dp_sum[i + 1] = dp[i] + dp_sum[i];
    dp_sum[i + 1] %= MOD;
  }
  if (dp[n - 1] < 0)
    dp[n - 1] += MOD;
  cout << dp[n - 1] << 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)
using ll = long long;
const ll MOD = 998244353;
const int INF = 1001001001;
// const ll INF = 1000000000000000000LL;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<pair<ll, ll>> pr(k);
  rep(i, k) {
    ll a, b;
    cin >> a >> b;
    pr[i].first = a;
    pr[i].second = b;
  }
  sort(pr.begin(), pr.end());

  vector<ll> dp(n);
  vector<ll> dp_sum(n + 1);
  dp[0] = 1;
  dp_sum[0] = 0;
  dp_sum[1] = 1;
  for (int i = 1; i < n; i++) {
    rep(j, k) {
      ll cur_l = pr[j].first;
      ll cur_r = pr[j].second;
      if (i - cur_l < 0)
        break;
      if (i - cur_r >= 0) {
        // あとで
        dp[i] += dp_sum[i - cur_l + 1] - dp_sum[i - cur_r];
        dp[i] %= MOD;
      } else {
        for (int k = cur_l; k <= cur_r; k++) {
          if (i - k >= 0) {
            dp[i] += dp[i - k];
            dp[i] %= MOD;
          } else
            break;
        }
      }
    }
    dp_sum[i + 1] = dp[i] + dp_sum[i];
    dp_sum[i + 1] %= MOD;
  }
  if (dp[n - 1] < 0)
    dp[n - 1] += MOD;
  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++)
#define rep3(i, s, n) for (int i = (s); i <= (int)(n); i++)

int a[200010];
int b[200010];
int lk[20];
int rk[20];

int main() {
  int n;
  int m;
  int k;
  int ans, lkk, rkk;
  long long p;

  m = 998244353;
  a[1] = 1;
  b[1] = 1;

  cin >> n >> k;
  rep(i, k) cin >> lk[i] >> rk[i];
  rep3(i, 2, n + 1) {
    rep(j, k) {
      rkk = max(i - rk[j] - 1, 0);
      lkk = max(i - lk[j], 0);
      p = b[lkk] - b[rkk] + m;
      p %= m;
      a[i] += p;
      a[i] %= m;
    }
    b[i] = b[i - 1] + a[i];
    b[i] %= m;
  }
  ans = a[n] + m;
  ans %= m;

  cout << ans << 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++)
#define rep3(i, s, n) for (int i = (s); i <= (int)(n); i++)

int a[400010];
int b[200010];

int main() {
  int n;
  int m;
  int k;
  int p, lk, rk;

  m = 998244353;
  p = 0;
  a[1] = 1;

  cin >> n >> k;
  rep(i, k) {
    cin >> lk >> rk;
    rep3(i, lk, rk) {
      b[p] = i;
      p++;
    }
  }
  rep(i, n) {
    if (a[i] == 0)
      continue;
    rep(j, p) {
      a[i + b[j]] += a[i];
      a[i + b[j]] %= m;
    }
  }

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

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

using namespace std;

int main() {
  int32_t N, K;
  cin >> N >> K;
  vector<pair<int32_t, int32_t>> lrs(K);
  for (int32_t k = 0; k < K; ++k) {
    cin >> lrs[k].first >> lrs[k].second;
  }

  vector<uint32_t> dp{1};
  vector<int64_t> hs(K);

  for (int32_t i = 1; i < N; ++i) {
    dp.push_back(0);
    uint32_t &cdp = dp[i];
    for (int32_t k = 0; k < K; ++k) {
      const auto &lr = lrs[k];
      auto &h = hs[k];
      int32_t e = i - lr.first;
      int32_t s = i - lr.second;

      if (e < 0) {
        continue;
      }

      h += dp[e];

      if (s > 0) {
        h -= dp[s - 1];
        if (h < 0) {
          h += 998244353;
        }
      }

      cdp = (cdp + h) % 998244353;
    }
  }

  cout << dp[N - 1];
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  int32_t N, K;
  cin >> N >> K;
  vector<pair<int32_t, int32_t>> lrs(K);
  for (int32_t k = 0; k < K; ++k) {
    cin >> lrs[k].first >> lrs[k].second;
  }

  vector<uint32_t> dp{1};

  for (int32_t i = 1; i < N; ++i) {
    dp.push_back(0);
    uint32_t &cdp = dp[i];
    for (const auto &lr : lrs) {
      int32_t e = i - lr.first;
      int32_t s = max(0, i - lr.second);
      if (e < 0) {
        continue;
      }

      for (int32_t j = s; j <= e; ++j) {
        cdp += dp[j];
        cdp %= 998244353;
      }
    }
  }

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

#include "bits/stdc++.h"
#include "math.h"

using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef pair<ll, ll> P;
typedef vector<P> vp;
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define drep(i, a, b) for (ll i = (a); i >= (b); --i)
#define SIZE(a) ll((a).size())
#define out(a) cout << (a) << endl;
const int inf = INT_MAX;
const int MAX = 510000;
const ll MOD = 998244353;

int main() {
  ll n, k;
  cin >> n >> k;
  vll l(k), r(k);
  rep(i, 0, k) { cin >> l[i] >> r[i]; }
  vll dp(n, 0), sdp(n + 1, 0);
  dp[0] = 1;
  sdp[1] = 1;
  rep(i, 1, n) {
    rep(j, 0, k) {
      ll left = max(0LL, i - r[j]), right = max(0LL, i - l[j] + 1);
      dp[i] += sdp[right] - sdp[left];
      dp[i] %= MOD;
    }
    sdp[i + 1] = sdp[i] + dp[i];
  }

  out(dp[n - 1] % MOD);
}


Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include "math.h"

using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef pair<ll, ll> P;
typedef vector<P> vp;
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define drep(i, a, b) for (ll i = (a); i >= (b); --i)
#define SIZE(a) ll((a).size())
#define out(a) cout << (a) << endl;
const int inf = INT_MAX;
const int MAX = 510000;
const ll MOD = 998244353;

int main() {
  ll n, k;
  cin >> n >> k;
  vb ok(n + 1, false);
  rep(i, 0, k) {
    ll l, r;
    cin >> l >> r;
    rep(i, l, r + 1) { ok[i] = true; }
  }
  vll dp(n, 0);
  dp[0] = 1;
  rep(i, 1, n) {
    rep(u, 1, i + 1) {
      if (ok[u]) {
        dp[i] += dp[i - u];
        dp[i] %= MOD;
      }
    }
  }
  out(dp[n - 1] % MOD);
}
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 < (n); ++i)
#define drep(i, n) for (int i = (n - 1); i >= 0; i--)
#define all(v) (v).begin(), (v).end()
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
// const ll MOD = 1e9+7;
const ll MOD = 998244353;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<P> s;
  rep(i, k) {
    int l, r;
    cin >> l >> r;
    s.push_back(make_pair(l, r));
  }
  vector<ll> dp(2 * n);
  dp[n] = 1;
  ll cum = 0;
  rep(i, n - 1) {
    for (P p : s) {
      cum += dp[i + n + 1 - p.first];
      cum -= dp[i + n + 1 - p.second - 1];
      cum += 2 * MOD;
      cum %= MOD;
    }
    dp[i + n + 1] = cum;
  }
  cout << dp[2 * n - 1] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n - 1); i >= 0; i--)
#define all(v) (v).begin(), (v).end()
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
typedef pair<int, int> P;
typedef long long ll;
const int INF = 1001001001;
const ll LINF = 1001002003004005006ll;
// const ll MOD = 1e9+7;
const ll MOD = 998244353;

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<int> s;
  rep(i, k) {
    int l, r;
    cin >> l >> r;
    for (int d = l; d <= r; d++) {
      s.push_back(d);
    }
  }
  vector<ll> dp(n);
  dp[0] = 1;
  rep(i, n - 1) {
    for (int d : s) {
      if (i + 1 >= d) {
        dp[i + 1] += dp[i + 1 - d];
        dp[i + 1] %= MOD;
      }
    }
  }
  cout << dp[n - 1] << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\\n'
#define mod 998244353
#define pb push_back
#define ff first
#define ss second
#define con continue
#define ub upper_bound
#define lb lower_bound
#define sum(a) (accumulate((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) (min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) (max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) (lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) (upper_bound((a).begin(), (a).end(), (x)) - (a).begin())

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

const int dx8[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dy8[] = {0, -1, 0, 1, 1, -1, 1, -1};

void solve() {
  ll n, k;
  cin >> n >> k;
  vector<ll> dp(n + 1, 0);
  vector<pair<ll, ll>> q;

  for (ll i = 0; i < k; i++) {

    ll a, b;
    cin >> a >> b;
    q.pb({a, b});
  }
  dp[0] = 1;
  for (ll i = 0; i < n; i++) {

    for (ll j = 0; j < k; j++) {
      ll x, y;
      x = i + q[j].ff;
      y = i + q[j].ss;
      if (x >= n)
        continue;
      y = min(y, n - 1);
      dp[x] = (dp[x] % mod + dp[i] % mod) % mod;
      dp[y + 1] = (dp[y + 1] % mod - dp[i] % mod + mod) % mod;
    }
    if (i == 0)
      continue;
    dp[i + 1] = (dp[i + 1] % mod + dp[i] % mod) % mod;
  }
  cout << dp[n - 1] << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\\n'
#define mod 998244353
#define pb push_back
#define ff first
#define ss second
#define con continue
#define ub upper_bound
#define lb lower_bound
#define sum(a) (accumulate((a).begin(), (a).end(), 0ll))
#define mine(a) (*min_element((a).begin(), (a).end()))
#define maxe(a) (*max_element((a).begin(), (a).end()))
#define mini(a) (min_element((a).begin(), (a).end()) - (a).begin())
#define maxi(a) (max_element((a).begin(), (a).end()) - (a).begin())
#define lowb(a, x) (lower_bound((a).begin(), (a).end(), (x)) - (a).begin())
#define uppb(a, x) (upper_bound((a).begin(), (a).end(), (x)) - (a).begin())

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};

const int dx8[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dy8[] = {0, -1, 0, 1, 1, -1, 1, -1};
vector<ll> dp, j;
ll solve(ll pos) {

  if (pos == dp.size() - 1)
    return 1;
  if (dp[pos] != -1)
    return dp[pos];
  ll ans = 0;
  for (auto x : j) {
    if (x + pos >= dp.size())
      break;
    ans = (ans % mod + solve(x + pos) % mod) % mod;
  }
  dp[pos] = ans;
  return ans;
}

void solve() {
  ll n;
  cin >> n;
  dp.clear();
  dp.resize(n, -1);
  j.clear();

  ll k;
  cin >> k;
  for (ll i = 0; i < k; i++) {
    ll x, y;
    cin >> x >> y;
    for (ll j1 = x; j1 <= y; j1++)
      j.pb(j1);
  }

  sort(j.begin(), j.end());
  cout << solve(0) << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define ntest 0
#define l first
#define r second

using namespace std;
const int mod = 998244353;
int n, k;
pair<int, int> seg[11];
long long dp[200005], f[200005];
void solve() {
  cin >> n >> k;
  for (int i = 1; i <= k; ++i)
    cin >> seg[i].l >> seg[i].r;
  dp[1] = 1;
  f[1] = 1;
  for (int i = 2; i <= n; ++i) {
    for (int j = 1; j <= k; ++j) {
      int s = max(0, i - seg[j].l);
      int e = max(1, i - seg[j].r);
      dp[i] = (dp[i] + (f[s] - f[e - 1] + mod) % mod) % mod;
    }
    f[i] = (f[i - 1] + dp[i]) % mod;
  }
  cout << dp[n];
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  if (ntest) {
    int t;
    cin >> t;
    while (t--)
      solve();
  } else {
    solve();
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

#define ntest 0
#define l first
#define r second

using namespace std;
const int mod = 998244353;
int n, k;
pair<int, int> seg[11];
long long dp[100005], f[100005];
void solve() {
  cin >> n >> k;
  for (int i = 1; i <= k; ++i)
    cin >> seg[i].l >> seg[i].r;
  dp[1] = 1;
  f[1] = 1;
  for (int i = 2; i <= n; ++i) {
    for (int j = 1; j <= k; ++j) {
      int s = max(0, i - seg[j].l);
      int e = max(1, i - seg[j].r);
      dp[i] = (dp[i] + (f[s] - f[e - 1] + mod) % mod) % mod;
    }
    f[i] = (f[i - 1] + dp[i]) % mod;
  }
  cout << dp[n];
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  if (ntest) {
    int t;
    cin >> t;
    while (t--)
      solve();
  } else {
    solve();
  }
}