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

#include <algorithm> //sort(a.begin(), a.end());reverse(a.begin(), a.end());
#include <bits/stdc++.h>
#include <iostream>
#include <math.h> //pow(x,y)=x^y 99999とかの場合+1するif文
#define ll long long
#define rep(init, i, n) for (ll i = init; i < (n); i++)
#define rev(s) reverse(s.begin(), s.end())
#define sor(v) sort(v.begin(), v.end())
#define PI 3.14159265358979323846264338327950L
// コード自動整形 Shift + Option + F @vscode
using namespace std;
using vec = vector<ll>;

ll facctorialMethod(ll k); // 階乗を出力する
/////main/////
int main() {
  ll mod = 998244353;
  ll N, K, li, ri;
  cin >> N >> K;
  vec L(K), R(K);
  vec dp(N + 1, 0), dpsum(N + 1, 0);

  rep(0, i, K) { cin >> L.at(i) >> R.at(i); }

  dp.at(1) = 1;
  dpsum.at(1) = 1;
  rep(2, x, N + 1) {
    rep(0, i, K) {
      li = x - R.at(i);
      ri = x - L.at(i);
      /*
      cout << "li=" << li << endl;
      cout << "ri=" << ri << endl;
      */
      if (ri < 0)
        continue;
      li = max(li, (ll)1);

      //
      /*
      cout << "i=" << i << endl;
      cout << "R.at(" << i << ")=" << R.at(i) << endl;
      cout << "L.at(" << i << ")=" << L.at(i) << endl;
      cout << "li=" << li << endl;
      cout << "dp.at(" << x << ")=" << dp.at(x) << endl;
      */
      dp.at(x) += dpsum.at(ri);
      dp.at(x) -= dpsum.at(li - 1);
      dp.at(x) += mod;
      dp.at(x) %= mod;
      /*
      cout << "dpsum.at(" << ri << ")=" << dpsum.at(ri) << endl;
      cout << "dpsum.at(" << li - 1 << ")=" << dpsum.at(li - 1) << endl;
      cout << "dp.at(" << x << ")=" << dp.at(x) << endl;
      */
    }
    // cout << dp.at(x) << endl;
    dpsum.at(x) = dpsum.at(x - 1) + dp.at(x);
    /*
    cout << "dpsum.at(" << x << ")=" << dpsum.at(x) << endl;
    */
    dpsum.at(x) %= mod;
  }

  /*rep(0, x, N) rep(0, i, K) rep(L.at(i), j, R.at(i) + 1)
  {
      if (x + j < N)
      {
          dp.at(x + j) += dp.at(x);
          dp.at(x + j) %= mod;
      }
  }*/
  /*rep(0,i,N){cout<<dpsum.at(i)<<endl;
  }*/
  cout << dp.at(N) << endl;
}
/////main/////

/////function/////
ll facctorialMethod(ll k) {
  ll sum = 1;
  for (ll i = 1; i <= k; ++i) {
    sum *= i;
  }
  return sum;
}

Error: TLE

Fixed submission:

#include <algorithm> //sort(a.begin(), a.end());reverse(a.begin(), a.end());
#include <bits/stdc++.h>
#include <iostream>
#include <math.h> //pow(x,y)=x^y 99999とかの場合+1するif文
#define ll long long
#define rep(init, i, n) for (ll i = init; i < (n); i++)
#define rev(s) reverse(s.begin(), s.end())
#define sor(v) sort(v.begin(), v.end())
#define PI 3.14159265358979323846264338327950L
// コード自動整形 Shift + Option + F @vscode
using namespace std;
using vec = vector<ll>;

ll facctorialMethod(ll k); // 階乗を出力する
/////main/////
int main() {
  ll mod = 998244353;
  ll N, K;
  cin >> N >> K;
  vec L(K), R(K);
  vec dp(N, 0);

  rep(0, i, K) { cin >> L.at(i) >> R.at(i); }

  dp.at(0) = 1;

  rep(0, x, N) rep(0, i, K) rep(L.at(i), j, R.at(i) + 1) {
    if (x + j < N) {
      dp.at(x + j) += dp.at(x);
      dp.at(x + j) %= mod;
    }
  }
  cout << dp.at(N - 1) << endl;
}
/////main/////

/////function/////
ll facctorialMethod(ll k) {
  ll sum = 1;
  for (ll i = 1; i <= k; ++i) {
    sum *= i;
  }
  return sum;
}