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

#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#define fo(i, n) for (long long i = 0; i < n; i++)
#define ll long long int
#define inf 999999999999999999
#define pb push_back
#define fi first
#define se second
#define vi vector<ll>
#define vii vector<vector<ll>>
#define pi pair<ll, ll>
#define mp make_pair
#define MOD 998244353
#define tr(c, it) for (auto it = (c).begin(); it != (c).end(); ++it)
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
using namespace std;
// cerr<< '\\n' << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << "
// ms\\n" ; freopen("explicit.in", "r", stdin); freopen("explicit.out", "w",
// stdout);

void SieveOfEratosthenes() {
  bool prime[10000001];
  memset(prime, true, sizeof(prime));

  for (ll p = 2; p * p <= 10000000; p++) {
    if (prime[p] == true) {
      // Update all multiples of p greater than or
      // equal to the square of it
      // numbers which are multiple of p and are
      // less than p^2 are already been marked.
      for (ll i = p * p; i <= 10000000; i += p) {
        prime[i] = false;
      }
    }
  }
}
ll binpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a);
    a = (a * a);
    b >>= 1;
  }
  return res;
}
ll gcd(ll a, ll b) {
  if (b % a == 0)
    return a;
  return gcd(b % a, a);
}
ll power(ll x, ll y, ll p) {
  ll r = 1;

  x = x % p;
  while (y > 0) {
    if (y & 1)
      r = (r * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return r;
}
ll modInverse(ll n, ll p) { return power(n, p - 2, p); }

ll nCrModP(ll n, ll r, ll p) {
  ll fac[100005];
  if (r == 0)
    return 1;

  return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %
         p;
}
ll a[200006] = {0};
ll pre[200007] = {0};
int32_t main() {
  fio ll j, i, n, t, l, r, c, h, k, m, maxi, d, x;
  cin >> n >> k;
  ll a[n], b[n];
  vector<ll> v;
  pre[0] = 0;
  for (j = 0; j < k; j++) {
    cin >> a[j] >> b[j];
  }
  pre[1] = 1;
  for (i = 2; i <= n; i++) {
    for (j = 0; j < k; j++) {
      if (i - a[j] < 1)
        continue;
      if (i - b[j] < 1)
        pre[i] += pre[i - a[j]];
      else {
        pre[i] += pre[i - a[j]] - pre[i - b[j] - 1];
      }
    }
    pre[i] %= MOD;
    if (i != n)
      pre[i] += pre[i - 1];
  }
  cout << pre[n];
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#define fo(i, n) for (long long i = 0; i < n; i++)
#define ll long long int
#define inf 999999999999999999
#define pb push_back
#define fi first
#define se second
#define vi vector<ll>
#define vii vector<vector<ll>>
#define pi pair<ll, ll>
#define mp make_pair
#define MOD 998244353
#define tr(c, it) for (auto it = (c).begin(); it != (c).end(); ++it)
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
using namespace std;
// cerr<< '\\n' << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << "
// ms\\n" ; freopen("explicit.in", "r", stdin); freopen("explicit.out", "w",
// stdout);

void SieveOfEratosthenes() {
  bool prime[10000001];
  memset(prime, true, sizeof(prime));

  for (ll p = 2; p * p <= 10000000; p++) {
    if (prime[p] == true) {
      // Update all multiples of p greater than or
      // equal to the square of it
      // numbers which are multiple of p and are
      // less than p^2 are already been marked.
      for (ll i = p * p; i <= 10000000; i += p) {
        prime[i] = false;
      }
    }
  }
}
ll binpow(ll a, ll b) {
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = (res * a);
    a = (a * a);
    b >>= 1;
  }
  return res;
}
ll gcd(ll a, ll b) {
  if (b % a == 0)
    return a;
  return gcd(b % a, a);
}
ll power(ll x, ll y, ll p) {
  ll r = 1;

  x = x % p;
  while (y > 0) {
    if (y & 1)
      r = (r * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return r;
}
ll modInverse(ll n, ll p) { return power(n, p - 2, p); }

ll nCrModP(ll n, ll r, ll p) {
  ll fac[100005];
  if (r == 0)
    return 1;

  return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %
         p;
}
ll a[200006] = {0};
ll dp[200007] = {0};
int32_t main() {
  fio ll j, i, n, t, l, r, c, h, k, m, maxi, d, x;
  cin >> n >> k;
  vector<ll> v;
  for (j = 0; j < k; j++) {
    cin >> l >> r;
    a[l] += 1;
    a[r + 1] += -1;
  }
  c = 0;
  for (j = 1; j <= 200006; j++) {
    c = c + a[j];
    if (c > 0) {
      v.pb(j);
      dp[j] = 1;
    }
  }

  for (i = 1; i <= n - 1; i++) {
    for (j = 0; j < v.size(); j++) {
      if (i - (v[j]) > 0) {
        dp[i] = (dp[i] + dp[i - v[j]]) % MOD;
      } else
        break;
    }
  }
  cout << dp[n - 1];
}