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

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <math.h>
#include <numeric>
#include <utility>
#include <vector>

#define int long long

struct edge {
  int to;
  int cost;
};

using namespace std;
using vi = vector<int>;
using vii = vector<vector<int>>;
using vb = vector<bool>;
using qi = queue<int>;
using P = pair<int, int>;
using graph = vector<vector<edge>>;
// using graph = vector<vector<int>>;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define REP(i, n) for (int i = 0; i < n; i++)
#define delim(i, n) cout << (i == n - 1 ? "\\n" : " ");

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const int inf = 1LL << 60;
// const int inf = 100000000;
const int mod = 1000000007;

const int maxn = 2e5 + 2;
int n, m, k;
vi dp(maxn);
vii num(10, vi(2));
vi nsum(10);

vi dx = {-1, 0, 0, 1};
vi dy = {0, -1, 1, 0};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> k;
  REP(i, k) { cin >> num[i][0] >> num[i][1]; }

  dp[0] = 1;

  REP(i, n) {
    REP(j, k) {
      if (num[j][1] < i) {
        nsum[j] =
            nsum[j] - dp[i - num[j][1] - 1] + dp[i - num[j][0]] + 998244353;
        nsum[j] %= 998244353;
        dp[i] += nsum[j];
        dp[i] %= 998244353;
      } else if (num[j][0] <= i) {
        nsum[j] += dp[i - num[j][0]];
        nsum[j] %= 998244353;
        dp[i] += nsum[j];
        dp[i] %= 998244353;
      }
    }
  }

  cout << std::fixed << std::setprecision(10) << dp[n - 1] << "\\n";
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cstdlib>
#include <fstream>
#include <functional>
#include <iostream>
#include <math.h>
#include <numeric>
#include <utility>
#include <vector>

#define int long long

struct edge {
  int to;
  int cost;
};

using namespace std;
using vi = vector<int>;
using vii = vector<vector<int>>;
using vb = vector<bool>;
using qi = queue<int>;
using P = pair<int, int>;
using graph = vector<vector<edge>>;
// using graph = vector<vector<int>>;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define REP(i, n) for (int i = 0; i < n; i++)
#define delim(i, n) cout << (i == n - 1 ? "\\n" : " ");

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const int inf = 1LL << 60;
// const int inf = 100000000;
const int mod = 1000000007;

const int maxn = 2e5 + 2;
int n, m, k;
vi dp(maxn);
vi num;

vi dx = {-1, 0, 0, 1};
vi dy = {0, -1, 1, 0};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> k;
  REP(i, k) {
    int l, r;
    cin >> l >> r;
    for (int j = l; j <= r; j++)
      num.push_back(j);
  }

  int len = (int)num.size();
  dp[0] = 1;

  REP(i, n) {
    REP(j, len) {
      if (num[j] <= i) {
        dp[i] += dp[i - num[j]];
        dp[i] %= 998244353;
      }
    }
  }

  cout << std::fixed << std::setprecision(10) << dp[n - 1] << "\\n";
}