Problem p02549 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define MOD (998244353)
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define nl '\\n'
#define all(x) x.begin(), x.end()
#define print(vec, l, r)                                                       \\
  for (int i = l; i <= r; i++)                                                 \\
    cout << vec[i] << " ";                                                     \\
  cout << endl;
#define forf(i, a, b) for (int i = (a); i < (b); i++)
#define forr(i, a, b) for (int i = (a); i > (b); i--)
#define input(vec, N)                                                          \\
  for (int i = 0; i < (N); i++)                                                \\
    cin >> vec[i];
#define debug(x) cerr << #x << " = " << (x) << endl;

// template starts
typedef long long int ll;
#define int ll
// template ends here
vector<int> S;
int N, K;
void solve() {
  // code starts from here
  cin >> N >> K;
  vector<pair<int, int>> ran;
  for (int k = 0; k < K; k++) {
    int l, r;
    cin >> l >> r;
    ran.pb(mp(l, r));
  }
  sort(all(ran));

  vector<int> dp(N + 1, 0);
  vector<int> p(N + 2, 0);
  dp[1] = 1;
  p[1] = 1;

  p[0] = 0;
  for (int i = 2; i <= N; i++) {
    int add = 0;
    for (int j = 0; j < K; j++) {
      if (ran[j].first >= i)
        break;

      int l = max((int)1, i - ran[j].second);
      int r = i - ran[j].first;

      add += p[r] - p[l - 1];
      // debug(p[ran[j].second]);
      add %= MOD;
      add += MOD;
      add %= MOD;
    }
    // debug(add);
    dp[i] = add;
    dp[i] %= MOD;
    p[i] = (p[i - 1] + dp[i]) % MOD;

    // print(dp,0,N);
    // print(p,0,N);
    // cerr << endl;
  }
  cout << dp[N] << endl;
}

clock_t startTime;
double getCurrentTime() {
  return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // startTime = clock();

  int T;
  // cin >> T;
  T = 1;
  while (T--) {
    solve();
  }

  // cout << getCurrentTime() << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

#define MOD (998244353)
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define nl '\\n'
#define all(x) x.begin(), x.end()
#define print(vec, l, r)                                                       \\
  for (int i = l; i <= r; i++)                                                 \\
    cout << vec[i] << " ";                                                     \\
  cout << endl;
#define forf(i, a, b) for (int i = (a); i < (b); i++)
#define forr(i, a, b) for (int i = (a); i > (b); i--)
#define input(vec, N)                                                          \\
  for (int i = 0; i < (N); i++)                                                \\
    cin >> vec[i];
#define debug(x) cerr << #x << " = " << (x) << endl;

// template starts
typedef long long int ll;
#define int ll
// template ends here
vector<int> S;
int N, K;
void solve() {
  // code starts from here
  cin >> N >> K;
  while (K--) {
    int l, r;
    cin >> l >> r;
    for (int i = l; i <= r; i++)
      S.pb(i);
  }

  sort(all(S));

  vector<int> dp(N + 1, 0);
  dp[1] = 1;
  int M = S.size();
  for (int i = 2; i <= N; i++) {
    for (int j = 0; j < M && S[j] < i; j++) {
      dp[i] += dp[i - S[j]];
      dp[i] %= MOD;
    }
  }
  cout << dp[N] << endl;
}

clock_t startTime;
double getCurrentTime() {
  return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // startTime = clock();

  int T;
  // cin >> T;
  T = 1;
  while (T--) {
    solve();
  }

  // cout << getCurrentTime() << endl;
  return 0;
}