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

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

template <long long mod> class Mod {
  long long value;
  void set_value(const long long x) { this->value = x % mod; }

public:
  Mod() { set_value(0ll); }

  template <typename T> Mod(T x = 0) { set_value((long long)x); }

  friend std::istream &operator>>(std::istream &is, Mod &x) {
    long long tmp;
    is >> tmp;
    x.set_value(tmp);
    return is;
  }

  friend std::ostream &operator<<(std::ostream &os, const Mod &x) {
    os << x.value;
    return os;
  }

  operator long long() const { return this->value; }

  template <typename T> bool operator<(const T x) {
    return this->value < (long long)x;
  }

  template <typename T> bool operator<=(const T x) {
    return this->value <= (long long)x;
  }

  template <typename T> bool operator>(const T x) {
    return this->value > (long long)x;
  }

  template <typename T> bool operator>=(const T x) {
    return this->value >= (long long)x;
  }

  template <typename T> bool operator==(const T x) {
    return this->value == (long long)x;
  }

  template <typename T> Mod &operator=(const T x) {
    set_value((long long)x);
    return *this;
  }

  template <typename T> Mod &operator&=(const T x) {
    set_value(this->value & (long long)x);
    return *this;
  }

  template <typename T> Mod &operator|=(const T x) {
    set_value(this->value | (long long)x);
    return *this;
  }

  template <typename T> Mod &operator^=(const T x) {
    set_value(this->value ^ (long long)x);
    return *this;
  }

  template <typename T> Mod &operator<<=(const T x) {
    set_value(this->value << (long long)x);
    return *this;
  }

  template <typename T> Mod &operator>>=(const T x) {
    set_value(this->value >> (long long)x);
    return *this;
  }

  template <typename T> Mod &operator+=(const T x) {
    set_value(this->value + (long long)x);
    return *this;
  }

  template <typename T> Mod &operator-=(const T x) {
    set_value(this->value - (long long)x + mod);
    return *this;
  }

  template <typename T> Mod &operator*=(const T x) {
    set_value(this->value * (long long)x);
    return *this;
  }

  template <typename T> Mod &operator/=(const T x) {
    long long a = (long long)x;
    long long b = mod;
    long long u = 1;
    long long v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    u %= mod;
    if (u < 0)
      u += mod;
    *this *= Mod(u);
    return *this;
  }

  template <typename T> Mod operator&(const T x) const {
    return Mod(*this) &= x;
  }

  template <typename T> Mod operator|(const T x) const {
    return Mod(*this) |= x;
  }

  template <typename T> Mod operator^(const T x) const {
    return Mod(*this) ^= x;
  }

  template <typename T> Mod operator<<(const T x) const {
    return Mod(*this) <<= x;
  }

  template <typename T> Mod operator>>(const T x) const {
    return Mod(*this) >>= x;
  }

  template <typename T> Mod operator+(const T x) const {
    return Mod(*this) += x;
  }

  template <typename T> Mod operator-(const T x) const {
    return Mod(*this) -= x;
  }

  template <typename T> Mod operator*(const T x) const {
    return Mod(*this) *= x;
  }

  template <typename T> Mod operator/(const T x) const {
    return Mod(*this) /= x;
  }

  Mod &operator++() {
    *this += 1;
    return *this;
  }

  Mod &operator--() {
    this->set_value(--this->value);
    return *this;
  }

  Mod operator++(int) {
    Mod ret(*this);
    ++*this;
    return ret;
  }

  Mod operator--(int) {
    Mod ret(*this);
    --*this;
    return ret;
  }
};

int main() {
  constexpr long long MOD = 998244353;
  size_t N, K;
  cin >> N >> K;

  vector<Mod<MOD>> dp(2 * N, 0);
  dp[0] = 0;

  int L, R;
  vector<pair<int, int>> S;
  for (size_t i = 0; i < K; i++) {
    cin >> L >> R;
    dp[L]++;
    dp[R + 1]--;
    S.push_back(make_pair(L, R));
  }

  for (size_t i = 1; i < N; i++) {
    dp[i] += dp[i - 1];
    for (pair<int, int> s : S) {
      int l = s.first;
      int r = s.second;
      dp[i + l] += dp[i];
      dp[i + r + 1] -= dp[i];
    }
  }
  cout << dp[N - 1] << endl;
}


Error: TLE

Fixed submission:

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

template <long long mod> class Mod {
  long long value;
  void set_value(const long long x) { this->value = x % mod; }

public:
  Mod() { set_value(0ll); }

  template <typename T> Mod(T x = 0) { set_value((long long)x); }

  friend std::istream &operator>>(std::istream &is, Mod &x) {
    long long tmp;
    is >> tmp;
    x.set_value(tmp);
    return is;
  }

  friend std::ostream &operator<<(std::ostream &os, const Mod &x) {
    os << x.value;
    return os;
  }

  operator long long() const { return this->value; }

  template <typename T> bool operator<(const T x) {
    return this->value < (long long)x;
  }

  template <typename T> bool operator<=(const T x) {
    return this->value <= (long long)x;
  }

  template <typename T> bool operator>(const T x) {
    return this->value > (long long)x;
  }

  template <typename T> bool operator>=(const T x) {
    return this->value >= (long long)x;
  }

  template <typename T> bool operator==(const T x) {
    return this->value == (long long)x;
  }

  template <typename T> Mod &operator=(const T x) {
    set_value((long long)x);
    return *this;
  }

  template <typename T> Mod &operator&=(const T x) {
    set_value(this->value & (long long)x);
    return *this;
  }

  template <typename T> Mod &operator|=(const T x) {
    set_value(this->value | (long long)x);
    return *this;
  }

  template <typename T> Mod &operator^=(const T x) {
    set_value(this->value ^ (long long)x);
    return *this;
  }

  template <typename T> Mod &operator<<=(const T x) {
    set_value(this->value << (long long)x);
    return *this;
  }

  template <typename T> Mod &operator>>=(const T x) {
    set_value(this->value >> (long long)x);
    return *this;
  }

  template <typename T> Mod &operator+=(const T x) {
    set_value(this->value + (long long)x);
    return *this;
  }

  template <typename T> Mod &operator-=(const T x) {
    set_value(this->value - (long long)x + mod);
    return *this;
  }

  template <typename T> Mod &operator*=(const T x) {
    set_value(this->value * (long long)x);
    return *this;
  }

  template <typename T> Mod &operator/=(const T x) {
    long long a = (long long)x;
    long long b = mod;
    long long u = 1;
    long long v = 0;
    while (b) {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    u %= mod;
    if (u < 0)
      u += mod;
    *this *= Mod(u);
    return *this;
  }

  template <typename T> Mod operator&(const T x) const {
    return Mod(*this) &= x;
  }

  template <typename T> Mod operator|(const T x) const {
    return Mod(*this) |= x;
  }

  template <typename T> Mod operator^(const T x) const {
    return Mod(*this) ^= x;
  }

  template <typename T> Mod operator<<(const T x) const {
    return Mod(*this) <<= x;
  }

  template <typename T> Mod operator>>(const T x) const {
    return Mod(*this) >>= x;
  }

  template <typename T> Mod operator+(const T x) const {
    return Mod(*this) += x;
  }

  template <typename T> Mod operator-(const T x) const {
    return Mod(*this) -= x;
  }

  template <typename T> Mod operator*(const T x) const {
    return Mod(*this) *= x;
  }

  template <typename T> Mod operator/(const T x) const {
    return Mod(*this) /= x;
  }

  Mod &operator++() {
    *this += 1;
    return *this;
  }

  Mod &operator--() {
    this->set_value(--this->value);
    return *this;
  }

  Mod operator++(int) {
    Mod ret(*this);
    ++*this;
    return ret;
  }

  Mod operator--(int) {
    Mod ret(*this);
    --*this;
    return ret;
  }
};

template <long long MOD>
Mod<MOD> f(size_t n, vector<Mod<MOD>> &dp, vector<int> &S) {
  if (dp[n] == 0) {
    for (int s : S) {
      if (n - s < n) {
        dp[n] += f(n - s, dp, S);
      }
    }
  }
  return dp[n];
}

int main() {
  constexpr long long MOD = 998244353;
  size_t N, K;
  cin >> N >> K;

  vector<Mod<MOD>> dp(N, 0);
  dp[0] = 1;

  int L, R;
  vector<int> S;
  for (size_t i = 0; i < K; i++) {
    cin >> L >> R;
    for (int s = L; s <= R; s++) {
      S.push_back(s);
    }
  }
  f(N - 1, dp, S);
  cout << dp.back() << endl;

  // int L, R;
  // vector<pair<int, int>> LR;
  // for (size_t i = 0; i < K; i++)
  // {
  //     cin >> L >> R;
  //     LR.push_back(make_pair(L, R));
  // }
  // for (size_t i = 0; i < K; i++)
  // {
  //     }
}