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

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

typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<long long> vll;
typedef long long ll;
typedef vector<int> vi;

const int INF = 1e6;

using namespace std;

#define MOD 998244353

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

  //    freopen("out.txt", "w", stdout);

  int N, K;
  cin >> N >> K;

  int v[N + 5], L[K], R[K];
  int prefix[N + 5];
  fill(v, v + N + 5, 0);
  fill(prefix, prefix + N + 5, 0);
  v[N] = 1;
  prefix[N] = 1;

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

  for (int j = N - 1; j >= 1; j--) {
    for (int k = 0; k < K; k++) {
      int right = min(N + 1, j + R[k]), left = min(N + 1, j + L[k]);
      v[j] = (v[j] + (prefix[left] - prefix[right + 1] + MOD) % MOD) % MOD;
    }
    prefix[j] = (prefix[j + 1] + v[j]) % MOD;
  }

  cout << v[1];

  return 0;
}


Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

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

typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<long long> vll;
typedef long long ll;
typedef vector<int> vi;

const int INF = 1e9;

using namespace std;

#define MOD 998244353

#define LSOne(S) ((S) & -(S)) // the key operation

class FenwickTree { // index 0 is not used
private:
  vll ft; // internal FT is an array
public:
  FenwickTree(int m) { ft.assign(m + 1, 0); } // create an empty FT

  ll rsq(int j) { // returns RSQ(1, j)
    ll sum = 0;
    for (; j; j -= LSOne(j))
      sum = (sum % MOD + ft[j] % MOD) % MOD;
    return sum;
  }

  ll rsq(int i, int j) { return rsq(j) - rsq(i - 1); } // inc/exclusion

  // updates value of the i-th element by v (v can be +ve/inc or -ve/dec)
  void update(int i, ll v) {
    for (; i < (int)ft.size(); i += LSOne(i))
      ft[i] = (ft[i] % MOD + v % MOD) % MOD;
  }
};

int range(int x, int N) {
  if (x > N)
    return N + 1;
  else
    return x;
}

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

  //    freopen("out.txt", "w", stdout);

  int N, K;
  cin >> N >> K;

  int v[N + 5], L[K], R[K];
  fill(v, v + N + 5, 0);
  FenwickTree ft(INF);
  v[N] = 1;
  ft.update(N, 1);
  for (int i = 0; i < K; i++)
    cin >> L[i] >> R[i];

  for (int j = N - 1; j >= 1; j--) {
    for (int k = 0; k < K; k++) {
      v[j] =
          (v[j] % MOD + ft.rsq(range(j + L[k], N), range(j + R[k], N)) % MOD) %
          MOD;
    }
    ft.update(j, v[j]);
  }

  cout << v[1];

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

#include <bits/stdc++.h>
#define int long long
using namespace std;
int f[200005], l[25], p[25], qzh[200005];
signed main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    cin >> l[i] >> p[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 - p[j] - 1, 0ll)]) % 998244353 +
           998244353) %
          998244353;
      f[i] %= 998244353;
    }
    qzh[i] = qzh[i - 1] + f[i];
    qzh[i] %= 998244353;
  }
  cout << f[n];
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
int f[200005], l[25];
signed main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    cin >> l[i * 2 - 1] >> l[i * 2];
  }
  f[0] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= k * 2; j++) {
      if (l[j] >= i) {
        f[i] += f[i - l[j]];
        f[i] %= 998244353;
      }
    }
  }
  cout << f[n];
  return 0;
}
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;

vector<int> L, R;
vector<ll> dp;

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

  int n, k;
  cin >> n >> k;
  dp.resize(n + 1);
  L.resize(k);
  R.resize(k);
  dp[1] = 1;
  dp[0] = 0;
  for (int i = 0; i < k; i++)
    cin >> L[i] >> R[i];

  for (int i = 2; i <= n; i++) {
    dp[i] = dp[i - 1];
    for (int j = 0; j < k; j++)
      if (i - L[j] > 0)
        dp[i] = (dp[i] + dp[i - L[j]] - dp[max(0, i - R[j] - 1)] + mod) % mod;
  }

  cout << (dp[n] - dp[n - 1] + mod) % mod << '\\n';
  cin.ignore(2);
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
const ll mod = 998244353;

vector<int> L, R;
vector<ll> dp;

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

  int n, k;
  cin >> n >> k;
  dp.resize(n + 1, 0);
  L.resize(k);
  R.resize(k);
  dp[1] = 1;
  for (int i = 0; i < k; i++)
    cin >> L[i] >> R[i];

  for (int i = 2; i <= n; i++) {
    dp[i] = dp[i - 1];
    for (int j = 0; j < k; j++)
      if (i - L[i] > 0)
        dp[i] = (dp[i] + dp[i - L[j]] - dp[max(0, i - R[j] - 1)] + mod) % mod;
  }

  cout << dp[n] - dp[n - 1] << '\\n';
  cin.ignore(2);
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const long long INF = INT_MAX / 4;
const long long MOD = 998244353;
const double EPS = 1e-14;
const bool DEBUG = false;

const string YES = "YES";
const string NO = "NO";
const string Yes = "Yes";
const string No = "No";

template <class T> void debug(T head) {
  if (DEBUG) {
    cout << head << endl;
  }
}

template <class Head, class... Body> void debug(Head head, Body... body) {
  if (DEBUG) {
    cout << head << " ";
    debug(body...);
  }
}

/////
void answer() {
  long N, K;
  cin >> N >> K;
  vector<int> L(K), R(K);
  vector<long> dp(N + 1, 0), csum(N + 1, 0);
  for (int i = 0; i < K; ++i) {
    cin >> L[i] >> R[i];
  }

  dp[0] = 0;
  dp[1] = 1;
  csum[0] = 0;
  csum[1] = dp[1];
  for (int i = 2; i <= N; ++i) {
    for (int j = 0; j < K; ++j) {
      dp[i] += csum[max(0, i - L[j])] - csum[max(0, i - R[j] - 1)] + MOD;
      debug(i - L[j], csum[max(0, i - L[j])], i - R[j] - 1,
            csum[max(0, i - R[j] - 1)]);
    }
    dp[i] %= MOD;
    csum[i] = (csum[i - 1] + dp[i]) % MOD;
    // for(auto x : dp){
    //     cout<<x<<" ";
    // }
    // cout<<endl;
    // for(auto x : csum){
    //     cout<<x<<" ";
    // }
    // cout<<endl;
  }

  cout << dp[N] << endl;

  return;
}

/////

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout.precision(16);

  answer();

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

const long long INF = INT_MAX / 4;
const long long MOD = 998244353;
const double EPS = 1e-14;
const bool DEBUG = false;

const string YES = "YES";
const string NO = "NO";
const string Yes = "Yes";
const string No = "No";

template <class T> void debug(T head) {
  if (DEBUG) {
    cout << head << endl;
  }
}

template <class Head, class... Body> void debug(Head head, Body... body) {
  if (DEBUG) {
    cout << head << " ";
    debug(body...);
  }
}

/////
void answer() {
  long N, K;
  cin >> N >> K;
  vector<unsigned long long> L(K), R(K);
  vector<unsigned long long> dp(N, 0), csum(N, 0);
  for (int i = 0; i < K; ++i) {
    cin >> L[i] >> R[i];
  }

  dp[0] = 1;
  csum[0] = dp[0];
  for (int i = 1; i < N; ++i) {
    for (int j = 0; j < K; ++j) {
      dp[i] += csum[max(0ull, i - L[j])] - csum[max(0ull, i - R[j] - 1)] + MOD;
      dp[i] %= MOD;
    }
    dp[i] %= MOD;
    debug(dp[i]);
    csum[i] = (csum[i - 1] + dp[i]) % MOD;
  }

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

  return;
}

/////

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout.precision(16);

  answer();

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

#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define VAL 100000
#define ll long long
#define INF 0x3f3f3f3f
#define pll pair<long long, long long>
const int MAX = 5e6 + 9;
const ll mod = 998244353;
const int TOT_PRIMES = 19;
const int MAX_A = 70;
using namespace std;
vector<int> adj[MAX];
int prime[MAX];
ll dp[MAX];
int main() {

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

  memset(dp, 0, sizeof(dp));
  dp[0] = 0;
  dp[1] = 1;
  vector<pair<int, int>> v;
  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    v.push_back({x, y});
  }
  for (int i = 1; i <= n + 5; i++) {
    if (i > 2)
      dp[i] += dp[i - 1];

    ll now = dp[i] % mod;
    for (int j = 0; j < v.size(); j++) {
      dp[i + v[j].first] = (dp[i + v[j].first] + now);
      dp[i + v[j].second + 1] = (dp[i + v[j].second + 1] - now);
    }
    // cout<<dp[i]<<endl;
  }
  cout << dp[n] % mod;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define VAL 100000
#define ll long long
#define INF 0x3f3f3f3f
#define pll pair<long long, long long>
const int MAX = 1e6 + 9;
const ll mod = 998244353;
const int TOT_PRIMES = 19;
const int MAX_A = 70;
using namespace std;
vector<int> adj[MAX];
int prime[MAX];
int main() {

  int n, k;
  cin >> n >> k;
  ll dp[n + 10000];
  memset(dp, 0, sizeof(dp));
  dp[0] = 0;
  dp[1] = 1;
  vector<pair<int, int>> v;
  for (int i = 0; i < k; i++) {
    int x, y;
    cin >> x >> y;
    v.push_back({x, y});
  }
  for (int i = 1; i <= n + 5; i++) {
    if (i > 2)
      dp[i] += dp[i - 1];

    ll now = dp[i] % mod;
    for (int j = 0; j < v.size(); j++) {
      dp[i + v[j].first] = (dp[i + v[j].first] + now);
      dp[i + v[j].second + 1] = (dp[i + v[j].second + 1] - now);
    }
    // cout<<dp[i]<<endl;
  }
  cout << dp[n] % mod;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
int q, x, i, k, l[N], r[N], j, M = 998244353;
long long dp[N * 2], n, ans, s[N * 2];
int main() {
  scanf("%d %d", &n, &k);
  for (i = 1; i <= k; ++i)
    scanf("%d %d", &l[i], &r[i]);
  dp[n] = 1;
  s[n] = 1;
  for (i = n - 1; i >= 0; --i) {
    for (j = 1; j <= k; ++j)
      dp[i] = (dp[i] + s[i + l[j]] - s[i + r[j] + 1]) % M;
    s[i] = (dp[i] + s[i + 1]) % M;
  }
  cout << (dp[1] % M + M) % M << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 998244353;
int n, k, f[200001];
vector<int> v;
signed main() {
  scanf("%lld %lld", &n, &k);
  for (int i = 1; i <= k; i++) {
    int l, r;
    scanf("%lld %lld", &l, &r);
    for (int j = l; j <= r; j++)
      v.push_back(j);
  }
  f[1] = 1;
  for (int i = 1; i <= n; i++)
    for (int l = 0; l < v.size(); l++)
      if (i >= v[l])
        f[i] = (f[i] + f[i - v[l]]) % mod;
  cout << f[n];
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll MOD = 998244353;
int l[10], r[10];
// dp[i] = 範囲 [1, i] に到着する場合の数
ll dp[200001] = {};
int main() {
  ll n, k;
  cin >> n >> k;
  for (int i = 0; i < k; ++i)
    cin >> l[i] >> r[i];
  dp[1] = 1;
  for (int i = 2; i <= n; ++i) {
    dp[i] = dp[i - 1];
    for (int j = 0; j < k; ++j) {
      int ll = max(1, i - r[j]);
      int rr = min(i - 1, i - l[j]);
      if (ll <= rr) {
        dp[i] += dp[rr] - dp[ll - 1];
      }
    }
    dp[i] = (dp[i] % MOD + MOD) % MOD;
  }
  cout << (dp[n] - dp[n - 1] + MOD) % MOD << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll MOD = 998244353;
int l[10], r[10];
// dp[i] = 範囲 [1, i] に到着する場合の数
ll dp[201] = {};
int main() {
  ll n, k;
  cin >> n >> k;
  for (int i = 0; i < k; ++i)
    cin >> l[i] >> r[i];
  dp[1] = 1;
  for (int i = 2; i <= n; ++i) {
    dp[i] = dp[i - 1];
    for (int j = 0; j < k; ++j) {
      int ll = max(1, i - r[j]);
      int rr = min(i - 1, i - l[j]);
      if (ll <= rr) {
        dp[i] += dp[rr] - dp[ll - 1];
      }
    }
    dp[i] = (dp[i] % MOD + MOD) % MOD;
  }
  cout << (dp[n] - dp[n - 1] + MOD) % MOD << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

// $%D%$.$%M%$.$%Y%$
// $%h%$:$%m%$:$%s%$

#pragma GCC optimize("O3")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)
#define ll long long
#define ld long double
#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)
#define per(i, a, n) for (int i = (int)(n - 1); i >= (int)(a); i--)
#define mp(a, b) make_pair(a, b)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pil pair<int, ll>
#define pli pair<ll, int>
#define fi first
#define se second

const int MOD = 998244353; // = (119<<23)+1
const int MX = 2e5 + 5;
const ll INF = 1e18;
const ld PI = 4 * atan((ld)1);
const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1};

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> dp(n + 1);
  dp[0] = 1;
  vector<int> pref(n + 2);
  pref[1] = 1;
  vector<pii> da(k);

  for (int i = 0; i < k; ++i) {
    cin >> da[i].first >> da[i].second;
  }
  for (int i = 1; i <= n; ++i) {
    for (auto x : da) {
      int l = i - x.second;
      int r = i - x.first + 1;
      dp[i] = (dp[i] + (pref[max(0, r)] - pref[max(0, l)] + MOD) % MOD) % MOD;
    }
    pref[i + 1] = (pref[i] + dp[i]) % MOD;
  }
  cout << dp[n - 1] << '\\n';
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

// $%D%$.$%M%$.$%Y%$
// $%h%$:$%m%$:$%s%$

#pragma GCC optimize("O3")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define IOS                                                                    \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)
#define ll long long
#define ld long double
#define rep(i, a, n) for (int i = (int)(a); i < (int)(n); i++)
#define per(i, a, n) for (int i = (int)(n - 1); i >= (int)(a); i--)
#define mp(a, b) make_pair(a, b)
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pil pair<int, ll>
#define pli pair<ll, int>
#define fi first
#define se second

const int MOD = 998244353; // = (119<<23)+1
const int MX = 2e5 + 5;
const ll INF = 1e18;
const ld PI = 4 * atan((ld)1);
const int xd[4] = {1, 0, -1, 0}, yd[4] = {0, 1, 0, -1};

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> dp(n + 1);
  dp[0] = 1;
  vector<int> pref(n + 1);
  pref[1] = 1;
  vector<pii> da(k);

  for (int i = 0; i < k; ++i) {
    cin >> da[i].first >> da[i].second;
  }
  for (int i = 1; i <= n; ++i) {
    for (auto x : da) {
      int l = i - x.second;
      int r = i - x.first + 1;
      dp[i] = (dp[i] + (pref[max(0, r)] - pref[max(0, l)] + MOD) % MOD) % MOD;
    }
    pref[i + 1] = (pref[i] + dp[i]) % MOD;
  }
  cout << dp[n - 1] << '\\n';
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

/*
    Mbak Sana Dong
    Mbak Chaeyoung Dong
    Mbak Seulgi Dong
*/
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
const int mod = 998244353;
ll dp[200200];
ll bit[200200];
void add(int x, ll val) {
  for (x; x <= 200000; x += (x & -x)) {
    bit[x] += val;
    bit[x] %= mod;
  }
}
ll get(int x) {
  ll res = 0;
  for (x; x > 0; x -= (x & -x)) {
    res += bit[x];
    res %= mod;
  }
  return res;
}
int main() {
  int n, i, j, k, x, y, ans, curr, temp, m;
  int tc;
  cin >> n >> k;
  add(1, 1);
  vector<pair<int, int>> v;
  for (i = 1; i <= k; i++) {
    cin >> x >> y;
    v.push_back({x, y});
  }
  sort(v.begin(), v.end());
  for (i = 2; i <= n; i++) {
    dp[i] = 0;
    for (auto x : v) {
      if (x.first > i - 1)
        break;
      int r = i - x.first;
      int l = i - min(i - 1, x.second);
      dp[i] += get(r);
      dp[i] %= mod;
      dp[i] -= get(l - 1);
      while (dp[i] < 0)
        dp[i] += mod;
    }
    add(i, dp[i]);
  }
  cout << dp[n] << endl;
  return 0;
}


Error: TLE

Fixed submission:

/*
    Mbak Sana Dong
    Mbak Chaeyoung Dong
    Mbak Seulgi Dong
*/
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
const int mod = 998244353;
ll dp[200200];
int main() {
  int n, i, j, k, x, y, ans, curr, temp, m;
  int tc;
  cin >> n >> k;
  dp[1] = 1;
  vector<int> v;
  for (i = 1; i <= k; i++) {
    cin >> x >> y;
    for (j = x; j <= y; j++) {
      v.push_back(j);
    }
  }
  sort(v.begin(), v.end());
  for (i = 2; i <= n; i++) {
    dp[i] = 0;
    for (auto x : v) {
      if (i - x > 0) {
        dp[i] += dp[i - x];
        dp[i] %= mod;
      }
    }
  }
  cout << dp[n] << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

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

ll mod = 998244353;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<pair<ll, ll>> t, ans(n + 250000, {0, 0});
  for (ll _ = 0; _ < k; ++_) {
    pair<ll, ll> p;
    cin >> p.first >> p.second;
    t.push_back(p);
  }

  ll f = 0;
  for (ll i = 1; i <= n; i++) {
    f += ans[i].first;
    if (i == 1 || f > 0) {
      for (auto p : t) {
        ans[i + p.first].first =
            ans[i + p.first].first + (i == 1 ? 1 : f) % mod;
        ans[i + p.second].second =
            ans[i + p.second].second + (i == 1 ? 1 : f) % mod;
      }
    }
    f -= ans[i].second;
  }
  ll aaaa = 0;
  for (ll i = 1; i < n; i++) {
    aaaa = ((aaaa + ans[i].first) % mod - (ans[i].second)) % mod;
  }
  aaaa = (aaaa + ans[n].first) % mod;
  cout << aaaa;
}

Error: WA

Fixed submission:

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

ll mod = 998244353;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<pair<ll, ll>> t, ans(n + 100000, {0, 0});
  for (ll _ = 0; _ < k; ++_) {
    pair<ll, ll> p;
    cin >> p.first >> p.second;
    t.push_back(p);
  }

  ll f = 0;
  for (ll i = 1; i <= n; i++) {
    f += ans[i].first;
    if (i == 1 || f > 0) {
      for (auto p : t) {
        ans[i + p.first].first =
            ans[i + p.first].first + (i == 1 ? 1 : f) % mod;
        ans[i + p.second].second =
            ans[i + p.second].second + (i == 1 ? 1 : f) % mod;
      }
    }
    f -= ans[i].second;
  }
  ll aaaa = 0;
  for (ll i = 1; i < n; i++) {
    aaaa = ((aaaa + ans[i].first) % mod - (ans[i].second)) % mod;
  }
  aaaa = (aaaa + ans[n].first) % mod;
  cout << aaaa;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const long long int INF = 998244353;

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

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

  for (long long i = 2; i <= N; i++) {
    for (int j = 0; j < K; j++) {
      int li = i - R[j];
      int ri = i - L[j];
      if (ri < 1)
        continue;
      li = std::max(li, 1);

      dp[i] += (dpsum[ri] - dpsum[li - 1] + INF) % INF;
      dp[i] %= INF;
    }
    dpsum[i] = dpsum[i - 1] + dp[i];
    dpsum[i] %= INF;
  }

  cout << dp[N] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

const long long int INF = 998244353;

int main(void) {
  long long N, K;
  cin >> N >> K;
  vector<long long> s;
  for (long long i = 0; i < K; i++) {
    long long L, R;
    cin >> L >> R;
    for (long long j = L; j <= R; j++) {
      s.push_back(j);
    }
  }

  long long W = s.size();
  sort(s.begin(), s.end());

  vector<long long> v(N, 0);
  v[0] = 1;

  for (long long i = 0; i < N; i++) {
    for (long long j = 0; j < W; j++) {
      long long next = i + s[j];

      if (next > N - 1)
        break;

      v[next] += v[i];
      v[next] %= INF;
    }
  }

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

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define FORR(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)

#define DEBUG(x) cout << #x << ": " << (x) << endl
#define DEBUG_VEC(v)                                                           \\
  cerr << #v << ": ";                                                          \\
  REP(__i, (v).size()) cerr << ((v)[__i]) << ", ";                             \\
  cerr << endl
#define PRINT(x) cout << x << endl
#define all(x) x.begin(), x.end()
#define MM << " " <<
int main() {
  int N, K;
  cin >> N >> K;

  ll MOD = 998244353;

  set<int> s;
  vector<int> L(K);
  vector<int> R(K);
  vector<ll> val(K);
  for (int i = 0; i < K; i++) {
    cin >> L[i] >> R[i];
  }

  vector<ll> dp(N + 1, 0);
  dp[1] = 1;
  for (int i = 2; i <= N; i++) {
    for (int k = 0; k < K; k++) {
      int l = L[k], r = R[k];
      if (i - l > 0)
        val[k] += dp[i - l] % MOD;
      if (i - r - 1 > 0)
        val[k] -= dp[i - r - 1] % MOD;
      dp[i] = (dp[i] + val[k]) % MOD;
    }
  }

  cout << dp[N] % MOD << endl;
}


Error: TLE

Fixed submission:

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

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define FORR(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)

#define DEBUG(x) cout << #x << ": " << (x) << endl
#define DEBUG_VEC(v)                                                           \\
  cerr << #v << ": ";                                                          \\
  REP(__i, (v).size()) cerr << ((v)[__i]) << ", ";                             \\
  cerr << endl
#define PRINT(x) cout << x << endl
#define all(x) x.begin(), x.end()
#define MM << " " <<
int main() {
  int N, K;
  cin >> N >> K;

  ll MOD = 998244353;

  set<int> s;
  for (int i = 0; i < K; i++) {
    int L, R;
    cin >> L >> R;
    for (int j = L; j <= R; j++)
      s.insert(j);
  }

  vector<ll> dp(N + 1, 0);
  dp[1] = 1;
  for (int i = 2; i <= N; i++) {
    for (const auto num : s) {
      if (i - num >= 1)
        dp[i] += dp[i - num] % MOD;
    }
  }

  cout << dp[N] % MOD << endl;
}