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

#include <bits/stdc++.h>
using namespace std;
#define ALL(x) (x).begin(), (x).end()
#define COUT(x) cout << (x) << "\\n"
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define REP(i, n) for (int i = 0; i < n; i++)
#define YES(x) cout << (x ? "YES" : "NO") << "\\n"
#define Yes(x) cout << (x ? "Yes" : "No") << "\\n"
#define dump(x) cout << #x << " = " << (x) << "\\n"
#define endl "\\n"
using G = vector<vector<int>>;
using M = map<int, int>;
using P = pair<int, int>;
using PQ = priority_queue<int>;
using PQG = priority_queue<int, vector<int>, greater<int>>;
using V = vector<int>;
using ll = long long;
using edge = struct {
  int to;
  int cost;
};
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1e9;
const int MOD = 998244353;
const ll LINF = 1e18;

int main() {
  IOS;
  int n, k;
  cin >> n >> k;
  vector<int> l(k), r(k);
  REP(i, k) cin >> l[i] >> r[i];

  vector<ll> dp(n + 1), dpsum(n + 1);
  // マス1から始まるため dp[1] = 1 にする
  dp[1] = 1;
  dpsum[1] = 1; // dpの累積和

  for (int i = 2; i <= n; i++) {
    REP(j, k) {
      int li = i - r[j];
      int ri = i - l[j];
      if (ri < 0)
        continue;
      li = max(li, 1);

      dp[i] += dpsum[ri] - dpsum[li - 1];
      if (dp[i] < 0)
        dp[i] += MOD;
      else
        dp[i] %= MOD;
    }
    dpsum[i] = (dpsum[i - 1] + dp[i]) % MOD;
  }
  COUT(dp[n]);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ALL(x) (x).begin(), (x).end()
#define COUT(x) cout << (x) << "\\n"
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define REP(i, n) for (int i = 0; i < n; i++)
#define YES(x) cout << (x ? "YES" : "NO") << "\\n"
#define Yes(x) cout << (x ? "Yes" : "No") << "\\n"
#define dump(x) cout << #x << " = " << (x) << "\\n"
#define endl "\\n"
using G = vector<vector<int>>;
using M = map<int, int>;
using P = pair<int, int>;
using PQ = priority_queue<int>;
using PQG = priority_queue<int, vector<int>, greater<int>>;
using V = vector<int>;
using ll = long long;
using edge = struct {
  int to;
  int cost;
};
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1e9;
const int MOD = 998244353;
const ll LINF = 1e18;

const int MAXN = 200010;
// int dp[10][MAXN];
int dp[MAXN];

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

  set<int> s;
  int l, r;
  vector<int> tmp(n + 5, 0), ss(n + 5, 0);
  REP(i, k) {
    cin >> l >> r;
    l--;
    r--;
    tmp[l]++;
    tmp[r + 1]--;
  }
  REP(i, n + 1) { ss[i + 1] = ss[i] + tmp[i]; }
  REP(i, n + 1) {
    if (ss[i] > 0)
      s.insert(i);
  }

  /*
  int i = 0;
  dp[0][0] = 1;
  for(auto x: s) {
    REP(j,n) {
      dp[i+1][j] += dp[i][j];
      if (j + x <= n) {
        cout << i << " " << j << " " << x << " " << dp[i][j] << endl;
        dp[i+1][j+x] += dp[i][j];
        dp[i+1][j+x] %= MOD;
      }
    }
    i++;
  }
  cout << endl;

  REP(i,n) {
    cout << dp[s.size()-1][i] << " ";
  }
  COUT(dp[s.size()][n]);
  */

  dp[0] = 1;
  for (int i = 1; i < n; i++) {
    for (auto x : s) {
      if (i - x < 0)
        break;
      if (dp[i - x] == 0)
        continue;
      dp[i] += dp[i - x];
      dp[i] %= MOD;
    }
  };
  COUT(dp[n - 1]);

  return 0;
}