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

// Library
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

// TYPEDEFS
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;

// CONSTANTS
#define SZA 0
#define SZB 0

// MACRO
#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y))
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
#define ABS(X) (((X) > 0) ? (X) : -(X))
#define YN(X) ((X) ? "YES" : "NO")
#define SWAP(T, X, Y)                                                          \\
  {                                                                            \\
    T t;                                                                       \\
    t = X;                                                                     \\
    X = Y;                                                                     \\
    Y = t;                                                                     \\
  }

#define F1 first
#define S2 second
#define PB push_back
#define MP make_pair

// Global Tables

// ull gPreProcess[SZA][SZB];

// Functions

void Print2DVector(vector<vector<int>> &v) {
  ull i, j;
  for (i = 0; i < v.size(); i++) {
    for (j = 0; j < v[i].size(); j++) {
      cout << v[i][j];
      if (j == v[i].size() - 1) {
        cout << endl;
      } else {
        cout << " ";
      }
    }
  }
}

void PrintVector(vi &v) {
  ull i;
  for (i = 0; i < v.size(); i++) {
    cout << v[i];
    if (i == v.size() - 1) {
      cout << endl;
    } else {
      cout << " ";
    }
  }
}

void PrintArr(int *a, ull N) {
  ull i;
  for (i = 0; i < N; i++) {
    cout << a[i];
    if (i == N - 1) {
      cout << endl;
    } else {
      cout << " ";
    }
  }
}

void PreProcessTables() {
  ull i, j;
  for (i = 0; i < SZA; i++) {
    for (j = 0; j < SZB; j++) {
      break;
    }
    break;
  }
}

void TC(int t) {
  ull mod_val = 998244353;
  ull dp[200001] = {0};
  ull dpsum[200001] = {0};
  ull N, K, L, R;
  cin >> N >> K;
  vector<pair<ull, ull>> seg;
  for (ull i = 0; i < K; i++) {
    cin >> L >> R;
    seg.push_back(make_pair(L, R));
  }
  dp[1] = 1;
  dpsum[1] = 1;
  for (ull i = 2; i < N + 1; i++) {
    for (ull j = 0; j < K; j++) {
      if (i <= seg[j].first) {
        continue;
      } else {
        L = seg[j].first;
        R = min(seg[j].second, i - 1);
        dp[i] = (dp[i] + mod_val + dpsum[i - L] - dpsum[i - R - 1]) % mod_val;
      }
    }
    dpsum[i] = (dpsum[i - 1] + dp[i]) % mod_val;
  }
  cout << dp[N] << endl;
}

// Main

int main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  PreProcessTables();

  int T = 1, t;
  // cin >> T;
  for (t = 1; t <= T; t++) {
    TC(t);
  }
  return 0;
}


Error: TLE

Fixed submission:

// Library
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

// TYPEDEFS
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> pi;

// CONSTANTS
#define SZA 0
#define SZB 0

// MACRO
#define MAX(X, Y) (((X) > (Y)) ? (X) : (Y))
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
#define ABS(X) (((X) > 0) ? (X) : -(X))
#define YN(X) ((X) ? "YES" : "NO")
#define SWAP(T, X, Y)                                                          \\
  {                                                                            \\
    T t;                                                                       \\
    t = X;                                                                     \\
    X = Y;                                                                     \\
    Y = t;                                                                     \\
  }

#define F1 first
#define S2 second
#define PB push_back
#define MP make_pair

// Global Tables

// ull gPreProcess[SZA][SZB];

// Functions

void Print2DVector(vector<vector<int>> &v) {
  ull i, j;
  for (i = 0; i < v.size(); i++) {
    for (j = 0; j < v[i].size(); j++) {
      cout << v[i][j];
      if (j == v[i].size() - 1) {
        cout << endl;
      } else {
        cout << " ";
      }
    }
  }
}

void PrintVector(vi &v) {
  ull i;
  for (i = 0; i < v.size(); i++) {
    cout << v[i];
    if (i == v.size() - 1) {
      cout << endl;
    } else {
      cout << " ";
    }
  }
}

void PrintArr(int *a, ull N) {
  ull i;
  for (i = 0; i < N; i++) {
    cout << a[i];
    if (i == N - 1) {
      cout << endl;
    } else {
      cout << " ";
    }
  }
}

void PreProcessTables() {
  ull i, j;
  for (i = 0; i < SZA; i++) {
    for (j = 0; j < SZB; j++) {
      break;
    }
    break;
  }
}

void TC(int t) {
  ull mod_val = 998244353;
  ull dp[200001] = {0};
  ull N, K, L, R;
  cin >> N >> K;
  vector<pair<ull, ull>> seg;
  for (ull i = 0; i < K; i++) {
    cin >> L >> R;
    seg.push_back(make_pair(L, R));
  }
  dp[1] = 1;
  for (ull i = 2; i < N + 1; i++) {
    for (ull j = 0; j < K; j++) {
      if (i <= seg[j].first) {
        continue;
      } else {
        L = seg[j].first;
        R = min(seg[j].second, i - 1);
        for (ull k = L; k <= R; k++) {
          dp[i] += dp[i - k];
        }
      }
    }
    dp[i] %= mod_val;
  }
  cout << dp[N] << endl;
}

// Main

int main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  PreProcessTables();

  int T = 1, t;
  // cin >> T;
  for (t = 1; t <= T; t++) {
    TC(t);
  }
  return 0;
}