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

//---------------------------------------------------------MANAS
//UNIYAL---------------------------------------------------------
//                                                        NEVER  GIVE  UP
#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll long long
#define pb push_back
#define fi first
#define se second
#define pii pair<ll, ll>
#define INF 0x3f3f3f3f
#define LINF 98765432123456789
#define testcases()                                                            \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define debug cout << "Hello\\n";
#define debug1(x) cout << #x << " " << x << "\\n";
#define debug2(x, y) cout << #x << " " << x << " " << #y << " " << y << "\\n";
#define debug3(x, y, z)                                                        \\
  cout << #x << " " << x << " " << #y << " " << y << " " << #z << " " << z     \\
       << "\\n";
#define debug4(w, x, y, z)                                                     \\
  cout << #w << " " << w << " " << #x << " " << x << " " << #y << " " << y     \\
       << " " << #z << " " << z << "\\n";
#define rep(i, start, end) for (ll i = start; i < end; i++)
#define sz(x) (ll)((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
//------------------------------------------------------------------------------------------------------------------------------
#define mod 998244353
int main() {

#ifndef ONLINE_JUDGE
  freopen("C:/Users/DELL/Desktop/input.txt", "r", stdin);
#endif

  fast;
  ll n, k;
  cin >> n >> k;
  vector<pair<ll, ll>> vp(k);
  vector<ll> dp(n + 1, 0);
  for (ll i = 0; i < k; i++)
    cin >> vp[i].fi >> vp[i].se;
  vector<ll> prefix(n + 1, 0);
  dp[0] = prefix[0] = 0;
  dp[1] = prefix[1] = 1;
  for (ll i = 2; i <= n; i++) {
    for (ll j = 0; j < k; j++) {
      ll r = min(n, max(i - vp[j].fi, 0ll));
      ll l = max(i - vp[j].se - 1, 0ll);
      dp[i] = (dp[i] + prefix[r] - prefix[l] + mod) % mod;
    }
    prefix[i] = (prefix[i - 1] + dp[i]) % mod;
  }
  cout << dp[n];
  return 0;
}


Error: TLE

Fixed submission:

//---------------------------------------------------------MANAS
//UNIYAL---------------------------------------------------------
//                                                        NEVER  GIVE  UP
#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define ll long long
#define pb push_back
#define fi first
#define se second
#define pii pair<ll, ll>
#define INF 0x3f3f3f3f
#define LINF 98765432123456789
#define testcases()                                                            \\
  ll t;                                                                        \\
  cin >> t;                                                                    \\
  while (t--)
#define debug cout << "Hello\\n";
#define debug1(x) cout << #x << " " << x << "\\n";
#define debug2(x, y) cout << #x << " " << x << " " << #y << " " << y << "\\n";
#define debug3(x, y, z)                                                        \\
  cout << #x << " " << x << " " << #y << " " << y << " " << #z << " " << z     \\
       << "\\n";
#define debug4(w, x, y, z)                                                     \\
  cout << #w << " " << w << " " << #x << " " << x << " " << #y << " " << y     \\
       << " " << #z << " " << z << "\\n";
#define rep(i, start, end) for (ll i = start; i < end; i++)
#define sz(x) (ll)((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
//------------------------------------------------------------------------------------------------------------------------------
#define mod 998244353
#define N 200005
ll dp[N];
vector<ll> v;
ll func(ll n) {
  if (n == 1)
    return 1;
  if (dp[n] != -1)
    return dp[n];
  ll ans = 0;
  for (auto x : v) {
    if (n >= x + 1) {
      // debug2(n,x)
      ans = (ans + func(n - x)) % mod;
    }
  }
  return dp[n] = ans;
}
int main() {

#ifndef ONLINE_JUDGE
  freopen("C:/Users/DELL/Desktop/input.txt", "r", stdin);
#endif

  fast;
  ll n, k, l, r;
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    cin >> l >> r;
    for (ll x = l; x <= r; x++) {
      // debug1(x)
      v.pb(x);
    }
  }
  memset(dp, -1, sizeof(dp));
  cout << func(n);
  // for(int i=1;i<=n;i++)
  // 	debug2(i,dp[i])
  return 0;
}