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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define endl "\\n"
#define ar array
#define deb(x) cout << #x << " = " << x << endl
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll MOD = 998244353;
const ll N = 2e5 + 1;

template <class T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
inline ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
inline ll sub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }
inline ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
struct comp {
  bool operator()(const pair<ll, ll> &x, const pair<ll, ll> &y) const {
    return x.first < y.first;
  }
};
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll pwr(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n & 1)
    return mul(x, pwr(mul(x, x), (n - 1) / 2));
  else
    return pwr(mul(x, x), n / 2);
}
ll modInv(ll n) { return pwr(n, mod - 2); }
ll modInverse(ll a, ll m) {
  ll m0 = m;
  ll y = 0, x = 1;

  if (m == 1)
    return 0;

  while (a > 1) {
    ll q = a / m;
    ll t = m;
    m = a % m, a = t;
    t = y;
    y = x - q * y;
    x = t;
  }

  if (x < 0)
    x += m0;

  return x;
}

int main() {
  IOS;
  int t = 1;
  // cin >> t;
  while (t--) {
    const int mod = 998244353;
    int n, k;
    cin >> n >> k;
    vector<array<int, 2>> intervals(k);
    for (auto &[x, y] : intervals)
      cin >> x >> y;
    vector<ll> dp(n + 1), pref(n + 1);
    dp[1] = pref[1] = 1;
    for (int i = 2; i <= n; ++i) {
      for (auto [l, r] : intervals) {
        int L = max(1, i - r), R = i - l;
        if (L > R)
          continue;
        // Add dp[L...R]
        dp[i] += (pref[R] - pref[L - 1]) % mod;
        dp[i] = (dp[i] % mod + mod) % mod;
      }
      pref[i] = (pref[i - 1] + dp[i]) % mod;
    }
    cout << dp[n];
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define endl "\\n"
#define ar array
#define deb(x) cout << #x << " = " << x << endl
const ll inf = 1e18;
const ll mod = 1e9 + 7;
const ll MOD = 998244353;
const ll N = 2e5 + 1;

template <class T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// order_of_key (k) : Number of items strictly smaller than k .
// find_by_order(k) : K-th element in a set (counting from zero).
inline ll add(ll a, ll b) { return ((a % mod) + (b % mod)) % mod; }
inline ll sub(ll a, ll b) { return ((a % mod) - (b % mod) + mod) % mod; }
inline ll mul(ll a, ll b) { return ((a % mod) * (b % mod)) % mod; }
struct comp {
  bool operator()(const pair<ll, ll> &x, const pair<ll, ll> &y) const {
    return x.first < y.first;
  }
};
ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
ll pwr(ll x, ll n) {
  if (n == 0)
    return 1;
  if (n & 1)
    return mul(x, pwr(mul(x, x), (n - 1) / 2));
  else
    return pwr(mul(x, x), n / 2);
}
ll modInv(ll n) { return pwr(n, mod - 2); }
ll modInverse(ll a, ll m) {
  ll m0 = m;
  ll y = 0, x = 1;

  if (m == 1)
    return 0;

  while (a > 1) {
    ll q = a / m;
    ll t = m;
    m = a % m, a = t;
    t = y;
    y = x - q * y;
    x = t;
  }

  if (x < 0)
    x += m0;

  return x;
}
vector<pair<int, int>> v;
ll dp[200005];
ll solve(int s) {
  // cout << "s " << s << endl;
  if (s == 1)
    return 1;
  else if (dp[s] != -1)
    return dp[s];
  ll ans = 0;

  for (int i = 0; i < v.size() && v[i].first < s; i++) {
    int l = v[i].first, r = v[i].second;
    for (int j = l; j <= r && j < s; j++) {
      ans = (ans + solve(s - j)) % MOD;
    }
  }
  dp[s] = ans;
  return ans;
}
int main() {
  IOS;
  int t = 1;
  // cin >> t;
  while (t--) {
    int n, k, x, y;
    cin >> n >> k;
    int ans = 0;
    for (int i = 0; i < k; i++) {
      cin >> x >> y;
      v.push_back({x, y});
    }
    sort(v.begin(), v.end());
    memset(dp, -1, sizeof(dp));
    cout << solve(n) << endl;
  }
}