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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pii pair<int, int>
ll dp[(ll)(2e5 + 2)];
ll MOD = 998244353;
int main() {
  memset(dp, 0, sizeof dp);
  int n, k;
  cin >> n >> k;
  vector<pii> v(k);
  for (int i = 0; i < k; i++) {
    cin >> v[i].first >> v[i].second;
  }
  // int ans=0;
  dp[0] = 1;
  vector<ll> pref(n + 1);
  ll pref_sum = 0;
  for (int i = 0; i < n; i++) {
    dp[i] += pref_sum;
    for (int j = 0; j < k; j++) {
      int L = v[j].first;
      int R = v[j].second;
      if (i + L < n)
        pref[i + L] += dp[i];
      // pref[i+L] = pref[i+L]%MOD;
      if (i + L < n && pref[i + L] > MOD)
        pref[i + L] -= MOD;
      if (i + R + 1 < n) {
        pref[i + R + 1] -= dp[i];
        if (pref[i + R + 1] < 0)
          pref[i + R + 1] += MOD;
      }
    }
    pref_sum += pref[i + 1];
    if (pref_sum > MOD)
      pref_sum -= MOD;
    if (pref_sum < 0)
      pref_sum += MOD;
  }
  cout << dp[n - 1] % MOD << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define pii pair<int, int>
ll dp[(ll)(2e5 + 2)];
ll MOD = 998244353;
int main() {
  memset(dp, 0, sizeof dp);
  int n, k;
  cin >> n >> k;
  vector<pii> v(k);
  for (int i = 0; i < k; i++) {
    cin >> v[i].first >> v[i].second;
  }
  // int ans=0;
  dp[0] = 1;
  vector<ll> pref(n + 1);
  ll pref_sum = 0;
  for (int i = 0; i < n; i++) {
    dp[i] += pref_sum;
    for (int j = 0; j < k; j++) {
      int L = v[j].first;
      int R = v[j].second;
      if (i + L < n)
        pref[i + L] += dp[i];
      // pref[i+L] = pref[i+L]%MOD;
      if (pref[i + L] > MOD)
        pref[i + L] -= MOD;
      if (i + R + 1 < n) {
        pref[i + R + 1] -= dp[i];
        if (pref[i + R + 1] < 0)
          pref[i + R + 1] += MOD;
      }
    }
    pref_sum += pref[i + 1];
    if (pref_sum > MOD)
      pref_sum -= MOD;
    if (pref_sum < 0)
      pref_sum += MOD;
  }
  cout << dp[n - 1] % MOD << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

const int mod = 998244353;

int main() {

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

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

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

  cout << dp[n];

  return 0;
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;

const int mod = 998244353;

int main() {

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

  vector<ll> dp(n + 1);
  dp[1] = 1;
  for (int j = 1; j <= n; j++) {
    for (int i = 0; i < p; i++) {
      if (j >= b[i])
        dp[j] = (dp[j] + dp[j - b[i]]) % mod;
    }
  }

  cout << dp[n];

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

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

using namespace std;

template <std::uint_fast64_t Mod> class ModInt {
  using u64 = std::uint_fast64_t;

public:
  u64 val;

  constexpr ModInt(const u64 x = 0) noexcept : val(x % Mod) {}
  constexpr ModInt operator+(const ModInt rhs) const noexcept {
    return ModInt(*this) += rhs;
  }
  constexpr ModInt operator-(const ModInt rhs) const noexcept {
    return ModInt(*this) -= rhs;
  }
  constexpr ModInt operator*(const ModInt rhs) const noexcept {
    return ModInt(*this) *= rhs;
  }
  constexpr ModInt operator/(const ModInt rhs) const noexcept {
    return ModInt(*this) /= rhs;
  }
  constexpr ModInt &operator+=(const ModInt rhs) noexcept {
    val += rhs.val;
    if (val >= Mod) {
      val -= Mod;
    }
    return *this;
  }
  constexpr ModInt &operator-=(const ModInt rhs) noexcept {
    if (val < rhs.val) {
      val += Mod;
    }
    val -= rhs.val;
    return *this;
  }
  constexpr ModInt &operator*=(const ModInt rhs) noexcept {
    val = val * rhs.val % Mod;
    return *this;
  }
  constexpr ModInt &operator/=(ModInt rhs) noexcept {
    u64 exp = Mod - 2;
    while (exp) {
      if (exp % 2) {
        *this *= rhs;
      }
      rhs *= rhs;
      exp /= 2;
    }
    return *this;
  }
};

const long long MOD = 998244353;
using mint = ModInt<MOD>;

int main() {
  int N, K;
  cin >> N >> K;
  vector<pair<int, int>> S(K);
  for (auto &[l, r] : S)
    cin >> l >> r;
  vector<mint> dp(N);
  dp[0] = 1;
  dp[1] -= 1;
  for (int i = 0; i != N; ++i) {
    if (i)
      dp[i] += dp[i - 1];
    for (auto [l, r] : S) {
      if (i + l < N)
        dp[i + l] += dp[i];
      if (i + r + 1 < N)
        dp[i + r + 1] -= dp[i];
    }
  }
  cout << dp[N - 1].val << endl;
}


Error: TLE

Fixed submission:

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

using namespace std;
using ll = long long;

const ll MOD = 998244353;

int main() {
  ll N, K, L, R;
  cin >> N >> K;
  vector<int> S;
  while (cin >> L >> R) {
    for (int s = L; s <= R; ++s) {
      S.push_back(s);
    }
  }
  sort(begin(S), end(S));
  vector<ll> dp(N + 1);
  dp[1] = 1;
  for (int i = 1; i != N; ++i) {
    if (!dp[i])
      continue;
    for (int s : S) {
      if (i + s > N)
        break;
      dp[i + s] = (dp[i + s] + dp[i]) % MOD;
    }
  }

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

#pragma warning(disable : 4996)
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> l(n), r(n);
  rep(i, k) cin >> l[i] >> r[i];
  vector<ll> dp(n + 1);
  vector<ll> dpsum(n + 1);
  dp[1] = 1;
  dpsum[1] = 1; // dpの累積和
  for (int i = 2; i <= n; i++) {
    rep(j, k) {
      int li = i - r[j];
      int ri = i - l[j];
      if (ri < 0)
        continue;
      li = max(li, 1);
      dp[i] += dpsum[ri] -
               dpsum[li - 1]; // 現在位置iからの差分のdpsumが更新に必要となる
      dp[i] %= 998244353;
      if (dp[i] < 0)
        dp[i] += 998244353;
    }
    dpsum[i] = dpsum[i - 1] + dp[i];
    dpsum[i] %= 998244353;
    if (dpsum[i] < 0)
      dpsum[i] += 998244353;
  }
  cout << dp[n] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#pragma warning(disable : 4996)
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> w;
  ll dp[200005] = {0};
  rep(i, k) {
    int l, r;
    cin >> l >> r;
    for (int i = l; i <= r; i++) {
      w.push_back(i);
      dp[i] = 1;
    }
  }
  int s = w.size();
  rep(i, n) {
    rep(j, s) {
      if (i - w[j] >= 0) {
        dp[i] += dp[i - w[j]];
        dp[i] %= 998244353;
      }
    }
  }

  cout << dp[n - 1] << endl;

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

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> move;
  ll ans[n];
  pair<int, int> l[k];
  ll sum[k];
  ll dis[k];
  ll now[k];
  for (int i = 0; i < k; i++) {
    cin >> l[i].first >> l[i].second;
    sum[i] = 0;
    dis[i] = 0;
    now[i] = -1;
  }
  for (int i = 0; i < n; i++) {
    ans[i] = 0;
  }
  ll mod = 998244353;
  sort(l, l + k);

  for (int i = 0; i < k; i++) {
    dis[i] = l[i].second - l[i].first;
  }
  int size = move.size();
  ans[0] = 1;
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < k; j++) {
      if (i - l[j].first < 0) {
        break;
      } else {
        if (now[j] == dis[j]) {
          sum[j] -= ans[i - l[j].second - 1];
          sum[j] += ans[i - l[j].first];
          ans[i] += sum[j];
          ans[i] %= mod;
        } else {
          now[j]++;
          sum[j] += ans[i - l[j].first];
          ans[i] += sum[j];
          ans[i] %= mod;
        }
      }
    }
  }

  cout << ans[n - 1] << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> move;
  ll ans[n];
  for (int i = 0; i < k; i++) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; j++) {
      move.push_back(j);
    }
  }
  for (int i = 0; i < n; i++) {
    ans[i] = 0;
  }
  ll mod = 998244353;
  sort(move.begin(), move.end());
  int size = move.size();
  ans[0] = 1;
  for (int i = 1; i < n; i++) {
    int cou = 0;
    for (int j = 0; j < size; j++) {
      if (i - move[j] < 0) {
        break;
      } else {
        ans[i] += ans[i - move[j]];
        ans[i] %= mod;
      }
    }
  }

  cout << ans[n - 1] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  long long n, k, i, j, mod = 998244353;
  cin >> n >> k;

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

  vector<long long> ans(n + 1 + 100000, 0), cur(n + 1 + 100000, 0);
  long long l, r, res;

  res = 1;
  for (i = 1; i < n; i++) {
    for (j = 0; j < k; j++) {
      l = d[j].first;
      r = d[j].second + 1;
      if (i + l > n) {
        l = n + 1 - i;
      }
      if (i + r > n) {
        r = n + 1 - i;
      }
      cur[i + l] += res;
      cur[i + r] -= res;
    }
    ans[i + 1] = (ans[i] + cur[i + 1]) % mod;
    res = (ans[i] + cur[i + 1]) % mod;
  }

  ans[n] %= mod;
  if (ans[n] < 0) {
    ans[n] += mod;
  }
  cout << ans[n] << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  long long n, k, i, j, mod = 998244353;
  cin >> n >> k;

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

  vector<long long> ans(n + 1 + 100000, 0), cur(n + 1 + 100000, 0);
  long long l, r, res;

  res = 1;
  for (i = 1; i < n; i++) {
    for (j = 0; j < k; j++) {
      l = d[j].first;
      r = d[j].second + 1;
      cur[i + l] += res;
      cur[i + r] -= res;
    }
    ans[i + 1] = (ans[i] + cur[i + 1]) % mod;
    res = (ans[i] + cur[i + 1]) % mod;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define pb push_back
#define faster                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define mem(a, v) memset(a, v, sizeof(a))
const ll INF = 1e18;
const int mod = 998244353;
const int iinf = 1e9 + 10;
const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1};
const int mxN = 2e5;
ll dp[mxN + 1], c[10], dp1[mxN + 1];
pii a[mxN + 1];
ll g(int l, int r) {
  // cout<<"r="<<r<<" "<<dp1[r]<<"\\n";
  // cout<<"l-1="<<l-1<<" "<<dp1[l-1]<<"\\n";
  if (l <= 0)
    l = 0;
  return ((dp1[r] - dp1[l - 1]) % mod + mod) % mod;
}
int main() {
  faster int n, k;
  cin >> n >> k;
  for (int i = 0; i < k; i++)
    cin >> a[i].fi >> a[i].se;
  dp[0] = 1;
  dp1[0] = 1;
  for (int i = 1; i <= n - 1; i++) {
    for (int j = 0; j < k; j++) {
      dp[i] = (dp[i] + g(i - a[j].se, i - a[j].fi)) % mod;
    }
    dp1[i] = (dp1[i - 1] + dp[i]) % mod;
  }
  cout << dp[n - 1];
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define pb push_back
#define faster                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define mem(a, v) memset(a, v, sizeof(a))
const ll INF = 1e18;
const int mod = 998244353;
const int iinf = 1e9 + 10;
const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1};
const int mxN = 2e5;
ll dp[mxN + 1];
int main() {
  faster int n, k;
  cin >> n >> k;
  vector<int> v;
  while (k--) {
    int a, b;
    cin >> a >> b;
    for (int i = a; i <= b; i++) {
      if (i <= n - 1)
        v.pb(i);
    }
  }
  dp[0] = 1;
  for (int i = 0; i <= n - 1; i++) {
    for (int j = 0; j < sz(v); j++) {
      if (i - v[j] >= 0) {
        dp[i] = (dp[i] + dp[i - v[j]]) % mod;
      }
    }
  }
  cout << dp[n - 1];
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
    हॉंजी । की हाल चाल?
    बड़ीया सब?
*/

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

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii unordered_map<int, int>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define MOD 998244353
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define EPS 1e-9

void gogeta() {
  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
}

int MAX = 2 * 100000 + 5;

int32_t main() {
  gogeta();
  int n, k;
  cin >> n >> k;
  vector<pii> v(k);
  for (int i = 0; i < k; i++) {
    cin >> v[i].ff >> v[i].ss;
  }
  vector<int> dp(n + 2, 0);
  dp[n] = 1;
  vector<int> pre(n + 2, 0);
  pre[n] = 1;
  for (int i = n - 1; i >= 1; i--) {
    for (auto x : v) {
      if (i + x.ff <= n)
        dp[i] = (dp[i] + pre[i + x.ff] - pre[min(n, i + x.ss) + 1]) % MOD;
    }
    pre[i] = (dp[i] + pre[i + 1]) % MOD;
  }
  cout << (dp[1] + MOD) % MOD << endl;
}


Error: TLE

Fixed submission:

/*
    हॉंजी । की हाल चाल?
    बड़ीया सब?
*/

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

#define ff first
#define ss second
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define vi vector<int>
#define mii unordered_map<int, int>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define MOD 998244353
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define w(x)                                                                   \\
  int x;                                                                       \\
  cin >> x;                                                                    \\
  while (x--)
#define EPS 1e-9

void gogeta() {
  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
}

int MAX = 2 * 100000 + 5;
vector<pii> v(15);
vector<int> memo(MAX, -1);

bool isValid(int i) {
  for (auto x : v) {
    if (i >= x.ff && i <= x.ss)
      return true;
  }
  return false;
}

int util(int i, int n) {
  if (i > n)
    return 0;
  if (i == n)
    return 1;
  if (memo[i] != -1)
    return memo[i];

  int ans = 0;
  for (int d = 1; d <= n - i; d++) {
    if (isValid(d))
      ans = (ans + util(i + d, n)) % MOD;
  }
  return memo[i] = ans;
}

int32_t main() {
  gogeta();
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    cin >> v[i].ff >> v[i].ss;
  }
  memo.resize(MAX, -1);
  cout << util(1, n) << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const long long INF = 1001001001;
const long long MOD = 998244353;
const double EPS = 1e-10;

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

  ll n, k;
  cin >> n >> k;
  ll l[k], r[k];
  for (ll i = 0; i < k; i++)
    cin >> l[i] >> r[i];
  ll imos[202020] = {};
  imos[1] = 1;
  for (ll i = 1; i < n; i++) {
    imos[i] += imos[i - 1];
    imos[i] %= MOD;
    for (ll j = 0; j < k; j++) {
      ll x = i + l[j], y = i + r[j];
      if (x > n)
        continue;
      y = min(y, n);
      imos[x] += imos[i];
      imos[x] %= MOD;
      imos[y + 1] -= imos[i];
      while (imos[y + 1] < 0)
        imos[y + 1] += MOD;
    }
  }
  cout << (imos[n]) % MOD << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const long long INF = 1001001001;
const long long MOD = 998244353;
const double EPS = 1e-10;

template <typename T> struct BIT {
  ll n = 0, msb = 0;
  vector<T> d;
  BIT(ll m) : d(m + 1) {
    n = d.size();
    for (ll i = 31; i >= 0; i--) {
      if (n >> i & 1) {
        msb = i;
        break;
      }
    }
  }
  void add(ll i, T x) {
    while (x < 0)
      x += MOD;
    x %= MOD;
    while (i < n) {
      d[i] += x;
      while (d[i] < 0)
        d[i] += MOD;
      d[i] %= MOD;
      i += i & -i;
    }
  }
  T sum(ll i) {
    T res = T();
    while (i > 0) {
      res += d[i];
      while (res < 0)
        res += MOD;
      res %= MOD;
      i -= i & -i;
    }
    return res;
  }
};

template <typename T> struct RangeAddBIT {
  BIT<ll> bit0, bit1;
  RangeAddBIT(ll n) : bit0(n + 1), bit1(n + 1) {}
  void add(ll l, ll r, T x) {
    bit0.add(l, -x * (l - 1));
    bit1.add(l, x);
    bit0.add(r + 1, x * r);
    bit1.add(r + 1, -x);
  }
  T sum(ll i) { return ((bit1.sum(i) * i) % MOD + bit0.sum(i)) % MOD; }
};

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

  ll n, k;
  cin >> n >> k;
  ll l[k], r[k];
  int cnt[202020] = {};
  vector<pair<ll, ll>> lr;
  for (int i = 0; i < k; i++) {
    cin >> l[i] >> r[i];
    cnt[l[i]]++;
    cnt[r[i] + 1]--;
  }
  for (int i = 1; i < 202020; i++)
    cnt[i] += cnt[i - 1];
  int le = -1, ri = -1;
  for (int i = 0; i < 202020; i++) {
    if (cnt[i] == 0) {
      if (lr.empty()) {
        if (le == -1)
          continue;
        else {
          lr.emplace_back(pair<int, int>(le, ri));
          le = ri = -1;
        }
      } else if (le != -1) {
        lr.emplace_back(pair<int, int>(le, ri));
        le = ri = -1;
      }
    } else {
      if (le == -1) {
        le = ri = i;
      } else {
        ri++;
      }
    }
  }
  RangeAddBIT<ll> bit(202020);
  bit.add(1, 1, 1);
  for (int i = 1; i < n; i++) {
    ll t = bit.sum(i) - bit.sum(i - 1);
    while (t < 0)
      t += MOD;
    t %= MOD;
    for (int j = 0; j < lr.size(); j++) {
      ll x = i + lr[j].first, y = i + lr[j].second;
      if (x > n)
        continue;
      y = min(y, n);
      bit.add(x, y, t);
    }
  }
  ll ans = bit.sum(n) - bit.sum(n - 1);
  while (ans < 0)
    ans += MOD;
  ans %= MOD;
  cout << ans << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <vector>
typedef long long ll;

const ll mod = 998244353;
ll dp[202020];
std::vector<int> v[10];

struct Node {
  ll sum, lazy;
} tree[808080];

void lazy(int node, int l, int r) {
  tree[node].sum += tree[node].lazy * (r - l + 1);
  tree[node].sum %= mod;
  if (l != r) {
    tree[node * 2].lazy += tree[node].lazy;
    tree[node * 2 + 1].lazy += tree[node].lazy;
  }
  tree[node].lazy = 0;
}

Node merge(Node o1, Node o2) {
  Node me;
  me.sum = (o1.sum + o2.sum) % mod;
  me.lazy = 0;
  return me;
}

void updateRange(int node, int l, int r, int s, int e, ll x) {
  if (e < s)
    return;
  lazy(node, l, r);
  if (s > r || e < l) {
    return;
  }
  if (s <= l && e >= r) {
    tree[node].lazy += x;
    lazy(node, l, r);
    return;
  }
  int m = (l + r) >> 1;
  updateRange(node * 2, l, m, s, e, x);
  updateRange(node * 2 + 1, m + 1, r, s, e, x);
  tree[node] = merge(tree[node * 2], tree[node * 2 + 1]);
}

ll query(int node, int l, int r, int idx) {
  lazy(node, l, r);
  if (idx > r || idx < l) {
    return 0;
  }
  if (l == r) {
    return tree[node].sum % mod;
  }
  int m = (l + r) >> 1;
  return query(node * 2, l, m, idx) + query(node * 2 + 1, m + 1, r, idx);
}

void init(int node, int l, int r) {
  if (l == r) {
    tree[node] = {dp[l], 0};
    return;
  }
  int m = (l + r) >> 1;
  init(node * 2, l, m);
  init(node * 2 + 1, m + 1, r);
  tree[node] = merge(tree[node * 2], tree[node * 2 + 1]);
}

int main(void) {
  int N, K;
  scanf("%d%d", &N, &K);

  dp[0] = 1;
  for (int i = 0; i < K; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    v[i].push_back(x);
    v[i].push_back(y);
  }
  init(1, 0, N - 1);

  for (int j = 0; j < N; j++) {
    for (int i = 0; i < K; i++) {
      int st = j + v[i][0];
      int ed = std::min(j + v[i][1], N - 1);
      updateRange(1, 0, N - 1, st, ed, query(1, 0, N - 1, j));
    }
  }
  printf("%lld\\n", query(1, 0, N - 1, N - 1));

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>
typedef long long ll;

const ll mod = 998244353;
ll dp[202020];
std::vector<int> v;

ll dfs(int node) {
  ll res = 0;
  if (dp[node] >= 0)
    return dp[node];

  for (int i = 0; i < v.size(); i++) {
    if (node <= v[i])
      break;
    res = (res + dfs(node - v[i])) % mod;
  }

  return dp[node] = res % mod;
}

int main(void) {
  int N, K;
  scanf("%d%d", &N, &K);

  for (int i = 0; i < K; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    for (int j = x; j <= y; j++) {
      v.push_back(j);
    }
  }
  std::sort(v.begin(), v.end());
  std::fill_n(dp, 202020, -1);
  dp[1] = 1;
  dfs(N);

  printf("%lld\\n", dp[N] % mod);

  return 0;
}