Problem p02538 (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;

inline ll mod_pow(ll x, ll n, ll mod) {
  if (n == 0)
    return 1;
  ll res = mod_pow(x * x % mod, n / 2, mod);
  if (n & 1)
    res = res * x % mod;
  return res;
}

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>;

// LazySegmentTree(0-indexed)
template <class Monoid, class OperatorMonoid = Monoid> class LazySegTree {
private:
  using F = function<Monoid(Monoid, Monoid)>;
  using G = function<Monoid(Monoid, OperatorMonoid)>;
  using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;
  using L = function<OperatorMonoid(OperatorMonoid, int)>;

  int n;
  const F f;
  const G g;
  const H h;
  const L reflect_len;
  const Monoid e;
  const OperatorMonoid o_e;
  vector<Monoid> node;
  vector<OperatorMonoid> lazy;

public:
  LazySegTree(int sz, const F _f, const G _g, const H _h, const L _l,
              const Monoid &_e, const OperatorMonoid &_o_e)
      : f(_f), g(_g), h(_h), reflect_len(_l), e(_e), o_e(_o_e) {
    n = 1;
    while (n < sz) {
      n *= 2;
    }
    node.resize(2 * n - 1, e);
    lazy.resize(2 * n - 1, o_e);
  }

  void set(int k, const Monoid &x) { node[k + n - 1] = x; }

  void build() {
    for (int i = n - 2; i >= 0; i--) {
      node[i] = f(node[2 * i + 1], node[2 * i + 2]);
    }
  }

  // k番目のノードについて遅延評価
  void eval(int k, int l, int r) {
    // 遅延配列を見て空でなかったら値を伝播
    if (lazy[k] != o_e) {
      node[k] = g(node[k], reflect_len(lazy[k], r - l));
      // 伝播
      if (r - l > 1) {
        lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
        lazy[2 * k + 2] = h(lazy[2 * k + 2], lazy[k]);
      }
    }

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

  Monoid at(int k) { return query(k, k + 1); }

  // [a,b)区間加算
  void update(int a, int b, OperatorMonoid 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] = h(lazy[k], x);
      eval(k, l, r);
    }
    // そうでないとき 子コードの値を再帰的に計算して計算済みの値をもってくる
    else {
      update(a, b, x, 2 * k + 1, l, (l + r) / 2);
      update(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = f(node[2 * k + 1], node[2 * k + 2]);
    }
  }

  // [a,b)区間取得
  Monoid query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    // まず評価
    eval(k, l, r);
    if (b <= l || r <= a)
      return e;
    if (a <= l && r <= b)
      return node[k];
    Monoid vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
    Monoid vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return f(vl, vr);
  }

  inline ll height(ll bit) {
    ll ret = 0;
    while (bit > 0) {
      bit /= 2;
      ret++;
    }
    return ret;
  }
};

vector<mint> ten, one;

struct Segtree : LazySegTree<pair<mint, int>, int> {
  using T = pair<mint, int>;
  using U = int;
  using Lazy = LazySegTree<T, U>;
  static T f(T x1, T x2) {
    return {x1.first * ten[x2.second] + x2.first, x1.second + x2.second};
  }
  static T g(T x, U a) { return {one[x.second] * a, x.second}; }
  static U h(U a, U b) { return b; }
  static U l(U a, int len) { return a; }

  Segtree(ll n, const T &e = {mint(0), 0}, const U &o_e = 0)
      : Lazy(n, f, g, h, l, e, o_e) {
    ten.resize(n + 10);
    one.resize(n + 10);
    ten[0] = 1;
    one[0] = 0;
    for (int i = 0; i <= n; i++) {
      ten[i + 1] = ten[i] * 10;
      one[i + 1] = one[i] * 10 + 1;
    }
  }
};

int main() {
  ll N, Q;
  cin >> N >> Q;
  Segtree seg(N);
  rep(i, N) seg.set(i, {mint(1), 1});
  seg.build();
  rep(i, Q) {
    int l, r;
    ll d;
    cin >> l >> r >> d;
    l--;
    seg.update(l, r, d);
    cout << seg.query(0, N).first << 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;

inline ll mod_pow(ll x, ll n, ll mod) {
  if (n == 0)
    return 1;
  ll res = mod_pow(x * x % mod, n / 2, mod);
  if (n & 1)
    res = res * x % mod;
  return res;
}

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>;

// LazySegmentTree(0-indexed)
template <class Monoid, class OperatorMonoid = Monoid> class LazySegTree {
private:
  using F = function<Monoid(Monoid, Monoid)>;
  using G = function<Monoid(Monoid, OperatorMonoid)>;
  using H = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;
  using L = function<OperatorMonoid(OperatorMonoid, int)>;

  int n;
  const F f;
  const G g;
  const H h;
  const L reflect_len;
  const Monoid e;
  const OperatorMonoid o_e;
  vector<Monoid> node;
  vector<OperatorMonoid> lazy;

public:
  LazySegTree(int sz, const F _f, const G _g, const H _h, const L _l,
              const Monoid &_e, const OperatorMonoid &_o_e)
      : f(_f), g(_g), h(_h), reflect_len(_l), e(_e), o_e(_o_e) {
    n = 1;
    while (n < sz) {
      n *= 2;
    }
    node.resize(2 * n - 1, e);
    lazy.resize(2 * n - 1, o_e);
  }

  void set(int k, const Monoid &x) { node[k + n - 1] = x; }

  void build() {
    for (int i = n - 2; i >= 0; i--) {
      node[i] = f(node[2 * i + 1], node[2 * i + 2]);
    }
  }

  // k番目のノードについて遅延評価
  void eval(int k, int l, int r) {
    // 遅延配列を見て空でなかったら値を伝播
    if (lazy[k] != o_e) {
      node[k] = g(node[k], reflect_len(lazy[k], r - l));
      // 伝播
      if (r - l > 1) {
        lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
        lazy[2 * k + 2] = h(lazy[2 * k + 2], lazy[k]);
      }
    }

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

  Monoid at(int k) { return query(k, k + 1); }

  // [a,b)区間加算
  void update(int a, int b, OperatorMonoid 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] = h(lazy[k], x);
      eval(k, l, r);
    }
    // そうでないとき 子コードの値を再帰的に計算して計算済みの値をもってくる
    else {
      update(a, b, x, 2 * k + 1, l, (l + r) / 2);
      update(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = f(node[2 * k + 1], node[2 * k + 2]);
    }
  }

  // [a,b)区間取得
  Monoid query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    // まず評価
    eval(k, l, r);
    if (b <= l || r <= a)
      return e;
    if (a <= l && r <= b)
      return node[k];
    Monoid vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
    Monoid vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return f(vl, vr);
  }

  inline ll height(ll bit) {
    ll ret = 0;
    while (bit > 0) {
      bit /= 2;
      ret++;
    }
    return ret;
  }
};

struct Segtree : LazySegTree<pair<mint, int>, int> {
  using T = pair<mint, int>;
  using U = int;
  using Lazy = LazySegTree<T, U>;
  static T f(T x1, T x2) {
    return {x1.first * mint(10).pow(x2.second) + x2.first,
            x1.second + x2.second};
  }
  static T g(T x, U a) {
    return {(mint(10).pow(x.second) - 1) / mint(9) * a, x.second};
  }
  static U h(U a, U b) { return b; }
  static U l(U a, int len) { return a; }

  Segtree(ll n, const T &e = {mint(0), 0}, const U &o_e = 0)
      : Lazy(n, f, g, h, l, e, o_e) {}
};

int main() {
  ll N, Q;
  cin >> N >> Q;
  Segtree seg(N);
  rep(i, N) seg.set(i, {mint(1), 1});
  seg.build();
  rep(i, Q) {
    int l, r;
    ll d;
    cin >> l >> r >> d;
    l--;
    seg.update(l, r, d);
    cout << seg.query(0, N).first << endl;
  }
}