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

#include <cstdint>
#include <iostream>
#include <set>
#include <utility>
#include <vector>

using namespace std;

template <typename T, T kMod> struct ModInt {
  using M = ModInt;
  T v;

  ModInt(T _v = 0) { set_norm(_v % kMod + kMod); }
  M &set_norm(T _v) {
    v = (_v < kMod) ? _v : _v - kMod;
    return *this;
  }

  explicit operator bool() const { return v != 0; }
  M operator+(const M &a) const { return M().set_norm(v + a.v); }
  M operator-(const M &a) const { return M().set_norm(v + kMod - a.v); }
  M operator*(const M &a) const { return M().set_norm(ull(v) * a.v % kMod); }
  M operator/(const M &a) const { return *this * a.inv(); }
  M &operator+=(const M &a) { return *this = *this + a; }
  M &operator-=(const M &a) { return *this = *this - a; }
  M &operator*=(const M &a) { return *this = *this * a; }
  M &operator/=(const M &a) { return *this = *this / a; }
  M operator-() const { return M() - *this; }
  M &operator++(int) { return *this = *this + 1; }
  M &operator--(int) { return *this = *this - 1; }

  M pow(T n) const {
    if (n < 0)
      return inv().pow(-n);
    M x = *this, res = 1;
    while (n) {
      if (n & 1)
        res *= x;
      x *= x;
      n >>= 1;
    }
    return res;
  }

  M inv() const {
    T a = v, b = kMod, p = 1, q = 0, t;
    while (b != 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(p -= t * q, q);
    }
    return M(p);
  }

  bool operator==(const M &a) const { return v == a.v; }
  bool operator!=(const M &a) const { return v != a.v; }
  friend ostream &operator<<(ostream &os, const M &a) { return os << a.v; }
  static T get_mod() { return kMod; }
};

using MInt = ModInt<int64_t, 998244353>;

struct Step {
  int64_t from;
  int64_t left;
  int64_t right;
  int64_t pattern;
};

int main() {
  int64_t N, K;
  cin >> N >> K;

  vector<pair<int64_t, int64_t>> ranges;
  ranges.reserve(10);
  for (int64_t k = 0; k < K; ++k) {
    int64_t l, r;
    cin >> l >> r;
    ranges.push_back({l, r + 1});
  }
  vector<MInt> dp(N + 1);
  dp[1] = 1;
  dp[2] = -1;
  for (int64_t n = 1; n <= N; ++n) {
    dp[n] += dp[n - 1];
    for (auto &range : ranges) {
      if (n + range.first <= N) {
        dp[n + range.first] += dp[n];
      }
      if (n + range.second <= N) {
        dp[n + range.second] -= dp[n];
      }
    }
  }
  cout << dp[N] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <cstdint>
#include <iostream>
#include <set>
#include <utility>
#include <vector>

using namespace std;

constexpr int64_t kMod = 998244353;

struct Step {
  int64_t from;
  int64_t left;
  int64_t right;
  int64_t pattern;
};

int main() {
  int64_t N, K;
  cin >> N >> K;

  vector<pair<int64_t, int64_t>> ranges;
  ranges.reserve(10);
  for (int64_t k = 0; k < K; ++k) {
    int64_t l, r;
    cin >> l >> r;
    ranges.push_back({l, r});
  }
  vector<Step> steps;
  int64_t step = 1;
  for (int64_t n = 1; n <= N; ++n) {
    set<int64_t> froms;
    if (n != 1)
      step = 0;
    for (auto it = steps.begin(); it != steps.end();) {
      if (froms.count((*it).from)) {
        ++it;
        continue;
      }
      if ((*it).left <= n && n <= (*it).right) {
        step += (*it).pattern;
        step %= kMod;
        froms.insert((*it).from);
      }
      if ((*it).right == n) {
        it = steps.erase(it);
      } else {
        ++it;
      }
    }
    if (n != N) {
      for (auto &range : ranges) {
        steps.push_back({n, n + range.first, n + range.second, step});
      }
    }
  }
  cout << step << endl;

  return 0;
}