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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vll = vector<vl>;
using Pll = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(x) ((int)x.size())
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
const int MOD = 1e9 + 7;
const ll INF = 2e15;
template <class T> void print(const T &t) { cout << t << endl; }
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

int main() {
  ll n, k;
  cin >> n >> k;
  ll mod = 998244353;
  vector<Pll> s(0);
  rep(i, k) {
    ll l, r;
    cin >> l >> r;
    s.pb(mp(l, r));
  }
  vl dp(n + 1, 0);
  dp[1] = 1;
  dp[2] = -1;
  for (ll i = 1; i <= n; i++) {
    dp[i] += dp[i - 1];
    dp[i] %= mod;
    for (ll j = 0; j < k; j++) {
      if (i + s[j].F <= n) {
        dp[i + s[j].F] += dp[i];
      }
      if (i + s[j].S + 1 <= n) {
        dp[i + s[j].S + 1] -= dp[i];
      }
    }
  }
  dp[n] %= mod;
  dp[n] = (dp[n] + mod) % mod;
  print(dp[n]);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vll = vector<vl>;
using Pll = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(x) ((int)x.size())
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
const int MOD = 1e9 + 7;
const ll INF = 2e15;
template <class T> void print(const T &t) { cout << t << endl; }
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

int main() {
  ll n, k;
  cin >> n >> k;
  ll mod = 998244353;
  vl s(0);
  rep(i, k) {
    ll l, r;
    cin >> l >> r;
    for (ll i = l; i <= r; i++) {
      s.pb(i);
    }
  }
  sort(all(s));
  vl dp(n + 1, 0);
  dp[1] = 1;
  for (ll i = 1; i <= n; i++) {
    for (ll j = 0; j < sz(s); j++) {
      if (i - s[j] < 0) {
        break;
      }
      dp[i] += dp[i - s[j]];
      dp[i] %= mod;
    }
  }
  print(dp[n]);
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using ll = long long;
const ll mod = 998244353;
using namespace std;

#define int ll
struct node {
  ll val = 0;
  ll lz = 0;
} st[8000001];

void push(int p, int l = 0, int r = 0) {
  st[p].val %= mod;
  st[p].val += st[p].lz * (r - l + 1);
  st[p].val %= mod;
  st[p * 2].lz += st[p].lz;
  st[p * 2].lz %= mod;
  st[p * 2 + 1].lz += st[p].lz;
  st[p * 2 + 1].lz %= mod;
  st[p].lz = 0;
}
void update(int p, int l, int r, int i, int j, ll val) {
  push(p);
  if (r < i || j < l)
    return;
  if (i <= l && r <= j) {
    st[p].lz = val;
    push(p, l, r);
  } else {
    int m = (l + r) / 2;
    update(p * 2, l, m, i, j, val);
    update(p * 2 + 1, m + 1, r, i, j, val);
  }
}

int qry(int p, int l, int r, int i) {
  push(p);
  if (l == r) {
    return st[p].val;
  } else {
    int m = (l + r) / 2;
    if (i <= m) {
      return qry(p * 2, l, m, i);
    } else {
      return qry(p * 2 + 1, m + 1, r, i);
    }
  }
}

signed main() {
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> segs;
  while (k--) {
    int l, r;
    cin >> l >> r;
    segs.emplace_back(l, r);
  }
  update(1, 0, n - 1, 0, 0, 1);
  for (int i = 0; i < n - 1; i++) {
    for (auto p : segs) {
      int l, r;
      l = p.first;
      r = p.second;
      if (i + l < n) {
        update(1, 0, n - 1, i + l, min(n - 1, i + r),
               qry(1, 0, n - 1, i) % mod);
      }
    }
  }
  cout << qry(1, 0, n - 1, n - 1);
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using ll = long long;
const ll mod = 998244353;
using namespace std;

#define int ll
struct node {
  ll val = 0;
  ll lz = 0;
} st[1 << 19];

void push(int p, int l = 0, int r = 0) {
  st[p].val %= mod;
  st[p].val += st[p].lz * (r - l + 1);
  st[p].val %= mod;
  st[p * 2].lz += st[p].lz;
  st[p * 2].lz %= mod;
  st[p * 2 + 1].lz += st[p].lz;
  st[p * 2 + 1].lz %= mod;
  st[p].lz = 0;
}
void update(int p, int l, int r, int i, int j, ll val) {
  push(p);
  if (r < i || j < l)
    return;
  if (i <= l && r <= j) {
    st[p].lz = val;
    push(p, l, r);
  } else {
    int m = (l + r) / 2;
    update(p * 2, l, m, i, j, val);
    update(p * 2 + 1, m + 1, r, i, j, val);
  }
}

int qry(int p, int l, int r, int i) {
  push(p);
  if (l == r) {
    return st[p].val;
  } else {
    int m = (l + r) / 2;
    if (i <= m) {
      return qry(p * 2, l, m, i);
    } else {
      return qry(p * 2 + 1, m + 1, r, i);
    }
  }
}

signed main() {
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> segs;
  while (k--) {
    int l, r;
    cin >> l >> r;
    segs.emplace_back(l, r);
  }
  update(1, 0, n - 1, 0, 0, 1);
  for (int i = 0; i < n - 1; i++) {
    for (auto p : segs) {
      int l, r;
      l = p.first;
      r = p.second;
      if (i + l < n) {
        update(1, 0, n - 1, i + l, min(n - 1, i + r),
               qry(1, 0, n - 1, i) % mod);
      }
    }
  }
  cout << qry(1, 0, n - 1, n - 1);
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <math.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <vector>

using namespace std;
using Graph = vector<vector<int>>;

using ll = long long;
typedef pair<ll, ll> P_ll;
typedef pair<int, int> P;

const ll INF_ll = 1e17;
const int INF = 1e8;

int main() {
  ll MOD = 998244353;

  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, 0);
  vector<ll> dpsum(N + 1, 0);
  dp[1] = 1;
  dpsum[1] = 1;
  for (int i = 2; i <= N; i++) {
    for (int j = 0; j < K; j++) {
      int l_idx = i - r[j];
      int r_idx = i - l[j];
      if (r_idx < 0) {
        continue;
      }
      l_idx = max(l_idx, 1);
      dp[i] += dpsum[r_idx] - dpsum[l_idx - 1];
      if (dp[i] < 0) {
        dp[i] = (dp[i] + MOD) % MOD;
      }

      dp[i] %= MOD;
    }
    dpsum[i] = dpsum[i - 1] + dp[i];
    dpsum[i] %= MOD;
  }

  cout << dp[N] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <math.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <vector>

using namespace std;
using Graph = vector<vector<int>>;

using ll = long long;
typedef pair<ll, ll> P_ll;
typedef pair<int, int> P;

const ll INF_ll = 1e17;
const int INF = 1e8;

int main() {
  ll MOD = 998244353;

  int N, K;
  cin >> N >> K;
  vector<vector<int>> lr(K, vector<int>(2));
  vector<int> d(0);
  for (int i = 0; i < K; i++) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; j++) {
      d.push_back(j);
    }
  }
  sort(d.begin(), d.end());
  vector<ll> dp(N + 1, 0);
  dp[1] = 1;
  for (int i = 1; i < N + 1; i++) {
    for (int j = 0; j < d.size(); j++) {
      if (i < d[j]) {
        continue;
      }
      dp[i] += dp[i - d[j]];
      dp[i] %= MOD;
    }
    // for (int i = 1; i < N + 1; i++) {
    //   cout << dp[i] << " ";
    // }
    // cout << endl;
  }

  // for (int i = 0; i < N + 1; i++) {
  //   cout << dp[i] << endl;
  // }

  cout << dp[N] << endl;

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

#include <bits/stdc++.h>
#define pb push_back
typedef long long ll;
using namespace std;
const int mx = 5e5;
const ll mod = 998244353;
ll res[mx + 5];
int main() {
  res[1] = 1;
  int n, k;
  cin >> n >> k;
  vector<pair<int, int>> vec(k);
  for (int i = 0; i < k; ++i) {
    cin >> vec[i].first >> vec[i].second;
  }
  for (int i = 2; i <= n; ++i) {
    for (int j = 0; j < k; ++j) {
      if (i - vec[j].first < 1)
        continue;

      if (i - vec[j].second < 1)
        res[i] += res[i - vec[j].first];

      else
        res[i] += res[i - vec[j].first] - res[i - vec[j].second - 1];

      res[i] %= mod;
    }
    if (i != n)
      res[i] += res[i - 1];
  }
  cout << res[n] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cmath>
#define pb push_back
#define ld long double
#define mp make_pair
#define vl vector<ll>
#define vd vector<double>
#define vld vector<long double>
#define ll long long int
#define pl pair<ll, ll>
#define all(a) a.begin(), a.end()
#define forr(i, n) for (ll i = 0; i < n; i++)
#define forr1(i, n) for (ll i = 1; i <= n; i++)
using namespace std;
const ld PI = 3.1415926535897923846;
const ll MOD = 1000000007;
const ll N = 998244353;
ll power(ll x, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * x % MOD;
    x = x * x % MOD;
    n >>= 1;
  }
  return res;
}
ll modinverse(ll a) { return power(a, MOD - 2); }
// so, this has to be some sort of dp
// dp[i] has to be the number of ways to get to i
// fuck you, ktksan lmao
void solve() {
  ll n, k;
  cin >> n >> k;
  vl seg;
  forr(i, k) {
    ll l, r;
    cin >> l >> r;
    for (ll ii = l; ii <= r; ii++)
      seg.pb(ii);
  }
  sort(all(seg));
  vl dp(n + 1, 0);
  dp[1] = 1;
  for (ll i = 1; i <= n; i++) {
    if (dp[i] == 0)
      continue;
    forr(j, (ll)seg.size()) {
      if (seg[j] + i > n)
        break;
      dp[i + seg[j]] += dp[i];
      dp[i + seg[j]] %= N;
    }
  }
  cout << dp[n] << endl;
  return;
}
int main() {
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll test = 1;
  // cin>>test;
  while (test--) {
    solve();
  }
  cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC
       << "ms\\n";
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <cassert>
#define rep(i, N) for (int i = 0; i < (N); ++i)
#define rep2(i, a, b) for (ll i = a; i <= b; ++i)
#define rep3(i, a, b) for (ll i = a; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(c) begin(c), end(c)
#define ok() puts(ok ? "Yes" : "No");
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using ii = pair<int, int>;
using vvi = vector<vi>;
using vii = vector<ii>;
using gt = greater<int>;
using minq = priority_queue<int, vector<int>, gt>;
using P = pair<ll, ll>;
const ll LINF = 1e18L + 1;
const int INF = 1e9 + 1;
// clang++ -std=c++11 -stdlib=libc++

ll dp[200005];
ll dpsum[200005];
int mod = 998244353;
int main() {
  int N, K;
  cin >> N >> K;
  vi L(N), R(N);
  rep(i, K) cin >> L[i] >> R[i];
  dp[1] = 1;
  dpsum[1] = dp[1];
  rep2(i, 2, N) {
    rep(j, K) {
      int l = i - R[j];
      int r = i - L[j];
      if (r < 0)
        continue;
      chmax(l, 1);
      (dp[i] += dpsum[r] - dpsum[l - 1]) %= mod;
      if (dp[i] < 0)
        dp[i] += mod;
    }
    dpsum[i] = (dpsum[i - 1] + dp[i]) % mod;
    if (dpsum[i] < 0)
      dpsum[i] += mod;
  }
  cout << dp[N] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <cassert>
#define rep(i, N) for (int i = 0; i < (N); ++i)
#define rep2(i, a, b) for (ll i = a; i <= b; ++i)
#define rep3(i, a, b) for (ll i = a; i >= b; --i)
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(c) begin(c), end(c)
#define ok() puts(ok ? "Yes" : "No");
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
using namespace std;
using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;
using ii = pair<int, int>;
using vvi = vector<vi>;
using vii = vector<ii>;
using gt = greater<int>;
using minq = priority_queue<int, vector<int>, gt>;
using P = pair<ll, ll>;
const ll LINF = 1e18L + 1;
const int INF = 1e9 + 1;
// clang++ -std=c++11 -stdlib=libc++

ll dp[200005];
int used[200006];
int main() {
  int N, K;
  cin >> N >> K;
  int mod = 998244353;
  vi s;
  rep(i, K) {
    int a, b;
    cin >> a >> b;
    rep2(j, a, b) {
      if (!used[j])
        s.pb(j);
      used[j] = true;
    }
  }
  sort(all(s));
  dp[1] = 1;
  rep2(i, 1, N) {
    bool changed = false;
    for (int num : s) {
      int ns = i + num;
      if (ns <= N) {
        (dp[ns] += dp[i]) %= mod;
        changed = true;
      } else
        break;
    }
    if (!changed)
      break;
  }
  cout << dp[N] << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (ll i = m; i < (n); i++)

using ll = long long;
using pii = pair<int, int>;
using Vi = vector<int>;

int main() {
  ll N, K;
  cin >> N >> K;
  vector<pair<ll, ll>> V;
  for (ll i = 0; i < K; i++) {
    ll L, R;
    cin >> L >> R;
    V.push_back(make_pair(L, R));
  }

  sort(V.begin(), V.end());
  vector<long long> dp(200010, 0);
  dp[1] = 1;
  dp[1 + V[0].first] = 1;

  for (ll i = V[0].first + 2; i <= 200010; i++) {
    long long dif = 0;
    for (auto v : V) {
      dif += (dp[max(0LL, i - v.first)] - dp[max(0LL, i - v.second - 1)]);
      dif += 998244353;
      dif %= 998244353;
    }
    dp[i] = dp[i - 1] + dif;
    dp[i] %= 998244353;
  }

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


Error: TLE

Fixed submission:

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

#define rep(i, n) rep2(i, 0, n)
#define rep2(i, m, n) for (ll i = m; i < (n); i++)

using ll = long long;
using pii = pair<int, int>;
using Vi = vector<int>;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> V;
  rep(i, K) {
    int L, R;
    cin >> L >> R;
    for (int i = L; i <= R; i++) {
      V.push_back(i);
    }
  }

  vector<long long> dp(200010, 0);
  dp[1] = 1;

  for (int i = 1; i <= N; i++) {
    for (auto v : V) {
      if (i + v > N)
        continue;
      dp[i + v] += dp[i];
      dp[i + v] %= 998244353;
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL mod = 998244353;
const int N = 200005;

int a[N], b[N];
LL f[N], c[20];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
  }
  f[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < m; j++) {
      c[j] += (i - a[j] >= 1 ? f[i - a[j]] : 0) -
              (i - b[j] - 1 >= 1 ? f[i - b[j] - 1] : 0);

      f[i] = (f[i] + c[j]) % mod;
    }
  }
  cout << (f[n] + mod) % mod << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
const LL mod = 998244353;
const int N = 200005;

int a[N], b[N];
LL f[N], c[20];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i];
  }
  f[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < m; j++) {
      c[j] += (i - a[j] >= 1 ? f[i - a[j]] : 0) -
              (f[i - b[j] - 1] >= 1 ? f[i - b[j] - 1] : 0);

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

#include <bits/stdc++.h>

#ifndef M_PI
#define M_PI 3.14159265358979
#endif
#define deg_to_rad(deg) (((deg) / 360) * 2 * M_PI)
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
typedef long double ld;
typedef pair<long double, long double> pld;

const ll INF = 1e18;
// const ll MOD = 1e9 + 7;
const ll MOD = 998244353;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll i, j, k;

  ll N, K;
  cin >> N >> K;
  vector<pll> P;
  for (i = 0; i < K; i++) {
    ll l, r;
    cin >> l >> r;
    P.push_back(pll(l, r));
  }
  vll C(N, 0);
  C[0] = 1;
  C[1] = -1;
  ll c = 0;
  for (i = 0; i < N; i++) {
    c = (c + C[i]) % MOD;
    for (j = 0; j < K; j++) {
      ll l = P[j].first;
      ll r = P[j].second;
      if (i + l < N) {
        C[i + l] = (C[i + l] + c) % MOD;
      }
      if (i + r + 1 < N) {
        C[i + r + 1] = (C[i + r + 1] + MOD - c) % MOD;
      }
    }
  }
  cout << c << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#ifndef M_PI
#define M_PI 3.14159265358979
#endif
#define deg_to_rad(deg) (((deg) / 360) * 2 * M_PI)
#define rad_to_deg(rad) (((rad) / 2 / M_PI) * 360)

using namespace std;

typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef pair<ll, ll> pll;
typedef pair<int, int> pi;
typedef long double ld;
typedef pair<long double, long double> pld;

const ll INF = 1e18;
// const ll MOD = 1e9 + 7;
const ll MOD = 998244353;

const ll MAX_N = 200200;

ll dp[MAX_N];
ll N, K;
vll S;

ll sub(ll n) {
  if (dp[n] != -1) {
    return dp[n];
  }
  ll ans = 0;
  for (auto s : S) {
    if (n - s < 0)
      break;
    ans = (ans + sub(n - s)) % MOD;
  }
  dp[n] = ans;
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll i, j, k;

  cin >> N >> K;
  vector<pll> P;
  for (i = 0; i < K; i++) {
    ll l, r;
    cin >> l >> r;
    P.push_back(pll(l, r));
  }
  sort(P.begin(), P.end());
  for (i = 0; i < K; i++) {
    ll l = P[i].first;
    ll r = P[i].second;
    for (j = l; j < r + 1; j++) {
      S.push_back(j);
    }
  }

  for (i = 0; i < N; i++) {
    dp[i] = -1;
  }
  dp[0] = 1;
  ll ans = sub(N - 1);
  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;

const ll MOD = 998244353;

int main() {

  ll n, k;
  cin >> n >> k;
  vector<pair<int, int>> imos(k);
  rep(i, k) {
    int L, R;
    cin >> L >> R;
    imos[i] = make_pair(L, R + 1);
  }
  vector<ll> dp(500000, 0);
  dp[1] = 1;
  dp[2] = -1;
  rep(i, 300000) {
    if (i > 0)
      dp[i] += dp[i - 1];
    dp[i] %= MOD;
    rep(j, k) {
      int L = imos[j].first;
      int R = imos[j].second;
      dp[i + L] += dp[i];
      dp[i + L] %= MOD;
      dp[i + R] -= dp[i];
      dp[i + R] = (dp[i + R] + MOD) % 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 < n; i++)
#define rep1(i, n) for (int i = 1; i < n + 1; i++)
#define all(A) A.begin(), A.end()
typedef long long ll;

const ll MOD = 998244353;

int main() {

  ll n, k;
  cin >> n >> k;
  vector<pair<int, int>> imos(k);
  rep(i, k) {
    int L, R;
    cin >> L >> R;
    imos[i] = make_pair(L, R + 1);
  }
  vector<ll> dp(500000, 0);
  dp[1] = 1;
  dp[2] = -1;
  rep(i, 500000) {
    if (i > 0)
      dp[i] += dp[i - 1];
    dp[i] %= MOD;
    rep(j, k) {
      int L = imos[j].first;
      int R = imos[j].second;
      dp[i + L] += dp[i];
      dp[i + L] %= MOD;
      dp[i + R] -= dp[i];
      dp[i + R] = (dp[i + R] + MOD) % MOD;
    }
  }
  cout << dp[n] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> a;
  vector<int> l(k);
  vector<int> r(k);
  for (int i = 0; i < k; i++) {
    cin >> l[i] >> r[i];
  }
  vector<long long> ans(n, 0);
  ans[0] = 1;
  bool f = false;
  for (int i = 0; i < k; i++) {
    if (l[i] == 1)
      f = true;
  }
  if (f)
    ans[1] = 1;
  else
    ans[1] = 0;
  for (int i = 2; i < n; i++) {
    ans[i] = ans[i - 1];
    for (int j = 0; j < k; j++) {
      if (l[j] <= i)
        ans[i] = ans[i] + ans[i - l[j]];
      if (r[j] + 1 <= i)
        ans[i] = ans[i] + (998244353 - ans[i - r[j] - 1]);
      ans[i] = ans[i] % 998244353;
    }
  }
  cout << ans[n - 1] << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  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.push_back(j);
    }
  }
  vector<int> ans(n, 0);
  sort(a.begin(), a.end());
  ans[0] = 1;
  for (int i = 1; i < n; i++) {
    for (int x : a) {
      if (x <= i)
        ans[i] = (ans[i] + ans[i - x]) % 998244353;
      else
        break;
    }
  }
  cout << ans[n - 1] << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define name "test"
#define fi first
#define se second
#define ll long long
#define db double
#define pb push_back
#define wrln cout << '\\n';
#define ii pair<int, int>
#define li pair<long long, int>
#define iii pair<int, ii>

using namespace std;
const int N = 2e5 + 100;
const int H = 998244353;

ll n, res, dem, sum, k;
ll f[N], d[N];

vector<ii> v;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  if (fopen(name ".inp", "r")) {
    freopen(name ".inp", "r", stdin);
    freopen(name ".out", "w", stdout);
  }
  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    int l, r;
    cin >> l >> r;
    v.pb({l, r});
  }

  f[1] = 1;
  d[1] = 1;
  f[0] = 0;
  d[0] = 0;

  for (int i = 2; i <= n; i++) {
    for (auto j : v) {
      int x = max(0, i - j.fi);
      int y = max(0, i - j.se - 1);

      f[i] = (f[i] + d[x] - d[y] + H) % H;
    }
    d[i] = (d[i - 1] + f[i]) % H;
  }
  cout << f[n];
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define name "test"
#define fi first
#define se second
#define ll long long
#define db double
#define pb push_back
#define wrln cout << '\\n';
#define ii pair<int, int>
#define li pair<long long, int>
#define iii pair<int, ii>

using namespace std;
const int N = 2e5 + 100;
const int H = 998244353;

ll n, res, dem, d, sum, k;
ll f[N];

vector<int> v;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  if (fopen(name ".inp", "r")) {
    freopen(name ".inp", "r", stdin);
    freopen(name ".out", "w", stdout);
  }
  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; j++) {
      v.pb(j);
    }
  }

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

  f[1] = 1;

  for (int i = 2; i <= n; i++) {
    for (auto j : v) {
      if (j > i)
        break;
      f[i] = (f[i] + f[i - j]) % H;
    }
  }
  cout << f[n];
}