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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

/*

*/

void test_case() {
  int n, k;
  cin >> n >> k;

  vector<int> l(k), r(k);
  for (int i = 0; i < k; i++) {
    cin >> l[i] >> r[i];
  }

  sort(l.begin(), l.end());
  sort(r.begin(), r.end());
  ll mod = 998244353;
  vector<ll> res(n, 0);
  res[0] = 1;

  vector<ll> sd(k, 0);
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < k; j++) {
      if (i - l[j] < 0)
        continue;

      if (i - r[j] > 0) {
        sd[j] += res[i - l[j]];
        sd[j] -= res[i - r[j] - 1];

      } else {
        sd[j] += res[i - l[j]];
      }
      // if( i == 1 && j ==

      sd[j] += mod;
      sd[j] %= mod;
      res[i] += sd[j];
      res[i] %= mod;
    }
  }

  cout << res[n - 1] << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int t;
  t = 1;
  // cin >> t;
  while (t--)
    test_case();
  return 0;
}

Error: TLE

Fixed submission:

/*It is better to taste failure
          than to regret*/

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <ratio>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

typedef long long ll;
typedef long double LD;
#define int ll
#define double LD
#define pb push_back
#define mp make_pair
#define REP(i, n) for (int i = 0; i < n; i++)
#define loop(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REPD(i, n) for (int i = n - 1; i >= 0; i--)
#define FORD(i, a, b) for (int i = a; i >= b; i--)
#define remax(a, b) a = max(a, b)
#define remin(a, b) a = min(a, b)
#define mem(a, b) memset(a, b, sizeof a)
#define append push_back
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define print(x) cout << (x) << endl
#define PRECISION(x) cout << fixed << setprecision(x)
#define FAST_IO                                                                \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define Test()                                                                 \\
  {                                                                            \\
    int t;                                                                     \\
    cin >> t;                                                                  \\
    for (int i = 1; i <= t; i++) {                                             \\
      cout << "Case #" << i << ": ";                                           \\
      solve();                                                                 \\
    }                                                                          \\
  }
#define test()                                                                 \\
  {                                                                            \\
    int t;                                                                     \\
    cin >> t;                                                                  \\
    for (int i = 1; i <= t; i++) {                                             \\
      solve();                                                                 \\
    }                                                                          \\
  }
#define vi vector<int>
#define vii vector<pair<int, int>>
#define vci vector<pair<char, int>>
#define vic vector<pair<int, char>>
#define N 200005
#define pb push_back
const double PI = acos(-1);
const int MOD = 1000000007;
const int FMOD = 998244353;

int dp[N];
int n, k;
map<int, int> mp1;
int solve(int start, vi &v) {
  if (start > n) {
    return 0;
  }

  if (start == n) {
    return 1;
  }
  if (dp[start] != -1)
    return dp[start];

  int ans = 0;
  int size = v.size();
  for (int i = 0; i < size; i++) {
    ans = (ans % FMOD + solve(start + v[i], v) % FMOD) % FMOD;
  }
  return dp[start] = ans;
}
void solve() {

  cin >> n >> k;

  set<int> st;
  loop(i, k) {
    int x, y;
    cin >> x >> y;

    for (int j = x; j <= y; j++)
      st.insert(j);
  }

  vi v;
  for (auto x : st)
    v.pb(x);

  mem(dp, -1);
  cout << solve(1, v) << endl;
}
signed main() {
  FAST_IO;

  // test();
  solve();
}