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

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

int64_t Q = 998244353;

int main() {
  int N, K;
  cin >> N >> K;
  vector<pair<int, int>> p(K);
  for (int i = 0; i < K; i++) {
    int a, b;
    cin >> a >> b;
    p[i] = make_pair(a, b);
  }
  sort(p.begin(), p.end());
  vector<int64_t> dp(N + 1);
  vector<int64_t> rui(N + 1);
  dp[1] = 1;
  rui[1] = 1;
  for (int i = 2; i <= N; i++) {
    int64_t A = 0;
    for (int j = 0; j < K; j++) {
      int a, b;
      tie(a, b) = p[j];
      if (i <= a) {
        break;
      }
      A += rui[i - a] - rui[max(i - b - 1, 0)];
      A += Q;
      A %= Q;
    }
    dp[i] = A;
    rui[i] = (dp[i] + rui[i - 1]) % Q;
  }
  cout << dp[N] << endl;
}

Error: -6
malloc(): corrupted top size


Fixed submission:

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

int64_t Q = 998244353;

int main() {
  int N, K;
  cin >> N >> K;
  vector<pair<int, int>> p(K);
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    p[i] = make_pair(a, b);
  }
  sort(p.begin(), p.end());
  vector<int64_t> dp(N + 1);
  vector<int64_t> rui(N + 1);
  dp[1] = 1;
  rui[1] = 1;
  for (int i = 2; i <= N; i++) {
    int64_t A = 0;
    for (int j = 0; j < K; j++) {
      int a, b;
      tie(a, b) = p[j];
      if (i <= a) {
        break;
      }
      A += rui[i - a] - rui[max(i - b - 1, 0)];
      A += Q;
      A %= Q;
    }
    dp[i] = A;
    rui[i] = (dp[i] + rui[i - 1]) % Q;
  }
  cout << dp[N] << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <set>
#include <vector>

using namespace std;

int main() {
  long long n, mod = 998244353;
  int k;
  cin >> n >> k;
  set<long long> s, next;
  vector<long long> df(n + 1, 0);
  vector<long long> df_sum(n + 1, 0);
  vector<long long> left(k);
  vector<long long> right(k);

  for (int i = 0; i < k; i++) {
    long long tmp1, tmp2;
    cin >> tmp1 >> tmp2;
    for (long long j = tmp1; j < tmp2 + 1; j++) {
      s.insert(j);
    }
    left[i] = tmp1;
    right[i] = tmp2;
  }

  df[1] = 1;
  df_sum[0] = 0;
  df_sum[1] = 1;
  // next.insert(0);

  for (long long i = 2; i <= n; i++) {
    /*if (df[i] == 0) {
        continue;
    }*/

    // next.erase(next.begin());

    /*for (auto itr = s.begin(); itr != s.end(); itr++) {
        if (i + *itr < n) {
            df[i + *itr] += df[i];
            df[i + *itr] %= mod;
            // next.insert(i + *itr);
        } else {
            break;
        }
    }*/

    for (long long j = 0; j < k; j++) {
      int lefti = i - right[j];
      int righti = i - left[j];
      if (righti < 0)
        continue;
      lefti = max(lefti, 1);
      df[i] += df_sum[righti] - df_sum[lefti - 1];
      df[i] %= mod;
    }

    df_sum[i] = df_sum[i - 1] + df[i];
    // cout << df_sum[i] << endl;
  }

  /*for (auto itr = s.begin(); itr != s.end(); itr++) {
      int now=0
  }*/

  cout << df[n] << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <set>
#include <vector>

using namespace std;

int main() {
  long long n, mod = 998244353;
  int k;
  cin >> n >> k;
  set<long long> s, next;
  vector<long long> df(n, 0);

  for (int i = 0; i < k; i++) {
    long long tmp1, tmp2;
    cin >> tmp1 >> tmp2;
    for (long long j = tmp1; j < tmp2 + 1; j++) {
      s.insert(j);
    }
  }

  df[0] = 1;
  next.insert(0);

  for (long long i = 0; i < n; i++) {
    if (df[i] == 0) {
      continue;
    }

    next.erase(next.begin());

    for (auto itr = s.begin(); itr != s.end(); itr++) {
      if (i + *itr < n) {
        df[i + *itr] += df[i];
        df[i + *itr] %= mod;
        next.insert(i + *itr);
      } else {
        break;
      }
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
using pi = pair<int, int>;
#define all(x) x.begin(), x.end()
#define rep(i, j, n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
const ll MOD = 1000000007;
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;
}
//(a+b-1)/b
// priority_queue<ll, vector<ll>, greater<ll>> q;
vector<pl> vec(0);

class SegmentTree {
public:
  vector<ll> seg;
  ll len;
  SegmentTree(ll n) {
    len = n;
    seg.resize(2 * n);
  }
  void add(ll ind, ll x) { // 1indexed
    ind += len;
    seg[ind] += x;
    while (true) {
      ind /= 2;
      if (ind == 0)
        break;
      seg[ind] = seg[ind * 2] + seg[ind * 2 + 1];
      seg[ind] %= 998244353;
    }
  }
  ll get_sum(ll l, ll r) { // 1indexed
    ll ans = 0;
    l += len;
    r += len;
    while (l < r) {
      if (l % 2 == 1) {
        ans += seg[l];
        l++;
      }
      l /= 2;
      if (r % 2 == 1) {
        ans += seg[r - 1];
        --r;
      }
      r /= 2;
    }
    return ans;
  }
};

signed main() {
  // cout << fixed << setprecision(10);
  ll n, k;
  cin >> n >> k;
  SegmentTree s(200200);
  s.add(1, 1);
  rep(i, 0, k) {
    ll a, b;
    cin >> a >> b;
    vec.push_back(pl(a, b));
  }
  rep(i, 1, n + 1) {
    ll sum = 0;
    rep(j, 0, k) {
      if (i - vec[j].first < 1)
        continue;
      ll l = max((ll)1, i - vec[j].second);
      ll r = i - vec[j].first + 1;
      sum += s.get_sum(l, r);
      sum %= 998244353;
    }
    s.add(i, sum);
  }
  cout << s.get_sum(n, n + 1) << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
using pi = pair<int, int>;
#define all(x) x.begin(), x.end()
#define rep(i, j, n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
const ll MOD = 1000000007;
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;
}
//(a+b-1)/b
// priority_queue<ll, vector<ll>, greater<ll>> q;

signed main() {
  // cout << fixed << setprecision(10);
  ll n;
  cin >> n;
  ll k;
  cin >> k;
  vl nx(0);
  rep(i, 0, k) {
    ll a, b;
    cin >> a >> b;
    rep(j, a, b + 1) nx.push_back(j);
  }
  vl dp(n + 1);
  dp[1] = 1;
  rep(i, 1, n) {
    for (auto c : nx) {
      if (i + c <= n) {
        dp[i + c] += dp[i];
        dp[i + c] %= 998244353;
      }
    }
  }
  cout << dp[n] << endl;

  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 namespace std;
using ll = long long;
using P = pair<int, int>;
using V = vector<int>;

int main(int argc, char const *argv[]) {
  int mod = 998244353;
  int n, k;
  cin >> n >> k;
  int l[15], r[15];
  for (int i = 0; i < k; i++) {
    cin >> l[i] >> r[i];
  }

  sort(l, l + k);
  sort(r, r + k);

  long long int dp[200010] = {};
  dp[1] = 1;
  if (l[0] == 1)
    dp[2] = 1;
  for (int i = 3; i <= n; i++) {
    dp[i] = dp[i - 1];
    for (int j = 0; j < k; j++) {
      if (i - l[j] - 1 >= 0) {
        dp[i] += dp[i - l[j]];
        if (i - r[j] - 1 >= 1)
          dp[i] -= dp[i - r[j] - 1];
        if (dp[i] < 0)
          dp[i] += mod;
        dp[i] %= mod;
      }
    }
    // cout << dp[i] << endl;
  }
  cout << dp[n] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using V = vector<int>;

int main(int argc, char const *argv[]) {
  int mod = 998244353;
  int n, k;
  cin >> n >> k;
  // int step[200010] = {};
  // cout << step[3] << endl;
  int l[15], r[15];
  for (int i = 0; i < k; i++) {
    cin >> l[i] >> r[i];
  }

  sort(l, l + k);
  sort(r, r + k);

  /*
  for (int i = 0; i < k; i++)
  {
      cin >> l >> r;
      for (int j = l; j <= r; j++)
      {
          step[j] = 1;
      }
  }
  */
  int dp[200010] = {};
  dp[1] = 1;
  for (int i = 2; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      if (l[j] >= i)
        break;
      for (int m = l[j]; m <= min(i, r[j]); m++) {
        dp[i] += dp[i - m];
        dp[i] %= mod;
      }
    }
  }
  cout << dp[n] << endl;

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

#include <bits/stdc++.h>

#define pb push_back
#define ll long long
#define F first
#define S second
#define ld long double
#define fa fflush(stdout);
#define mp make_pair
using namespace std;
void data() {
#ifdef NURS
  freopen("main.in", "r", stdin);
  freopen("main.out", "w", stdout);
#endif
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ;
}
int dx[5] = {-1, 0, 0, 1};
int dy[5] = {0, -1, 1, 0};

const int N = 1e6 + 100;
const ll mod = 1e6 + 3;
const ll big = 1e9;
const ll p = 27;

ll dp[N];
ll ok[N];
int main() {
  data();
  // freopen("substr3.in","r",stdin);
  // freopen("substr3.out","w",stdout);
  int n, k;
  cin >> n >> k;
  dp[1] = 1;
  int l[k + 1], r[k + 1];
  for (int i = 1; i <= k; i++) {
    cin >> l[i] >> r[i];
  }
  for (int i = 1; i <= n; i++) {
    ok[i] += ok[i - 1];
    dp[i] += ok[i];
    dp[i] %= 998244353;
    if (dp[i] > 0) {
      for (int j = 1; j <= k; j++) {
        if (i + l[j] <= n) {
          ok[i + l[j]] += dp[i];
          ok[i + l[j]] %= 998244353;
          if (i + r[j] + 1 <= n) {
            ok[min(n + 1, i + r[j] + 1)] -= dp[i];
          }
        }
      }
    }
  }
  cout << dp[n];
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define pb push_back
#define ll long long
#define F first
#define S second
#define ld long double
#define fa fflush(stdout);
#define mp make_pair
using namespace std;
void data() {
#ifdef NURS
  freopen("main.in", "r", stdin);
  freopen("main.out", "w", stdout);
#endif
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ;
}
int dx[5] = {-1, 0, 0, 1};
int dy[5] = {0, -1, 1, 0};

const int N = 1e6 + 100;
const ll mod = 1e6 + 3;
const ll big = 1e9;
const ll p = 27;

ll dp[N];
int ok[N];
int main() {
  data();
  // freopen("substr3.in","r",stdin);
  // freopen("substr3.out","w",stdout);
  int n, k;
  cin >> n >> k;
  dp[1] = 1;
  for (int i = 1; i <= k; i++) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; j++) {
      ok[j] = 1;
    }
  }
  for (int i = 1; i <= n; i++) {
    if (dp[i] > 0) {
      for (int j = 1; j + i <= n; j++) {
        if (ok[j]) {
          dp[i + j] += dp[i];
          dp[i + j] %= 998244353LL;
        }
      }
    }
  }
  cout << dp[n];
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

const int DIV = 998244353;

int max(int a, int b) {
  if (a >= b) {
    return (a);
  } else {
    return (b);
  }
}

int main(void) {
  int n;
  int k;
  int lr[10][2] = {0};
  int dp[200001] = {0};
  int sdp[200001] = {0};
  int i, j;

  scanf("%d%d", &n, &k);
  for (i = 0; i < k; i++) {
    scanf("%d%d", &lr[i][0], &lr[i][1]);
  }
  dp[1] = 1;
  sdp[2] = 1;
  for (i = 2; i <= n; i++) {
    for (j = 0; j < k; j++) {
      dp[i] +=
          (DIV + sdp[max(1, i - lr[j][0] + 1)] - sdp[max(1, i - lr[j][1])]) %
          DIV;
      dp[i] = dp[i] % DIV;
    }
    sdp[i + 1] += (sdp[i] + dp[i]) % DIV;
  }
  printf("%d\\n", dp[n]);
  return (0);
}

Error: TLE

Fixed submission:

#include <stdio.h>

const int DIV = 998244353;

int main(void) {
  int n;
  int k;
  int temp;
  int lr[10][2] = {0};
  int dp[200001] = {0};
  int i, j, l;

  scanf("%d%d", &n, &k);
  for (i = 0; i < k; i++) {
    scanf("%d%d", &lr[i][0], &lr[i][1]);
  }
  for (i = 0; i < k; i++) {
    for (j = k - 1; j > i; j--) {
      if (lr[j - 1][0] > lr[j][0]) {
        temp = lr[j][0];
        lr[j][0] = lr[j - 1][0];
        lr[j - 1][0] = temp;
        temp = lr[j][1];
        lr[j][1] = lr[j - 1][1];
        lr[j - 1][1] = temp;
      }
    }
  }
  dp[1] = 1;
  for (i = 1; i <= n; i++) {
    l = 0;
    j = lr[l][0];
    while (i + j <= n) {
      dp[i + j] = (dp[i + j] + dp[i]) % DIV;
      j++;
      if (j > lr[l][1]) {
        l++;
        if (l == k) {
          break;
        }
        j = lr[l][0];
      }
    }
  }
  printf("%d\\n", dp[n]);
  return (0);
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cfenv>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define vint vector<ll>
#define vvint vector<vint>
#define P pair<ll, ll>
#define INT_MAX 2147483647
#define MOD 998244353
#define PI 3.14159265358979323846
#define all(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;

#define MAX 1000000000000

int main(void) {
  ll n, k;
  cin >> n >> k;
  vector<P> s(k);

  rep(i, k) { cin >> s[i].first >> s[i].second; }

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

  for (int i = 2; i <= n; i++) {
    dp[i] += dp[i - 1];
    rep(j, k) {
      if (i - s[j].first >= 1) {
        if (i - s[j].second < 1) {
          dp[i] = (dp[i] + dp[i - s[j].first]) % MOD;
        } else {
          dp[i] = (dp[i] + dp[i - s[j].first] - dp[i - s[j].second - 1]) % MOD;
          if (dp[i] < 0) {
            dp[i] += MOD;
          }
        }
      }
    }
  }

  ll ans = dp[n] - dp[n - 1];
  if (ans < 0) {
    ans += MOD;
  }

  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cfenv>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define vint vector<ll>
#define vvint vector<vint>
#define P pair<ll, ll>
#define INT_MAX 2147483647
#define MOD 998244353
#define PI 3.14159265358979323846
#define all(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;

#define MAX 1000000000000

int main(void) {
  ll n, k;
  cin >> n >> k;
  vint s;
  rep(i, k) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; j++) {
      s.push_back(j);
    }
  }

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

  for (int i = 2; i <= n; i++) {
    rep(j, s.size()) {
      if (i - s[j] >= 1) {
        dp[i] = (dp[i] + dp[i - s[j]]) % 998244353;
      }
    }
  }
  cout << dp[n] << endl;
  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 all(x) (x).begin(), (x).end()
#define dump(x) cout << #x << " = " << (x) << endl

using namespace std;
using ll = long long;
ll MOD = 998244353;

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

  vector<int> l(k, 0), r(k, 0);
  rep(i, k) cin >> l[i] >> r[i];
  vector<ll> dp(n, 0), sdp(n + 1, 0);

  dp[0] = 1;
  sdp[1] = 1;

  for (int i = 1; i < n; ++i) {
    rep(j, k) {
      int left = max(0, i - r[j]);
      int right = max(0, i - l[j] + 1);
      ll val = sdp[right] - sdp[left];
      if (val < 0)
        val += MOD;
      dp[i] += val % MOD;
      dp[i] %= MOD;
    }
    sdp[i + 1] = (sdp[i] + dp[i]) % MOD;
  }

  cout << (dp[n - 1] % MOD) << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define all(x) (x).begin(), (x).end()
#define dump(x) cout << #x << " = " << (x) << endl

using namespace std;
using ll = long long;
ll MOD = 998244353;

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

  set<int> s;

  rep(j, k) {
    int l, r;
    cin >> l >> r;
    for (int i = l; i <= r; ++i) {
      s.insert(i);
    }
  }

  vector<int> v(s.begin(), s.end());

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

  dp[0] = 1;
  for (int i = 1; i < n; ++i) {
    for (int u : v) {
      if (i - u >= 0) {
        dp[i] += dp[i - u];
        dp[i] %= MOD;
      }
    }
  }
  cout << (dp[n - 1] % MOD) << endl;
}
Problem p02549 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int32_t main() {

  ios_base::sync_with_stdio(false);

  cin.tie(0);

  long long n;

  cin >> n;

  long long k;

  cin >> k;

  long long diff[4 * 200005];

  memset(diff, 0, sizeof diff);

  vector<pair<long long, long long>> v;

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

    long long x;

    cin >> x;

    long long y;

    cin >> y;

    v.push_back({x, y});
  }

  diff[0] = 0;

  diff[1] = 1;

  long long last = 0;

  for (long long i = 1; i <= n + 5; i++) {

    if (i > 2) {

      diff[i] += diff[i - 1];
    }

    long long cur = diff[i] % 998244353;

    for (long long j = 0; j < v.size(); j++) {

      diff[i + v[j].first] += cur;

      diff[i + v[j].second + 1] -= cur;
    }
  }

  cout << diff[n] % 998244353 << '\\n';

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int32_t main() {

  ios_base::sync_with_stdio(false);

  cin.tie(0);

  long long n;

  cin >> n;

  long long k;

  cin >> k;

  long long diff[200005];

  memset(diff, 0, sizeof diff);

  vector<pair<long long, long long>> v;

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

    long long x;

    cin >> x;

    long long y;

    cin >> y;

    v.push_back({x, y});
  }

  diff[0] = 0;

  diff[1] = 1;

  long long last = 0;

  for (long long i = 1; i <= n + 5; i++) {

    if (i > 2) {

      diff[i] += diff[i - 1];
    }

    long long cur = diff[i] % 998244353;

    for (long long j = 0; j < v.size(); j++) {

      diff[i + v[j].first] += cur;

      diff[i + v[j].second + 1] -= cur;
    }
  }

  cout << diff[n] % 998244353 << '\\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 < (int)(n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, s, e) for (int i = s; i <= (int)(e); ++i)
using namespace std;
#define int long long int
#define MOD 998244353
//------------ATCODER TEMPLATE-------------

signed main() {
  int N, K;
  cin >> N >> K;
  vector<pair<int, int>> ps(K);
  REP(i, K) { cin >> ps[i].first >> ps[i].second; }
  sort(ps.begin(), ps.end());

  vector<int> cnt(N + 1, 0);
  vector<int> S(N + 1, 0);
  S[0] = 0, S[1] = 1;
  ;
  cnt[1] = 1;
  FOR(i, 2, N) {
    for (auto [L, R] : ps) {
      int l = max(i - R - 1, 0LL);
      int r = i - L;
      if (r < 0)
        break;
      cnt[i] = (cnt[i] + S[r] - S[l] + MOD) % MOD;
    }
    S[i] = (S[i - 1] + cnt[i]) % MOD;
  }
  cout << cnt[N] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, s, e) for (int i = s; i <= (int)(e); ++i)
using namespace std;
#define int long long int
#define MOD 998244353
//------------ATCODER TEMPLATE-------------

signed main() {
  int N, K;
  cin >> N >> K;
  vector<int> v;
  REP(i, K) {
    int L, R;
    cin >> L >> R;
    FOR(j, L, R) { v.push_back(j); }
  }
  sort(v.begin(), v.end());

  vector<int> cnt(N + 1, 0);
  cnt[0] = 1;
  FOR(i, 1, N) {
    for (int x : v) {
      if (i - x < 0)
        break;
      cnt[i] += cnt[i - x];
      cnt[i] %= MOD;
    }
  }
  cout << cnt[N - 1] << endl;
  return 0;
}
Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const long long mod = 998244353;

int main() {
  long long n;
  int k;
  cin >> n >> k;
  vector<pair<int, int>> range;
  for (int i = 0; i < k; i++) {
    int l, r;
    cin >> l >> r;
    range.push_back({l, r + 1});
  }

  vector<long long> dp(n);
  dp[0] = 1;
  for (int i = 0; i < n; i++) {
    if (i > 1) {
      dp[i] += dp[i - 1];
      dp[i] %= mod;
    }
    for (int j = 0; j < k; j++) {
      int l, r;
      tie(l, r) = range[j];
      if (i + l < n) {
        dp[i + l] += dp[i];
        dp[i + l] %= mod;
      }
      if (i + r < n) {
        dp[i + r] -= dp[i];
        dp[i + r] += mod;
        dp[i + r] %= mod;
      }
    }
  }

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


Error: TLE

Fixed submission:

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

const long long mod = 998244353;
vector<long long> sm;
vector<long long> dp(200005, -1);

long long dfs(long long rem) {
  if (rem < 0) {
    return 0;
  }
  if (rem == 0) {
    return 1;
  }
  if (dp[rem] != -1) {
    return dp[rem];
  }
  long long ret = 0;
  for (int i = 0; i < sm.size(); i++) {
    ret += dfs(rem - sm[i]);
    ret %= mod;
  }
  dp[rem] = ret;
  return ret;
}

int main() {
  long long n;
  int k;
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    long long l, r;
    cin >> l >> r;
    while (l <= r) {
      sm.push_back(l++);
    }
  }

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

#include <bits/stdc++.h>
#define ll long long int
#define ar array
#define pb push_back
#define input(arr, n)                                                          \\
  for (int i = 0; i < n; i++)                                                  \\
  cin >> arr[i]
const int mod = 998244353;
ll power(ll a, ll b) {
  ll r = 1;
  for (; b; b >>= 1) {
    if (b & 1)
      r = r * a % mod;
    a = a * a % mod;
  }
  return r;
}
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  ll n, k;
  cin >> n >> k;
  vector<ar<ll, 2>> v;
  ll dp[n + 2] = {0};
  dp[1] = 1;
  for (int i = 0, j, k1; i < k; i++) {
    cin >> j >> k1;
    v.pb({j, k1});
  }
  for (int i = 1; i <= n; i++) {
    if (i > 2)
      dp[i] = (dp[i] + dp[i - 1]) % mod;
    for (int j = 0; j < k; j++) {
      if (i + v[j][0] <= n) {
        dp[i + v[j][0]] = (dp[i + v[j][0]] + dp[i]) % mod;
        dp[min(i + v[j][1] + 1, n + 1)] =
            ((dp[min(i + v[j][1] + 1, n + 1)] - dp[i]) % mod + mod) % mod;
      }
    }
  }
  cout << dp[n] << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define ar array
#define pb push_back
#define input(arr, n)                                                          \\
  for (int i = 0; i < n; i++)                                                  \\
  cin >> arr[i]
const int mod = 998244353;
ll power(ll a, ll b) {
  ll r = 1;
  for (; b; b >>= 1) {
    if (b & 1)
      r = r * a % mod;
    a = a * a % mod;
  }
  return r;
}
using namespace std;

map<int, int> m;
int count(int i, int j, int n, vector<int> &v) {
  if (i == n)
    return 1;
  if (i > n || j == int(v.size()))
    return 0;
  if (m.count(i))
    return m[i];
  for (int i1 = 0; i1 < v.size(); i1++)
    m[i] = (m[i] + count(i + v[i1], i1, n, v)) % mod;
  return m[i];
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  vector<int> v;
  for (int i = 0, j, k1; i < k; i++) {
    cin >> j >> k1;
    for (int j1 = j; j1 <= k1; j1++)
      v.pb(j1);
  }
  cout << count(1, 0, n, v) << '\\n';
  // for(auto it:m)
  // 	cout<<it.first.first<<' '<<it.first.second<<' '<<it.second<<'\\n';
  return 0;
}