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

/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cmath>     //abs,,,
#include <cstdint>   // int64_t, int*_t
#include <deque>     // deque
#include <iomanip>
#include <iostream> // cout, endl, cin
#include <limits>   //setprecision
#include <map>      // map
#include <math.h>   //pow,,,
#include <numeric>  //accumulate,,,
#include <queue>    // queue, priority_queue
#include <set>      // set,multiset
#include <stack>    // stack
#include <string>   // string, to_string, stoi
#include <tuple>    // tuple, make_tuple
#include <utility>  // pair, make_pair
#include <vector>   // vector
// #define endl "\\n";
using namespace std;

const long long INF = 4000000000000000001;
const int inf = 1001001001;
long long MOD = 998244353;

void chmax(long long &a, long long b) {
  if (a < b)
    a = b;
}
void chmin(long long &a, long long b) {
  if (a > b)
    a = b;
}

// Solve N^M. This, mod_pow use Iterative Square Method.
long long mod_pow(long long N, long long M) {
  if (M == 0)
    return 1;
  long long res = mod_pow((N * N) % MOD, M / 2);
  // When end-of-a bit is 1, times simple N.
  if (M & 1)
    res = (res * N) % MOD;
  return res %= MOD;
}
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

// dp[i]:そのマスへの来方の総和
long long dp[200005];
long long rui[200005];

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  long long N;
  int K;
  cin >> N >> K;
  vector<pair<int, int>> move(K);
  for (int i = 0; i < K; i++)
    cin >> move.at(i).first >> move.at(i).second;
  for (int i = 0; i < 200005; i++)
    dp[i] = 0, rui[i] = 0;
  dp[0] = 1;
  rui[1] = 1;
  for (int i = 0; i < N; i++) {
    for (pair<int, int> P : move) {
      int left = max(i - P.second, 0), right = max(i - P.first + 1, 0);
      dp[i] += rui[right] - rui[left];
      dp[i] %= MOD;
      if (dp[i] < 0)
        dp[i] += MOD;
    }
    // cout << dp[i] << " ";
    rui[i + 1] = rui[i] + dp[i];
    rui[i + 1] %= MOD;
    if (rui[i + 1] < 0)
      rui[i + 1] += MOD;
  }
  // for(int i = 0;i<=N;i++) cout << dp[i] << " ";
  // cout << endl;
  cout << dp[N - 1] << endl;
}


Error: TLE

Fixed submission:

/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cmath>     //abs,,,
#include <cstdint>   // int64_t, int*_t
#include <deque>     // deque
#include <iomanip>
#include <iostream> // cout, endl, cin
#include <limits>   //setprecision
#include <map>      // map
#include <math.h>   //pow,,,
#include <numeric>  //accumulate,,,
#include <queue>    // queue, priority_queue
#include <set>      // set,multiset
#include <stack>    // stack
#include <string>   // string, to_string, stoi
#include <tuple>    // tuple, make_tuple
#include <utility>  // pair, make_pair
#include <vector>   // vector
// #define endl "\\n";
using namespace std;

const long long INF = 4000000000000000001;
const int inf = 1001001001;
long long MOD = 998244353;

void chmax(long long &a, long long b) {
  if (a < b)
    a = b;
}
void chmin(long long &a, long long b) {
  if (a > b)
    a = b;
}

// Solve N^M. This, mod_pow use Iterative Square Method.
long long mod_pow(long long N, long long M) {
  if (M == 0)
    return 1;
  long long res = mod_pow((N * N) % MOD, M / 2);
  // When end-of-a bit is 1, times simple N.
  if (M & 1)
    res = (res * N) % MOD;
  return res %= MOD;
}
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

// dp[i]:そのマスへの来方の総和
long long dp[200005];

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  long long N;
  int K;
  cin >> N >> K;
  vector<long long> move;
  for (int i = 0; i < K; i++) {
    int f, t;
    cin >> f >> t;
    for (int j = f; j <= t; j++) {
      move.push_back(j);
    }
  }
  sort(move.begin(), move.end());
  for (int i = 0; i < 200005; i++)
    dp[i] = 0;
  dp[N] = 1;
  for (long long i = N; i >= 1; i--) {
    for (int j = 0; j < move.size(); j++) {
      if (i - move.at(j) < 1)
        break;
      dp[i - move.at(j)] += dp[i];
      dp[i - move.at(j)] %= MOD;
    }
  }
  cout << dp[1] << endl;
}