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

#include <bits/stdc++.h>
typedef long long int lli;

#define em emplace_back
#define rep(i, j, n) for (lli i = j; i < n; i++)
#define repf(i, a, j, n) for (lli i = j, a = 0; i < n; i++, a++)
#define repg(i, j, n) for (int i = j; i > n; --i)
using namespace std;
lli m = 998244353;

void solve() {

  lli n, k;
  cin >> n >> k;

  vector<lli> temp(n + 1, 0);

  vector<pair<lli, lli>> v;

  rep(i, 0, k) {
    lli a, b;
    cin >> a >> b;

    v.em(make_pair(a, b));
  }

  sort(v.begin(), v.end());
  lli ans = 0;

  temp[1] = 1;

  lli current = 0;
  rep(i, 1, n) {
    temp[i] %= m;
    current = (current + temp[i]);
    if (current <= 0) {
      continue;
    }

    rep(j, 0, v.size()) {
      lli f = i + v[j].first;
      lli l = i + v[j].second;
      if (f > n) {
        break;
      } else {
        temp[f] = (temp[f] + (current));
      }
      if (l + 1 > n) {
        continue;
      } else {
        temp[l + 1] = (temp[l + 1] - current);
      }
    }
    temp[i] = temp[i] % m;
  }

  cout << temp[n] % m;
}

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

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long int lli;

#define em emplace_back
#define rep(i, j, n) for (lli i = j; i < n; i++)
#define repf(i, a, j, n) for (lli i = j, a = 0; i < n; i++, a++)
#define repg(i, j, n) for (int i = j; i > n; --i)
using namespace std;
lli m = 998244353;

void solve() {

  lli n, k;
  cin >> n >> k;

  vector<lli> arrfront(n + 1, 0);

  vector<pair<lli, lli>> v;
  unordered_map<lli, lli> s;

  rep(i, 0, k) {
    lli a, b;
    cin >> a >> b;

    v.em(make_pair(a, b));
  }

  vector<lli> arrback(n + 1, 0);
  sort(v.begin(), v.end());
  lli ans = 0;
  rep(i, 1, n) {

    if (arrfront[i] == 0) {
      if (i != 1) {
        continue;
      } else {
        arrfront[i] = 1;
      }
    }

    rep(j, 0, v.size()) {
      if (i + v[j].first > n) {
        break;
      }
      rep(k, v[j].first, v[j].second + 1) {
        if (i + k > n) {
          break;
        }
        arrfront[i + k] = arrfront[i + k] % m;
        arrfront[i + k] = (arrfront[i + k] % m + arrfront[i] % m) % m;
        arrfront[i + k] = arrfront[i + k] % m;
      }
    }
  }

  cout << (arrfront[n] % m);
}

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

  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define MOD 998244353
#define N 200005
int n, k;
vector<pair<int, int>> d;
long dp[N], sumdp[N];
int dcnt = 0;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> k;
  d.reserve(k + 1);
  for (int i = 0; i < k; i++) {
    int s, e;
    cin >> s >> e;
    d.push_back({s, e});
  }
  dp[1] = 1;
  sumdp[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (const auto &sect : d) {
      if (sect.first > i)
        continue;
      int ubound = i - sect.first;
      int lbound = max(i - sect.second, 1);
      dp[i] = (dp[i] + MOD + sumdp[ubound] - sumdp[lbound - 1]) % MOD;
    }
    sumdp[i] = (sumdp[i - 1] + dp[i]) % MOD;
  }
  cout << dp[n] << '\\n';

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MOD 998244353
#define N 200005
int n, k;
vector<pair<int, int>> d;
long dp[N], sumdp[N];
int dcnt = 0;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> k;
  d.reserve(k + 1);
  for (int i = 0; i < k; i++) {
    int s, e;
    cin >> s >> e;
    d.push_back({s, e});
  }
  dp[1] = 1;
  sumdp[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (const auto &sect : d) {
      if (sect.first > i)
        continue;
      int ubound = i - sect.first;
      int lbound = min(i - sect.second, i);
      dp[i] = (dp[i] + MOD + sumdp[ubound] - sumdp[lbound - 1]) % MOD;
    }
    sumdp[i] = (sumdp[i - 1] + dp[i]) % MOD;
  }
  cout << dp[n] << '\\n';

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

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

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

  // 総和を N に
  --N;
  vector<ll> dp(N + 1, 0), sdp(N + 2, 0);
  dp[0] = 1, sdp[1] = 1;
  for (int n = 1; n <= N; ++n) {
    for (auto p : v) {
      int left = max(0, n - p.second);
      int right = max(0, n - p.first + 1);
      int diff = sdp[right] - sdp[left];
      if (diff < 0)
        diff += mod;
      dp[n] += diff;
      dp[n] %= mod;
    }
    sdp[n + 1] = sdp[n] + dp[n];
    sdp[n + 1] %= mod;
  }
  cout << dp[N] << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < int(n); i++)
const ll mod = 998244353;
ll dp[200005];

int main() {
  int n, k;
  cin >> n >> k;
  set<int> st;
  rep(i, k) {
    int x, y;
    cin >> x >> y;
    for (int i = x; i <= y; i++) {
      st.insert(i);
    }
  }
  dp[1] = 1LL;

  for (int i = 1; i <= n; i++) {
    for (auto q : st) {
      if (q + i > n)
        break;
      else {
        dp[i + q] += dp[i];
        dp[i + q] %= mod;
      }
    }
  }
  cout << dp[n] << endl;

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

#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main() {
  // cout << "" << endl; // prints
  int N, K;
  const long long int mod = 998244353;
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> K;
  long long int dp[N + 1];
  dp[0] = 1;
  dp[1] = -1;
  dp[2] = 0;
  for (int a = 3; a <= N; a++) {
    dp[a] = 0;
  }
  long long int LR[K + 1][3];
  bool TF[N + 1];
  for (int i = 0; i <= N; i++) {
    TF[i] = false;
  }
  for (int i = 1; i <= K; i++) {
    cin >> LR[i][1] >> LR[i][2];
    for (int j = LR[i][1]; j <= LR[i][2]; j++) {
      TF[j] = true;
    }
  }

  for (int i = 0; i < N; i++) {
    if (i > 0)
      dp[i] += dp[i - 1];
    dp[i] = dp[i] % mod;
    for (int j = 1; j <= K; j++) {
      if (i + LR[j][1] < N) {
        dp[i + LR[j][1] + 0] += dp[i];
        dp[i + LR[j][1] + 0] = dp[i + LR[j][1] + 0] % mod;
      }
      if (i + LR[j][2] + 1 < N) {
        dp[i + LR[j][2] + 1] -= dp[i];
        dp[i + LR[j][2] + 1] = dp[i + LR[j][2] + 1] % mod;
      }
    }
  }
  dp[N - 1] = dp[N - 1] % mod;
  if (dp[N - 1] < 0) {
    dp[N - 1] += mod;
  }
  cout << dp[N - 1] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int main() {
  // cout << "" << endl; // prints
  int N, K;
  const int mod = 998244353;
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> K;
  int dp[N + 1];
  dp[0] = 0;
  dp[1] = 1;
  for (int a = 2; a <= N; a++) {
    dp[a] = 0;
  }
  int LR[K + 1][3];
  bool TF[N + 1];
  for (int i = 0; i <= N; i++) {
    TF[i] = false;
  }
  for (int i = 1; i <= K; i++) {
    cin >> LR[i][1] >> LR[i][2];
    for (int j = LR[i][1]; j <= LR[i][2]; j++) {
      TF[j] = true;
    }
  }

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j < i; j++) {
      if (TF[i - j] == true) {
        dp[i] += dp[j];
      }
      dp[i] = dp[i] % mod;
    }
  }
  cout << dp[N] << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define ALL(v) v.begin(), v.end()
#define V vector
#define P pair
using ll = long long;
const ll MOD = 998244353;

int main() {
  int n, k;
  cin >> n >> k;
  V<P<int, int>> v(k);
  for (int i = 0; i < k; i++)
    cin >> v[i].first >> v[i].second;

  V<ll> dp(n, 0), s(n + 1, 0);
  dp[0] = 1, s[1] = 1;
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < k; j++) {
      int l = max(0, i - v[j].second);
      int r = max(0, i - v[j].first + 1);
      dp[i] += (s[r] - s[l] + MOD) % MOD;
      dp[i] %= MOD;
    }
    s[i + 1] = dp[i] + s[i];
    s[i + 1] %= MOD;
  }
  cout << dp[n - 1] << endl;

  return 0;
}


Error: TLE

Fixed submission:

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

#define ALL(v) v.begin(), v.end()
#define V vector
#define P pair
using ll = long long;
const ll MOD = 998244353;

int main() {
  int n, k;
  cin >> n >> k;
  set<int> se;
  for (int i = 0; i < k; i++) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; j++) {
      se.insert(j);
    }
  }

  V<ll> dp(n + 1, 0);
  dp[1] = 1;
  for (int i = 1; i < n; i++) {
    for (auto s : se) {
      if (i + s <= n) {
        dp[i + s] += dp[i];
        dp[i + s] %= MOD;
      }
    }
  }
  cout << dp[n] % MOD << endl;

  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 998244353;
const ll INF = (1ll << 60);

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

  vector<ll> l(k), r(k), dp(n + 100, 0), s(n + 100, 0);

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

  dp[0] = 1;
  s[1] = 1;

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < k; j++) {
      ll x, y;

      if (i - l[j] < 0) {
        continue;
      }

      x = max(i - l[j], 0ll) + 1; // big
      y = max(i - r[j], 0ll);     // small

      dp[i] += s[x] - s[y] + MOD;
      dp[i] %= MOD;
    }

    s[i + 1] = s[i] + dp[i] + MOD;
    s[i + 1] %= MOD;
  }

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


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 998244353;
const ll INF = (1ll << 60);

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

  vector<ll> l(k), r(k), dp(n + 1, 0), s(n + 1, 0);

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

  dp[0] = 1;
  s[1] = 1;

  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      ll x, y;

      if (i - l[j] < 0) {
        continue;
      }

      x = max(i - l[j], 0ll) + 1; // big
      y = max(i - r[j], 0ll);     // small

      dp[i] += s[x] - s[y] + MOD;
      dp[i] %= MOD;
    }

    s[i + 1] = s[i] + dp[i] + MOD;
    s[i + 1] %= MOD;
  }

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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 998244353;
const ll INF = (1ll << 60);

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

  vector<ll> l(k), r(k), dp(n, 0), s(n + 1, 0);

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

  dp[0] = 1;
  s[1] = 1;

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < k; j++) {
      ll x, y;

      if (i - l[j] < 0) {
        continue;
      }

      x = max(i - l[j], 0ll) + 1; // big
      y = max(i - r[j], 0ll);     // small

      dp[i] += s[x] - s[y] + MOD;
      dp[i] %= MOD;
    }

    s[i + 1] = s[i] + dp[i] + MOD;
    s[i + 1] %= MOD;
  }

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


Error: WA

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <utility>
#include <vector>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 998244353;
const ll INF = (1ll << 60);

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

  vector<ll> l(k), r(k), dp(n, 0), s(n + 1, 0);

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

  dp[0] = 1;
  s[1] = 1;

  for (int i = 1; i < n; i++) {
    for (int j = 0; j < k; j++) {
      ll x, y;

      if (i - l[j] < 0) {
        continue;
      }

      x = i - l[j] + 1; // big
      y = i - r[j];     // small

      dp[i] += s[x] - s[y] + MOD;
      dp[i] %= MOD;
    }

    s[i + 1] = s[i] + dp[i] + MOD;
    s[i + 1] %= MOD;
  }

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

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

using namespace std;

typedef long long ll;

const ll MOD = 998244353;

ll sum(ll a, ll b) { return (a + b) % MOD; }

struct segment_tree {
  vector<ll> t;
  segment_tree(int n) { t.resize(n << 2); }
  void update(int v, int tl, int tr, int l, int r, int val) {
    if (l > r)
      return;
    if (l == tl && r == tr) {
      t[v] = sum(t[v], val);
      return;
    }
    int tm = (tl + tr) / 2;
    t[v * 2] = sum(t[v * 2], t[v]);
    t[v * 2 + 1] = sum(t[v * 2 + 1], t[v]);
    t[v] = 0;
    update(v * 2, tl, tm, l, min(r, tm), val);
    update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, val);
  }
  ll get(int v, int tl, int tr, int pos) {
    if (tl == tr)
      return t[v];
    int tm = (tl + tr) / 2;
    t[v * 2] = sum(t[v * 2], t[v]);
    t[v * 2 + 1] = sum(t[v * 2 + 1], t[v]);
    t[v] = 0;
    if (pos <= tm)
      return get(v * 2, tl, tm, pos);
    else
      return get(v * 2 + 1, tm + 1, tr, pos);
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> a(k);
  for (int i = 0; i < k; i++)
    cin >> a[i].first >> a[i].second;
  segment_tree se(n);
  se.update(1, 0, n - 1, 0, 0, 1);
  for (int i = 0; i < n; i++) {
    ll val = se.get(1, 0, n - 1, i);
    for (auto &now : a)
      se.update(1, 0, n - 1, i + now.first, min(i + now.second, n - 1), val);
  }
  cout << se.get(1, 0, n - 1, n - 1);
  return 0;
}


Error: WA

Fixed submission:

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

using namespace std;

typedef long long ll;

const ll MOD = 998244353;

ll sum(ll a, ll b) { return (a + b) % MOD; }

struct segment_tree {
  vector<ll> t;
  segment_tree(int n) { t.resize(n << 2); }
  void update(int v, int tl, int tr, int l, int r, int val) {
    if (l > r)
      return;
    if (l == tl && r == tr) {
      t[v] = sum(t[v], val);
      return;
    }
    int tm = (tl + tr) / 2;
    t[v * 2] = sum(t[v * 2], t[v]);
    t[v * 2 + 1] = sum(t[v * 2 + 1], t[v]);
    t[v] = 0;
    update(v * 2, tl, tm, l, min(r, tm), val);
    update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, val);
  }
  ll get(int v, int tl, int tr, int pos) {
    if (tl == tr)
      return t[v];
    int tm = (tl + tr) / 2;
    t[v * 2] = sum(t[v * 2], t[v]);
    t[v * 2 + 1] = sum(t[v * 2 + 1], t[v]);
    t[v] = 0;
    if (pos <= tm)
      get(v * 2, tl, tm, pos);
    else
      get(v * 2 + 1, tm + 1, tr, pos);
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> a(k);
  for (int i = 0; i < k; i++)
    cin >> a[i].first >> a[i].second;
  segment_tree se(n);
  se.update(1, 0, n - 1, 0, 0, 1);
  for (int i = 0; i < n; i++) {
    ll val = se.get(1, 0, n - 1, i);
    for (auto &now : a)
      se.update(1, 0, n - 1, i + now.first, min(i + now.second, n - 1), val);
  }
  cout << se.get(1, 0, n - 1, n - 1);
  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++)
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
const int inf = 1001001001;

const ll mod = 998244353;
int main() {
  ll n, k;
  cin >> n >> k;

  vector<pair<int, int>> s(0);
  rep(i, k) {
    int l, r;
    cin >> l >> r;
    s.push_back({l, r});
  }

  static ll dp[200005] = {0};
  static ll dpsum[200005] = {0};
  dp[1] = 1;
  dpsum[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      int li = i - s[j].second;
      int ri = i - s[j].first;
      if (ri < 0)
        continue;
      li = max(1, li);
      dp[i] += dpsum[ri] - dpsum[li - 1] + mod;
      dp[i] %= mod;
    }
    dpsum[i] = dpsum[i - 1] + dp[i];
    dpsum[i] %= mod;
  }
  cout << dp[n] << endl;
}


Error: TLE

Fixed submission:

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

const ll mod = 998244353;
int main() {
  ll n, k;
  cin >> n >> k;

  vector<pair<int, int>> s(0);
  rep(i, k) {
    int l, r;
    cin >> l >> r;
    s.push_back({l, r});
  }

  static ll dp[2000000] = {0};
  dp[0] = 1;
  rep(i, n) {
    for (auto p : s) {
      for (int j = p.first; j <= p.second; j++) {
        dp[i + j] += dp[i];
        dp[i + j] %= mod;
      }
    }
  }
  cout << dp[n - 1] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <math.h>
#include <stdio.h>
#include <string.h>

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

constexpr long long INFLL = 1e18;

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

int main() {
  const int mPN = 998244353;
  ll n, k;
  cin >> n >> k;
  vector<ll> l(k);
  vector<ll> r(k);

  for (int i = 0; i < k; i++) {
    cin >> l.at(i) >> r.at(i);
  }

  vector<ll> alSum(n + 1, 0);
  vector<ll> dp(n + 1, 0);

  dp.at(1) = 1;
  alSum.at(1) = 1;

  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      ll tempL, tempR;
      tempL = i - l.at(j);
      tempR = i - r.at(j);
      if (tempR >= 1) {
        if (tempR == tempL) {
          dp.at(i) += dp.at(tempL);
        } else {
          dp.at(i) += alSum.at(tempL) - alSum.at(tempR - 1);
        }
      } else if (tempL >= 0) {
        dp.at(i) += alSum.at(tempL) - alSum.at(0);
      }
      dp.at(i) %= mPN;
    }
    alSum.at(i) = alSum.at(i - 1) + dp.at(i);
    alSum.at(i) %= mPN;
  }
  if (dp.at(n) < 0) {
    dp.at(n) += mPN;
  }
  cout << dp.at(n) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <math.h>
#include <stdio.h>
#include <string.h>

#include <algorithm>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

constexpr long long INFLL = 1e18;

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

int main() {
  const int mPN = 998244353;
  int n, k;
  cin >> n >> k;
  int l, r;
  vector<int> v;
  for (int i = 0; i < k; i++) {
    cin >> l >> r;

    for (int j = l; j <= r; j++) {
      v.push_back(j);
    }
  }
  sort(v.begin(), v.end());
  vector<int> dp(n, 0);
  dp.at(0) = 1;
  int temp;
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < v.size(); j++) {
      temp = i - v.at(j);
      if (temp >= 0) {
        dp.at(i) += dp.at(temp);
        dp.at(i) %= mPN;
      } else {
        break;
      }
    }
  }

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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; ++i)
#define debug(s, param) std::cerr << s << param << std::endl;

using namespace std;
using ll = long long;

const ll MOD = 998244353;

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

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; ++i)
#define debug(s, param) std::cerr << s << param << std::endl;

using namespace std;
using ll = long long;

const ll MOD = 998244353;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<ll> nums;
  rep(i, k) {
    int l, r;
    cin >> l >> r;
    for (int i = l; i <= r; ++i) {
      nums.emplace_back(i);
    }
  }
  // sort(nums.begin(), nums.end());
  vector<int> dp(n + 1, 0);
  dp[1] = 1;
  for (int i = 2; i <= n; ++i) {
    for (auto num : nums) {
      if (i >= num) {
        dp[i] += (dp[i - num] % MOD);
        dp[i] = (dp[i] % MOD);
      }
      // if (i < num) break;
      // dp[i] += (dp[i-num] % MOD);
      // dp[i] = (dp[i] % MOD);
    }
  }
  cout << dp[n] << endl;
  return 0;
}