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

/*****And nothing else matters!******/

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

#define pi 3.1415926535
#define endl "\\n"
#define yes cout << "YES\\n"
#define no cout << "NO\\n"
#define ll long long
#define llu unsigned long long
#define setbits(x) __builtin_popcountll(x)
#define pb push_back
#define mp make_pair
#define sz(x) (int)x.size()
#define tc                                                                     \\
  int test_case;                                                               \\
  cin >> test_case;                                                            \\
  while (test_case--)
#define f(i, a, n) for (int i = a; i < n; i++)
#define rf(i, n, a) for (int i = n - 1; i >= a; i--)
#define F first
#define S second
#define all(c) (c).begin(), (c).end()
#define get(a, n) f(i, 0, n) cin >> a[i];
#define show(a, n)                                                             \\
  f(i, 0, n) cout << a[i] << " ";                                              \\
  cout << endl;
#define sp(x) fixed << setprecision(10) << x
#define nitro                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define trace(x) cerr << '>' << #x << ':' << (x) << "\\n"
#define trace2(x, y)                                                           \\
  cerr << '>' << #x << ':' << (x) << " | " << #y << ':' << (y) << "\\n"
#define trace3(a, b, c)                                                        \\
  cerr << #a << "=" << (a) << ", " << #b << "=" << (b) << ", " << #c << "="    \\
       << (c) << "\\n"
#define trace4(a, b, c, d)                                                     \\
  cerr << #a << "=" << (a) << ", " << #b << "=" << (b) << ", " << #c << "="    \\
       << (c) << ", " << #d << "=" << (d) << "\\n"

typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pii, int> ppi;
typedef vector<pii> vpi;
typedef map<int, int> mii;
typedef map<ll, ll> mll;

const int inf = 1e9;
const ll inf64 = 1e18;
const int mod = 998244353;
const int N = 2e5 + 10;

vpi seg(N);
vl dp(N), pre(N);

int main() {
  nitro;
  int n, k;
  cin >> n >> k;
  f(i, 0, k) cin >> seg[i].F >> seg[i].S;

  pre[1] = 1;
  for (int i = 1; i <= n; i++) {
    if (i > 2)
      pre[i] = (pre[i] + pre[i - 1]) % mod;
    pre[i] = (pre[i] + dp[i]) % mod;
    for (int j = 0; j < k; j++) {
      if (i + seg[j].F <= n)
        dp[i + seg[j].F] = (dp[i + seg[j].F] + pre[i]) % mod;
      if (i + seg[j].S + 1 <= n)
        dp[i + seg[j].S + 1] = (dp[i + seg[j].S + 1] - pre[i]) % mod;
    }
  }
  cout << (pre[n] + mod) % mod;
}


Error: TLE

Fixed submission:

/*****And nothing else matters!******/

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

#define pi 3.1415926535
#define endl "\\n"
#define yes cout << "YES\\n"
#define no cout << "NO\\n"
#define ll long long
#define llu unsigned long long
#define setbits(x) __builtin_popcountll(x)
#define pb push_back
#define mp make_pair
#define sz(x) (int)x.size()
#define tc                                                                     \\
  int test_case;                                                               \\
  cin >> test_case;                                                            \\
  while (test_case--)
#define f(i, a, n) for (int i = a; i < n; i++)
#define rf(i, n, a) for (int i = n - 1; i >= a; i--)
#define F first
#define S second
#define all(c) (c).begin(), (c).end()
#define get(a, n) f(i, 0, n) cin >> a[i];
#define show(a, n)                                                             \\
  f(i, 0, n) cout << a[i] << " ";                                              \\
  cout << endl;
#define sp(x) fixed << setprecision(10) << x
#define nitro                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define trace(x) cerr << '>' << #x << ':' << (x) << "\\n"
#define trace2(x, y)                                                           \\
  cerr << '>' << #x << ':' << (x) << " | " << #y << ':' << (y) << "\\n"
#define trace3(a, b, c)                                                        \\
  cerr << #a << "=" << (a) << ", " << #b << "=" << (b) << ", " << #c << "="    \\
       << (c) << "\\n"
#define trace4(a, b, c, d)                                                     \\
  cerr << #a << "=" << (a) << ", " << #b << "=" << (b) << ", " << #c << "="    \\
       << (c) << ", " << #d << "=" << (d) << "\\n"

typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<pii, int> ppi;
typedef vector<pii> vpi;
typedef map<int, int> mii;
typedef map<ll, ll> mll;

const int inf = 1e9;
const ll inf64 = 1e18;
const int mod = 998244353;
const int N = 2e5 + 10;

int main() {
  nitro;
  int n, k;
  cin >> n >> k;
  vi steps;
  f(i, 0, k) {
    int l, r;
    cin >> l >> r;
    f(i, l, r + 1) steps.pb(i);
  }
  sort(all(steps));

  // TLE
  vl dp(n);
  dp[0] = 1;
  f(i, 1, n) {
    f(j, 0, sz(steps)) {
      if (steps[j] <= i)
        dp[i] = (dp[i] + dp[i - steps[j]]) % mod;
      else
        break;
    }
  }
  cout << dp[n - 1];
}