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;

#define ff first
#define ss second
#define endl "\\n"
#define loop(i, s, e) for (int i = s; i < e; i++)
#define int long long
#define vi vector<int>
#define S(v) sort(v.begin(), v.end())
#define RS(v) sort(v.rbegin(), v.rend())
#define R(v) reverse(v.begin(), v.end())
#define mxpq(T) priority_queue<T>
#define mnpq(T) priority_queue<T, vector<T>, greater<T>>
#define prv(v)                                                                 \\
  for (auto &x : v)                                                            \\
    cout << x << " ";
#define piirv(v)                                                               \\
  for (auto &x : v)                                                            \\
    cout << x.ff << " " << x.ss << endl;
#define prvv(v)                                                                \\
  for (auto &x : v)                                                            \\
    for (auto &y : x.ss)                                                       \\
      cout << y << " ";
#define mp make_pair
#define pii pair<int, int>
#define mset(a, f) memset(a, f, sizeof(a))
#define pb push_back
#define inf INT_MAX
#define INF LLONG_MAX
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;

const int N = 2e6 + 6;
const int M = 998244353;
int A[N];
int dp[N];
int n, m;

inline int mul(int a, int b) { return (a * b) % M; };
inline int add(int a, int b) {
  int res = a + b;
  if (res < 0)
    res += M;
  if (res >= M)
    res -= M;
  return res;
}

void solve() {
  int n, k;
  cin >> n >> k;
  vector<pii> vec(k);
  loop(i, 0, k) cin >> vec[i].ff >> vec[i].ss;
  S(vec);

  dp[0] = 0;
  dp[1] = 1;

  loop(i, 2, n + 1) {
    loop(j, 0, k) {
      int l = max(i - vec[j].ss, 1ll);
      int r = max(i - vec[j].ff, 0ll);

      dp[i] = add(dp[i], add(dp[r], -dp[l - 1]));
    }
    dp[i] = add(dp[i], dp[i - 1]);
  }

  cout << (dp[n] - dp[n - 1] + M) % M;
}

int32_t main() {

  cout << fixed << setprecision(16);

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t = 1;
  // cin >> t;
  while (t--)
    solve();
}

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;

#define ff first
#define ss second
#define endl "\\n"
#define loop(i, s, e) for (int i = s; i < e; i++)
#define int long long
#define vi vector<int>
#define S(v) sort(v.begin(), v.end())
#define RS(v) sort(v.rbegin(), v.rend())
#define R(v) reverse(v.begin(), v.end())
#define mxpq(T) priority_queue<T>
#define mnpq(T) priority_queue<T, vector<T>, greater<T>>
#define prv(v)                                                                 \\
  for (auto &x : v)                                                            \\
    cout << x << " ";
#define piirv(v)                                                               \\
  for (auto &x : v)                                                            \\
    cout << x.ff << " " << x.ss << endl;
#define prvv(v)                                                                \\
  for (auto &x : v)                                                            \\
    for (auto &y : x.ss)                                                       \\
      cout << y << " ";
#define mp make_pair
#define pii pair<int, int>
#define mset(a, f) memset(a, f, sizeof(a))
#define pb push_back
#define inf INT_MAX
#define INF LLONG_MAX
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    oset;

const int N = 2e6 + 6;
const int M = 998244353;
int A[N];
int dp[N];
int n, k;
map<int, int> mmp;

int alpha(int i) {
  if (i == n)
    return 1;
  if (i > n)
    return 0;
  int &abs = dp[i];
  if (abs != -1)
    return abs;

  int ans = 0;

  for (auto &it : mmp) {
    ans = (ans % M + alpha(i + it.ff) % M) % M;
    ans %= M;
  }

  return abs = ans % M;
}

void solve() {
  cin >> n >> k;

  map<int, int> m;

  loop(i, 0, k) {
    int x, y;
    cin >> x >> y;
    loop(j, x, y + 1) m[j]++;
    // mmp[x]++ , mmp[y]++;
  }
  for (auto &it : m) {
    if (it.ff < n)
      mmp[it.ff]++;
    else
      break;
  }
  mset(dp, -1);
  int ans = alpha(1);

  cout << ans;
}

int32_t main() {

  cout << fixed << setprecision(16);

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int t = 1;
  // cin >> t;
  while (t--)
    solve();
}