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

#include <bits/stdc++.h>
using namespace std;
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define watch(x) cout << (#x) << " is " << (x) << endl
#define f(t) for (ll i = 0; i < t; i++)
#define ll long long int
#define ld long double
#define umpl unordered_map<ll, ll>
#define vl vector<ll>
#define vld vector<ld>
#define vvl vector<vl>
#define pb push_back
#define pll pair<ll, ll>
#define inf 1e18
#define pcout(x, p) cout << fixed << setprecision(p) << x
#define all(a) a.begin(), a.end()
#define endl "\\n"
const ll mod = 998244353;
inline ll mul(ll a, ll b) { return (a * 1ll * b) % mod; }
inline ll sub(ll a, ll b) {
  ll c = a - b;
  if (c < 0)
    c += mod;
  return c;
}
inline ll add(ll a, ll b) {
  ll c = a + b;
  if (c >= mod)
    c -= mod;
  return c;
}
inline ll max(ll a, ll b) { return a > b ? a : b; }
inline ll min(ll a, ll b) { return a < b ? a : b; }
inline ll ceil(ll a, ll b) { return (a % b == 0) ? (a / b) : (a / b + 1); }
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  for (auto v : V)
    os << v << "  ";
  return os;
}
template <class T> ostream &operator<<(ostream &os, set<T> S) {
  os << "{ ";
  for (auto s : S)
    os << s << " ";
  return os << "}";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R> ostream &operator<<(ostream &os, map<L, R> M) {
  os << "{ ";
  for (auto m : M)
    os << "(" << m.first << ":" << m.second << ") ";
  return os << "}";
}
struct comp {
  template <class T1, class T2, class T3, class T4>
  bool operator()(const pair<T1, T2> &p1, const pair<T3, T4> &p2) {
    return p1.first < p2.first;
  }
};

int main() {
  fio;
  ll n, k;
  cin >> n >> k;
  set<ll> s;
  vector<pll> a(k);
  for (ll i = 0; i < k; i++) {
    ll l, r;
    cin >> l >> r;
    a[i] = {l, r};
  }
  sort(all(a));
  vl dp(n + 1, 0);
  dp[0] = 1;
  dp[1] = 1;
  map<ll, ll> pre;
  pre[1] = 1;
  for (ll i = 2; i <= n; i++) {
    for (int j = 0; j < k; j++) {
      if (a[j].first >= i) {
        break;
      } else {
        ll temp = sub(pre[i - a[j].first], pre[i - a[j].second - 1]);
        dp[i] = add(dp[i], temp);
      }
    }
    pre[i] = add(pre[i - 1], dp[i]);
  }
  cout << dp[n] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fio                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define watch(x) cout << (#x) << " is " << (x) << endl
#define f(t) for (ll i = 0; i < t; i++)
#define ll long long int
#define ld long double
#define umpl unordered_map<ll, ll>
#define vl vector<ll>
#define vld vector<ld>
#define vvl vector<vl>
#define pb push_back
#define pll pair<ll, ll>
#define inf 1e18
#define pcout(x, p) cout << fixed << setprecision(p) << x
#define all(a) a.begin(), a.end()
#define endl "\\n"
const ll mod = 998244353;
inline ll mul(ll a, ll b) { return (a * 1ll * b) % mod; }
inline ll sub(ll a, ll b) {
  ll c = a - b;
  if (c < 0)
    c += mod;
  return c;
}
inline ll add(ll a, ll b) {
  ll c = a + b;
  if (c >= mod)
    c -= mod;
  return c;
}
inline ll max(ll a, ll b) { return a > b ? a : b; }
inline ll min(ll a, ll b) { return a < b ? a : b; }
inline ll ceil(ll a, ll b) { return (a % b == 0) ? (a / b) : (a / b + 1); }
template <class T> ostream &operator<<(ostream &os, vector<T> V) {
  for (auto v : V)
    os << v << "  ";
  return os;
}
template <class T> ostream &operator<<(ostream &os, set<T> S) {
  os << "{ ";
  for (auto s : S)
    os << s << " ";
  return os << "}";
}
template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R> ostream &operator<<(ostream &os, map<L, R> M) {
  os << "{ ";
  for (auto m : M)
    os << "(" << m.first << ":" << m.second << ") ";
  return os << "}";
}
struct comp {
  template <class T1, class T2, class T3, class T4>
  bool operator()(const pair<T1, T2> &p1, const pair<T3, T4> &p2) {
    return p1.first < p2.first;
  }
};

int main() {
  fio;
  ll n, k;
  cin >> n >> k;
  set<ll> s;
  for (ll i = 0; i < k; i++) {
    ll l, r;
    cin >> l >> r;
    for (ll j = l; j <= r; j++) {
      s.insert(j);
    }
  }
  vl dp(n + 1, 0);
  dp[1] = 1;
  for (ll i = 2; i <= n; i++) {
    for (auto j : s) {
      if ((i - j) >= 0) {
        dp[i] = add(dp[i], dp[i - j]);
      }
    }
  }
  cout << dp[n] << endl;
}