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

#ifndef DEBUG
#pragma GCC optimize("O3,no-stack-protector")
#pragma GCC optimize("unroll-loops")

#if __cplusplus < 201703L
#pragma GCC target("avx")
#else
#pragma GCC target("avx2")
#endif

#endif

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define dump(x) cout << x << endl
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i <= i##_len; ++i)
#define rep3(i, l, r) for (int i = l, i##_len = (r); i < i##_len; ++i)
#define REP3(i, l, r) for (int i = l, i##_len = (r); i <= i##_len; ++i)
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
typedef int64_t Int;
typedef long double Ld;

using namespace std;
template <class T> using Graph = vector<vector<T>>;

const Ld pi = M_PI;
const Int MOD = 998244353;
const Int INF = 1LL << 62;

Int Floor(Int a, Int b) { return (a - (a % b)) / b; }

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> inline T Gcd(T a, T b) {
  T c;
  while (b) {
    c = b;
    b = a % b;
    a = c;
  }
  return a;
}

Int Lcm(Int a, Int b) {
  if (a < b) {
    swap(a, b);
  }

  return a / Gcd(a, b) * b;
}

vector<Int> divisor(Int n) {
  vector<Int> ret;
  for (Int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }

  return ret;
}

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

  Int n, k;
  cin >> n >> k;
  vector<Int> ans(n + n + 5, 0);
  vector<pair<Int, Int>> s;
  for (Int i = 0; i < k; i++) {
    Int l, r;
    cin >> l >> r;
    s.push_back({l, r});
  }

  ans[1] = 1;
  ans[2] = -1;

  for (Int i = 1; i <= n; i++) {
    ans[i] += ans[i - 1];
    ans[i] %= MOD;

    for (auto [l, r] : s) {
      ans[i + l] += ans[i] + MOD;
      ans[i + l] %= MOD;
      ans[i + r + 1] -= (ans[i] - MOD);
      ans[i + r + 1] %= MOD;
    }
  }

  cout << ans[n] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#ifndef DEBUG
#pragma GCC optimize("O3,no-stack-protector")
#pragma GCC optimize("unroll-loops")

#if __cplusplus < 201703L
#pragma GCC target("avx")
#else
#pragma GCC target("avx2")
#endif

#endif

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define dump(x) cout << x << endl
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REP(i, n) for (int i = 0, i##_len = (n); i <= i##_len; ++i)
#define rep3(i, l, r) for (int i = l, i##_len = (r); i < i##_len; ++i)
#define REP3(i, l, r) for (int i = l, i##_len = (r); i <= i##_len; ++i)
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
typedef int64_t Int;
typedef long double Ld;

using namespace std;
template <class T> using Graph = vector<vector<T>>;

const Ld pi = M_PI;
const Int MOD = 998244353;
const Int INF = 1LL << 62;

Int Floor(Int a, Int b) { return (a - (a % b)) / b; }

template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> inline T Gcd(T a, T b) {
  T c;
  while (b) {
    c = b;
    b = a % b;
    a = c;
  }
  return a;
}

Int Lcm(Int a, Int b) {
  if (a < b) {
    swap(a, b);
  }

  return a / Gcd(a, b) * b;
}

vector<Int> divisor(Int n) {
  vector<Int> ret;
  for (Int i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n)
        ret.push_back(n / i);
    }
  }

  return ret;
}

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

  Int n, k;
  cin >> n >> k;
  vector<Int> ans(n + n + 5, 0), s;
  ans[0] = 1;
  for (Int i = 0; i < k; i++) {
    Int l, r;
    cin >> l >> r;

    for (Int j = l; j <= r; j++) {
      s.push_back(j);
    }
  }
  sort(all(s));

  for (Int i = 1; i <= n; i++) {
    for (auto e : s) {
      if (0 <= i - e) {
        ans[i] += ans[i - e];
        ans[i] %= MOD;
      } else {
        break;
      }
    }
  }

  cout << ans[n - 1] << endl;

  return 0;
}