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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define All(v) (v).begin(), (v).end()
#define pb push_back
#define MP(a, b) make_pair((a), (b))
template <class T> vector<T> make_vec(size_t a, T val) {
  return vector<T>(a, val);
}
template <class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec(ts...))>(a, make_vec(ts...));
}
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Graph = vector<vector<int>>;
template <typename T> struct edge {
  int to;
  T cost;
  edge(int t, T c) : to(t), cost(c) {}
};
template <typename T> using WGraph = vector<vector<edge<T>>>;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
const int MOD = 998244353;

template <uint_fast64_t MOD> class ModInt {
  using u64 = uint_fast64_t;

public:
  u64 val;

  ModInt(const u64 x = 0) : val((x + MOD) % MOD) {}
  constexpr u64 &value() { return val; }
  constexpr ModInt operator-() { return val ? MOD - val : 0; }
  constexpr ModInt operator+(const ModInt &rhs) const {
    return ModInt(*this) += rhs;
  }
  constexpr ModInt operator-(const ModInt &rhs) const {
    return ModInt(*this) -= rhs;
  }
  constexpr ModInt operator*(const ModInt &rhs) const {
    return ModInt(*this) *= rhs;
  }
  constexpr ModInt operator/(const ModInt &rhs) const {
    return ModInt(*this) /= rhs;
  }
  constexpr ModInt &operator+=(const ModInt &rhs) {
    val += rhs.val;
    if (val >= MOD) {
      val -= MOD;
    }
    return *this;
  }
  constexpr ModInt &operator-=(const ModInt &rhs) {
    if (val < rhs.val) {
      val += MOD;
    }
    val -= rhs.val;
    return *this;
  }
  constexpr ModInt &operator*=(const ModInt &rhs) {
    val = val * rhs.val % MOD;
    return *this;
  }

  constexpr ModInt &operator/=(const ModInt &rhs) {
    *this *= rhs.inv();
    return *this;
  }

  constexpr bool operator==(const ModInt &rhs) { return this->val == rhs.val; }
  constexpr bool operator!=(const ModInt &rhs) { return this->val != rhs.val; }
  friend constexpr ostream &operator<<(ostream &os, const ModInt<MOD> &x) {
    return os << x.val;
  }
  friend constexpr istream &operator>>(istream &is, ModInt<MOD> &x) {
    return is >> x.val;
  }

  constexpr ModInt inv() const { return ModInt(*this).pow(MOD - 2); }

  constexpr ModInt pow(ll e) const {
    u64 x = 1, p = val;
    while (e > 0) {
      if (e % 2 == 0) {
        p = (p * p) % MOD;
        e /= 2;
      } else {
        x = (x * p) % MOD;
        e--;
      }
    }
    return ModInt(x);
  }
};

using mint = ModInt<MOD>;

// Range Sum Query by LazySegmentTree
struct LazySegTree {
private:
  int n;
  vector<mint> node, lazy;

public:
  LazySegTree(vector<mint> v) {
    int sz = (int)v.size();
    n = 1;
    while (n < sz)
      n *= 2;
    node.resize(2 * n - 1);
    lazy.resize(2 * n - 1, 0);

    for (int i = 0; i < sz; i++)
      node[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = node[i * 2 + 1] + node[i * 2 + 2];
  }

  // k番目のノードについて遅延評価
  void eval(int k, int l, int r) {
    // 遅延配列を見て空でなかったら値を伝播
    if (lazy[k] != 0) {
      node[k] += lazy[k];
      // 最下段かどうかチェック
      // 伝播
      if (r - l > 1) {
        lazy[2 * k + 1] += lazy[k] / 2;
        lazy[2 * k + 2] += lazy[k] / 2;
      }
    }

    // 伝播の終了
    lazy[k] = 0;
  }

  mint at(int k) { return getsum(k, k + 1); }

  // [a,b)区間加算
  void add(int a, int b, mint x, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    // まず評価
    eval(k, l, r);
    // 範囲外なら何もしない
    if (b <= l || r <= a)
      return;

    // 完全に被覆 遅延配列に値を入れ評価
    if (a <= l && r <= b) {
      lazy[k] += mint(r - l) * x;
      eval(k, l, r);
    }
    // そうでないとき 子コードの値を再帰的に計算して計算済みの値をもってくる
    else {
      add(a, b, x, 2 * k + 1, l, (l + r) / 2);
      add(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = node[2 * k + 1] + node[2 * k] + 2;
    }
  }

  // [a,b)区間取得
  mint getsum(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    if (b <= l || r <= a)
      return 0;

    // まず評価
    eval(k, l, r);
    if (a <= l && r <= b)
      return node[k];
    mint vl = getsum(a, b, 2 * k + 1, l, (l + r) / 2);
    mint vr = getsum(a, b, 2 * k + 2, (l + r) / 2, r);
    return vl + vr;
  }
};

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> L(K), R(K);
  rep(i, K) cin >> L[i] >> R[i];
  auto dp = make_vec(N + 1, mint(0));
  dp[0] = 1;
  vector<mint> imos(N + 1);
  mint sum = 1;
  for (int i = 0; i < N; i++) {
    sum += imos[i];
    dp[i] = sum;
    for (int j = 0; j < K; j++) {
      imos[min(N, i + L[j])] += dp[i];
      imos[min(N, i + R[j] + 1)] -= dp[i];
    }
  }
  cout << imos[N - 1] << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define All(v) (v).begin(), (v).end()
#define pb push_back
#define MP(a, b) make_pair((a), (b))
template <class T> vector<T> make_vec(size_t a, T val) {
  return vector<T>(a, val);
}
template <class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec(ts...))>(a, make_vec(ts...));
}
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using Graph = vector<vector<int>>;
template <typename T> struct edge {
  int to;
  T cost;
  edge(int t, T c) : to(t), cost(c) {}
};
template <typename T> using WGraph = vector<vector<edge<T>>>;
const int INF = 1 << 30;
const ll LINF = 1LL << 60;
const int MOD = 998244353;

template <uint_fast64_t MOD> class ModInt {
  using u64 = uint_fast64_t;

public:
  u64 val;

  ModInt(const u64 x = 0) : val((x + MOD) % MOD) {}
  constexpr u64 &value() { return val; }
  constexpr ModInt operator-() { return val ? MOD - val : 0; }
  constexpr ModInt operator+(const ModInt &rhs) const {
    return ModInt(*this) += rhs;
  }
  constexpr ModInt operator-(const ModInt &rhs) const {
    return ModInt(*this) -= rhs;
  }
  constexpr ModInt operator*(const ModInt &rhs) const {
    return ModInt(*this) *= rhs;
  }
  constexpr ModInt operator/(const ModInt &rhs) const {
    return ModInt(*this) /= rhs;
  }
  constexpr ModInt &operator+=(const ModInt &rhs) {
    val += rhs.val;
    if (val >= MOD) {
      val -= MOD;
    }
    return *this;
  }
  constexpr ModInt &operator-=(const ModInt &rhs) {
    if (val < rhs.val) {
      val += MOD;
    }
    val -= rhs.val;
    return *this;
  }
  constexpr ModInt &operator*=(const ModInt &rhs) {
    val = val * rhs.val % MOD;
    return *this;
  }

  constexpr ModInt &operator/=(const ModInt &rhs) {
    *this *= rhs.inv();
    return *this;
  }

  constexpr bool operator==(const ModInt &rhs) { return this->val == rhs.val; }
  constexpr bool operator!=(const ModInt &rhs) { return this->val != rhs.val; }
  friend constexpr ostream &operator<<(ostream &os, const ModInt<MOD> &x) {
    return os << x.val;
  }
  friend constexpr istream &operator>>(istream &is, ModInt<MOD> &x) {
    return is >> x.val;
  }

  constexpr ModInt inv() const { return ModInt(*this).pow(MOD - 2); }

  constexpr ModInt pow(ll e) const {
    u64 x = 1, p = val;
    while (e > 0) {
      if (e % 2 == 0) {
        p = (p * p) % MOD;
        e /= 2;
      } else {
        x = (x * p) % MOD;
        e--;
      }
    }
    return ModInt(x);
  }
};

using mint = ModInt<MOD>;

// Range Sum Query by LazySegmentTree
struct LazySegTree {
private:
  int n;
  vector<mint> node, lazy;

public:
  LazySegTree(vector<mint> v) {
    int sz = (int)v.size();
    n = 1;
    while (n < sz)
      n *= 2;
    node.resize(2 * n - 1);
    lazy.resize(2 * n - 1, 0);

    for (int i = 0; i < sz; i++)
      node[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = node[i * 2 + 1] + node[i * 2 + 2];
  }

  // k番目のノードについて遅延評価
  void eval(int k, int l, int r) {
    // 遅延配列を見て空でなかったら値を伝播
    if (lazy[k] != 0) {
      node[k] += lazy[k];
      // 最下段かどうかチェック
      // 伝播
      if (r - l > 1) {
        lazy[2 * k + 1] += lazy[k] / 2;
        lazy[2 * k + 2] += lazy[k] / 2;
      }
    }

    // 伝播の終了
    lazy[k] = 0;
  }

  mint at(int k) { return getsum(k, k + 1); }

  // [a,b)区間加算
  void add(int a, int b, mint x, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    // まず評価
    eval(k, l, r);
    // 範囲外なら何もしない
    if (b <= l || r <= a)
      return;

    // 完全に被覆 遅延配列に値を入れ評価
    if (a <= l && r <= b) {
      lazy[k] += mint(r - l) * x;
      eval(k, l, r);
    }
    // そうでないとき 子コードの値を再帰的に計算して計算済みの値をもってくる
    else {
      add(a, b, x, 2 * k + 1, l, (l + r) / 2);
      add(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = node[2 * k + 1] + node[2 * k] + 2;
    }
  }

  // [a,b)区間取得
  mint getsum(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    if (b <= l || r <= a)
      return 0;

    // まず評価
    eval(k, l, r);
    if (a <= l && r <= b)
      return node[k];
    mint vl = getsum(a, b, 2 * k + 1, l, (l + r) / 2);
    mint vr = getsum(a, b, 2 * k + 2, (l + r) / 2, r);
    return vl + vr;
  }
};

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> L(K), R(K);
  rep(i, K) cin >> L[i] >> R[i];
  auto dp = make_vec(N + 1, mint(0));
  dp[0] = 1;
  LazySegTree segtree(dp);
  for (int i = 0; i < N; i++) {
    dp[i] = segtree.at(i);
    for (int j = 0; j < K; j++) {
      segtree.add(min(N, i + L[j]), min(N + 1, i + R[j] + 1), dp[i]);
    }
  }
  cout << dp[N - 1] << endl;
}