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

// #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define free                                                                   \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define Fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
#define pb push_back
#define mp make_pair
#define F first
#define S second

bool vowel(char che) {
  if (che == 'A' || che == 'E' || che == 'I' || che == 'O' || che == 'U' ||
      che == 'Y')
    return true;
  else
    return false;
}

const int N = 2e5 + 5;
const int mod = 998244353;
ll a[N], l[N], r[N], b[N], c[N], d, dp[N], dis, pr[N];
pair<ll, ll> p[N];
char o;
bool mark[N], h;
string s, s1;
stack<int> st;
set<ll> st1, st2;
deque<int> dq;
vector<int> vt, vs, vm;
// int a1[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main() {
  Fast ll t = 0, n, m = 0, k = 0, f = 0, sum = 0, ans = 0, n1 = 0;
  cin >> n >> n1;
  ll mn = 1e6;
  for (int i = 0; i < n1; i++) {
    cin >> p[i].F >> p[i].S;
    mn = min(mn, p[i].F);
  }
  sort(p, p + n1);
  dp[mn + 1] = 1;
  pr[mn + 1] = 2;
  for (int i = 1; i <= mn; i++) {
    pr[i] = 1;
  }
  for (int i = mn + 2; i <= n; i++) {
    ll mx = 0;
    for (int j = 0; j < n1; j++) {
      mx = max(mx, p[j].F);
      if (p[j].S >= mx) {
        dp[i] += pr[i - mx] - pr[max(ll(0), i - p[j].S - 1)];
        dp[i] %= mod;
      }
      mx = max(p[j].S, mx);
    }
    pr[i] = pr[i - 1] + dp[i];
    pr[i] %= mod;
  }
  cout << (dp[n] + mod) % mod << endl;
}


Error: TLE

Fixed submission:

// #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define free                                                                   \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define Fast                                                                   \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);
#define pb push_back
#define mp make_pair
#define F first
#define S second

bool vowel(char che) {
  if (che == 'A' || che == 'E' || che == 'I' || che == 'O' || che == 'U' ||
      che == 'Y')
    return true;
  else
    return false;
}

const int N = 1e6 + 5;
const int mod = 998244353;
ll a[N], l[N], r[N], b[N], c[N], d, dp[N], dis, tree[N];
pair<ll, ll> p[N];
char o;
bool mark[N], h;
string s, s1;
stack<int> st;
set<ll> st1, st2;
deque<int> dq;
vector<int> vt, vs, vm;
// int a1[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int main() {
  Fast ll t = 0, n, m = 0, k = 0, f = 0, sum = 0, ans = 0, n1 = 0;
  cin >> n >> n1;
  for (int i = 0; i < n1; i++) {
    cin >> p[i].F >> p[i].S;
  }
  sort(p, p + n1);
  t = 0;
  for (int i = 0; i < n1; i++) {
    t = max(t, p[i].F);
    for (int j = t; j <= p[i].S; j++) {
      a[j] = 1;
    }
    t = max(t, p[i].S);
  }
  for (int i = 1; i <= 2e5 + 5; i++) {
    if (a[i] == 1) {

      b[m++] = i;
    }
  }
  for (int i = 0; i < m; i++) {
    dp[b[i] + 1] = 1;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < m; j++) {
      if ((i - b[j]) > 0)
        dp[i] += dp[i - b[j]];
      dp[i] %= mod;
    }
  }
  cout << dp[n] << endl;
}