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 __gnu_pbds;
using namespace std;
template <typename T>
using indexed_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define in insert
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define sz(v) (int)(v.size())
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define mem(a, s) memset(a, s, sizeof(a))
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define repr(i, a, b) for (int i = a; i >= b; --i)
#define MAX 8000000000000000064LL
#define MIN -8000000000000000064LL
typedef vector<int> vi;
typedef stack<int> sti;
typedef queue<int> qi;
typedef pair<int, int> pii;
typedef set<int> si;
typedef map<int, int> mii;
typedef vector<pii> vpi;
const int mod = 998244353;
ll add(ll a, ll b) { return (a % mod + b % mod + ((MAX) / mod) * mod) % mod; }
ll sub(ll a, ll b) { return (a % mod - b % mod + ((MAX) / mod) * mod) % mod; }
ll mul(ll a, ll b) {
  return ((a % mod) * (b % mod) + ((MAX) / mod) * mod) % mod;
}
ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); }
ll fact[1000007] = {0};
ll expo(ll x, ll y) {
  ll res = 1;
  x = x % mod;
  while (y > 0) {
    if (y & 1)
      res = (1ll * res * x) % mod;
    y = y >> 1;
    x = (1ll * x * x) % mod;
  }
  return res;
}
void facto() {
  fact[0] = 1;
  fact[1] = 1;
  for (ll i = 2; i < 1000007; i++)
    fact[i] = (fact[i - 1] * i) % mod;
}
ll ncr(ll n, ll r) {
  ll res = 1;
  res = fact[n];
  res = (res * (expo(fact[r], mod - 2))) % mod;
  res = (res * (expo(fact[n - r], mod - 2))) % mod;
  return res;
}
ll npr(ll n, ll r) {
  facto();
  ll res = 1;
  res = fact[n];
  res = (res * (expo(fact[n - r], mod - 2))) % mod;
  return res;
}
int _x[] = {0, 0, -1, 1, -1, 1, -1, 1};
int _y[] = {-1, 1, 0, 0, 1, -1, -1, 1};
void solve();
void pre();
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  pre();
  int t = 1;
  // cin >> t;
  rep(i, 1, t) { solve(); }
  return 0;
}
void pre() {}
void solve() {
  int n, k;
  cin >> n >> k;
  int l[k], r[k];
  rep(i, 0, k - 1) { cin >> l[i] >> r[i]; }
  int dp[n + 1];
  mem(dp, 0);
  dp[0] = 0, dp[1] = 1;
  rep(i, 2, n) {
    rep(j, 0, k - 1) {
      if ((i - l[j]) >= 1) {
        dp[i] = add(dp[i], dp[i - l[j]]);
        dp[i] = sub(dp[i], dp[max(0LL, i - r[j] - 1)]);
      }
    }
    if (i == n) {
      cout << dp[n] << " ";
      return;
    }
    dp[i] = add(dp[i], dp[i - 1]);
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using indexed_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                 \\
  sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(    \\
            c i) {
sim > struct rge {
  c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifndef LOCAL
  ~debug() { cerr << endl; }
  eni(!=) cerr << boolalpha << i;
  ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
  sim dor(const c &) { ris; }
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
#define int long long
#define ld long double
#define pb push_back
#define mp make_pair
#define in insert
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define sz(v) (int)(v.size())
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define mem(a, s) memset(a, s, sizeof(a))
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define repr(i, a, b) for (int i = a; i >= b; --i)
typedef vector<int> vi;
typedef stack<int> sti;
typedef queue<int> qi;
typedef pair<int, int> pii;
typedef set<int> si;
typedef map<int, int> mii;
typedef vector<pii> vpi;
int _x[] = {0, 0, -1, 1, -1, 1, -1, 1};
int _y[] = {-1, 1, 0, 0, 1, -1, -1, 1};
void solve();
void pre();
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  pre();
  int t = 1;
  // cin >> t;
  rep(i, 1, t) {
    // cout << "Case # " << i << ": ";
    solve();
  }
  return 0;
}
const int mod = 998244353;
void pre() {}
void solve() {
  int n, k;
  cin >> n >> k;
  unordered_set<int> s;
  int x, y;
  rep(i, 1, k) {
    cin >> x >> y;
    rep(i, x, y) s.in(i);
    // s.in(y);
  }
  int dp[n + 1];
  dp[1] = 1;
  rep(i, 2, n) {
    dp[i] = 0;
    for (auto x : s) {
      if (i - x > 0) {
        dp[i] = (dp[i] % mod + dp[i - x] % mod) % mod;
      }
    }
  }
  cout << dp[n];
}