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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define ins insert

#define debug(a) cerr << (a) << endl
#define dbrep(a, n)                                                            \\
  rep(_i, n) cerr << (a[_i]) << " ";                                           \\
  cerr << endl
#define dbrep2(a, n, m)                                                        \\
  rep(_i, n) {                                                                 \\
    rep(_j, m) cerr << (a[_i][_j]) << " ";                                     \\
    cerr << endl;                                                              \\
  }

using namespace std;

template <class A, class B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  return os << "(" << p.fi << "," << p.se << ")";
}
template <class A, class B> istream &operator>>(istream &is, pair<A, B> &p) {
  return is >> p.fi >> p.se;
}

//-------------------------------------------------
//--ModInt
//-------------------------------------------------
const ::std::uint_fast64_t MOD = 998244353;

class mint {
private:
  using value_type = ::std::uint_fast64_t;
  value_type n;

public:
  mint() : n(0) {}
  mint(::std::int_fast64_t _n) : n(_n < 0 ? MOD - (-_n) % MOD : _n % MOD) {}
  mint(const mint &m) : n(m.n) {}

  friend ::std::ostream &operator<<(::std::ostream &os, const mint &a) {
    return os << a.n;
  }
  friend ::std::istream &operator>>(::std::istream &is, mint &a) {
    value_type temp;
    is >> temp;
    a = mint(temp);
    return is;
  }

  mint &operator+=(const mint &m) {
    n += m.n;
    n = (n < MOD) ? n : n - MOD;
    return *this;
  }
  mint &operator-=(const mint &m) {
    n += MOD - m.n;
    n = (n < MOD) ? n : n - MOD;
    return *this;
  }
  mint &operator*=(const mint &m) {
    n = n * m.n % MOD;
    return *this;
  }
  mint &operator/=(const mint &m) { return *this *= m.inv(); }
  mint &operator++() { return *this += 1; }
  mint &operator--() { return *this -= 1; }

  mint operator+(const mint &m) const { return mint(*this) += m; }
  mint operator-(const mint &m) const { return mint(*this) -= m; }
  mint operator*(const mint &m) const { return mint(*this) *= m; }
  mint operator/(const mint &m) const { return mint(*this) /= m; }
  mint operator++(int) {
    mint t(*this);
    *this += 1;
    return t;
  }
  mint operator--(int) {
    mint t(*this);
    *this -= 1;
    return t;
  }

  bool operator==(const mint &m) const { return n == m.n; }
  bool operator!=(const mint &m) const { return n != m.n; }
  mint operator-() const { return mint(MOD - n); }

  mint pow(value_type b) const {
    mint ret(1), m(*this);
    while (b) {
      if (b & 1)
        ret *= m;
      m *= m;
      b >>= 1;
    }
    return ret;
  }

  mint inv() const { return pow(MOD - 2); }
};

//-------------------------------------------------
//--Lazy Evaluation Segment tree (Proportional)
//-------------------------------------------------
template <typename T = mint, typename E = mint> class LazySegmentTree {
private:
  const T te;
  const E ee;
  inline T f(T x, T y) { return x + y; }
  inline T g(T x, E y) { return (y == ee) ? x : y; }
  inline E h(E x, E y) { return (y == ee) ? x : y; }
  int n, height;
  ::std::vector<T> seg;
  ::std::vector<T> sum;
  ::std::vector<E> lazy;

public:
  LazySegmentTree(int _n, T te, E ee) : te(te), ee(ee) { init(_n); }
  LazySegmentTree(::std::vector<T> &v, T te, E ee) : te(te), ee(ee) {
    int _n = v.size();
    init(_n);
    for (int i = n; i < n + _n; i++)
      seg[i] = v[i - n];
    build();
  }
  void init(int _n) {
    n = 1;
    height = 0;
    while (n < _n)
      n <<= 1, height++;
    seg.resize(2 * n, te);
    sum.resize(2 * n, 0);
    lazy.resize(2 * n, ee);
    sum[n + _n - 1] = 1;
    for (int i = n + _n - 2; i >= n; i--)
      sum[i] = sum[i + 1] * 10;
    for (int i = n - 1; i > 0; i--)
      sum[i] = f(sum[i << 1], sum[i << 1 | 1]);
  }
  void build() {
    for (int i = n - 1; i > 0; i--)
      seg[i] = f(seg[i << 1], seg[i << 1 | 1]);
  }
  T eval(int k) {
    return (lazy[k] == ee) ? seg[k] : g(seg[k], lazy[k] * sum[k]);
  }
  void waveto(int k) {
    for (int i = height; i > 0; i--)
      wave(k >> i);
  }
  void wave(int k) {
    seg[k] = eval(k);
    lazy[k << 1] = h(lazy[k << 1], lazy[k]);
    lazy[k << 1 | 1] = h(lazy[k << 1 | 1], lazy[k]);
    lazy[k] = ee;
  }
  void recalc(int k) {
    for (int i = 1; i <= height; i++) {
      int m = k >> i;
      seg[m] = f(eval(m << 1), eval(m << 1 | 1));
    }
  }
  void update(int a, int b, E x) {
    if (b <= a)
      return;
    waveto(a += n);
    waveto(b += n - 1);
    T L = ee, R = ee;
    int l = a, r = b;
    while (l <= r) {
      if (l & 1)
        lazy[l] = h(lazy[l], x), l++;
      if (!(r & 1))
        lazy[r] = h(lazy[r], x), r--;
      l >>= 1;
      r >>= 1;
    }
    recalc(a);
    recalc(b);
  }
  T query(int a, int b) {
    waveto(a += n);
    waveto(b += n - 1);
    T L = te, R = te;
    int l = a, r = b;
    while (l <= r) {
      if (l & 1)
        L = f(L, eval(l++));
      if (!(r & 1))
        R = f(eval(r--), R);
      l >>= 1;
      r >>= 1;
    }
    return f(L, R);
  }
  T operator[](int k) { return query(k, k + 1); };
  inline int breadth(int k) {
    int ret = height - 31 + __builtin_clz(k);
    return 1 << ret;
  }
};

//-------------------------------------------------

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, Q;
  cin >> N >> Q;
  LazySegmentTree<mint, mint> seg(N, 0, 10);
  seg.update(0, N, 1);
  while (Q--) {
    int l, r, d;
    cin >> l >> r >> d;
    l--;
    seg.update(l, r, d);
    cout << seg.query(0, N) << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define RFOR(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define RITR(x, c) for (__typeof(c.rbegin()) x = c.rbegin(); x != c.rend(); x++)
#define setp(n) fixed << setprecision(n)

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll, ll>
#define pi pair<int, int>

#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define ins insert

#define debug(a) cerr << (a) << endl
#define dbrep(a, n)                                                            \\
  rep(_i, n) cerr << (a[_i]) << " ";                                           \\
  cerr << endl
#define dbrep2(a, n, m)                                                        \\
  rep(_i, n) {                                                                 \\
    rep(_j, m) cerr << (a[_i][_j]) << " ";                                     \\
    cerr << endl;                                                              \\
  }

using namespace std;

template <class A, class B>
ostream &operator<<(ostream &os, const pair<A, B> &p) {
  return os << "(" << p.fi << "," << p.se << ")";
}
template <class A, class B> istream &operator>>(istream &is, pair<A, B> &p) {
  return is >> p.fi >> p.se;
}

//-------------------------------------------------
//--ModInt
//-------------------------------------------------
const ::std::uint_fast64_t MOD = 998244353;

class mint {
private:
  using value_type = ::std::uint_fast64_t;
  value_type n;

public:
  mint() : n(0) {}
  mint(::std::int_fast64_t _n) : n(_n < 0 ? MOD - (-_n) % MOD : _n % MOD) {}
  mint(const mint &m) : n(m.n) {}

  friend ::std::ostream &operator<<(::std::ostream &os, const mint &a) {
    return os << a.n;
  }
  friend ::std::istream &operator>>(::std::istream &is, mint &a) {
    value_type temp;
    is >> temp;
    a = mint(temp);
    return is;
  }

  mint &operator+=(const mint &m) {
    n += m.n;
    n = (n < MOD) ? n : n - MOD;
    return *this;
  }
  mint &operator-=(const mint &m) {
    n += MOD - m.n;
    n = (n < MOD) ? n : n - MOD;
    return *this;
  }
  mint &operator*=(const mint &m) {
    n = n * m.n % MOD;
    return *this;
  }
  mint &operator/=(const mint &m) { return *this *= m.inv(); }
  mint &operator++() { return *this += 1; }
  mint &operator--() { return *this -= 1; }

  mint operator+(const mint &m) const { return mint(*this) += m; }
  mint operator-(const mint &m) const { return mint(*this) -= m; }
  mint operator*(const mint &m) const { return mint(*this) *= m; }
  mint operator/(const mint &m) const { return mint(*this) /= m; }
  mint operator++(int) {
    mint t(*this);
    *this += 1;
    return t;
  }
  mint operator--(int) {
    mint t(*this);
    *this -= 1;
    return t;
  }

  bool operator==(const mint &m) const { return n == m.n; }
  bool operator!=(const mint &m) const { return n != m.n; }
  mint operator-() const { return mint(MOD - n); }

  mint pow(value_type b) const {
    mint ret(1), m(*this);
    while (b) {
      if (b & 1)
        ret *= m;
      m *= m;
      b >>= 1;
    }
    return ret;
  }

  mint inv() const { return pow(MOD - 2); }
};

//-------------------------------------------------
//--Lazy Evaluation Segment tree (Proportional)
//-------------------------------------------------
template <typename T, typename E> class Lazy1 {
private:
  inline T f(T x, T y) { return x + y; }
  inline T g(T x, T y) { return x + y; }
  inline T h(T x, T y) { return x + y; }
  int n, height;
  ::std::vector<T> seg;
  ::std::vector<E> lazy;
  const T te = 0;
  const E ee = 0;

public:
  Lazy1() {}
  void rebuild(::std::vector<T> &v) {
    int _n = v.size();
    init(_n);
    for (int i = n; i < n + _n; i++)
      seg[i] = v[i - n];
    build();
  }
  void init(int _n) {
    n = 1;
    height = 0;
    while (n < _n)
      n <<= 1, height++;
    seg.resize(2 * n, te);
    lazy.resize(2 * n, ee);
  }
  void build() {
    for (int i = n - 1; i > 0; i--)
      seg[i] = f(seg[i << 1], seg[i << 1 | 1]);
  }
  T eval(int k) {
    int c = breadth(k);
    return (lazy[k] == ee) ? seg[k] : g(seg[k], quantify(lazy[k], c));
  }
  void waveto(int k) {
    for (int i = height; i > 0; i--)
      wave(k >> i);
  }
  void wave(int k) {
    seg[k] = eval(k);
    lazy[k << 1] = h(lazy[k << 1], lazy[k]);
    lazy[k << 1 | 1] = h(lazy[k << 1 | 1], lazy[k]);
    lazy[k] = ee;
  }
  void recalc(int k) {
    for (int i = 1; i <= height; i++) {
      int m = k >> i;
      seg[m] = f(eval(m << 1), eval(m << 1 | 1));
    }
  }
  void update(int a, int b, E x) {
    if (b <= a)
      return;
    waveto(a += n);
    waveto(b += n - 1);
    T L = ee, R = ee;
    int l = a, r = b;
    while (l <= r) {
      if (l & 1)
        lazy[l] = h(lazy[l], x), l++;
      if (!(r & 1))
        lazy[r] = h(lazy[r], x), r--;
      l >>= 1;
      r >>= 1;
    }
    recalc(a);
    recalc(b);
  }
  T query(int a, int b) {
    waveto(a += n);
    waveto(b += n - 1);
    T L = te, R = te;
    int l = a, r = b;
    while (l <= r) {
      if (l & 1)
        L = f(L, eval(l++));
      if (!(r & 1))
        R = f(eval(r--), R);
      l >>= 1;
      r >>= 1;
    }
    return f(L, R);
  }
  T operator[](int k) { return query(k, k + 1); };
  inline int breadth(int k) {
    int ret = height - 31 + __builtin_clz(k);
    return 1 << ret;
  }
  // return value proportional to segment's width
  inline E quantify(E laz, int c) { return laz * c; }
};

//-------------------------------------------------
//--Lazy Evaluation Segment tree (Proportional)
//-------------------------------------------------
template <typename T, typename E> class LazySegmentTree {
private:
  inline T f(T x, T y) { return x + y; }
  inline T g(mint x, mint y) { return (y == 10) ? x : y; }
  inline T h(mint x, mint y) { return (y == 10) ? x : y; }
  int n, height;
  ::std::vector<T> seg;
  ::std::vector<E> lazy;
  Lazy1<T, T> sum;
  const T te = 0;
  const E ee = 10;

public:
  LazySegmentTree(int _n) { init(_n); }
  void init(int _n) {
    n = 1;
    height = 0;
    while (n < _n)
      n <<= 1, height++;
    seg.resize(2 * n, te);
    lazy.resize(2 * n, ee);

    ::std::vector<T> summ(n, 0);
    summ[_n - 1] = 1;
    rep(i, _n - 1) { summ[_n - i - 2] = summ[_n - i - 1] * 10; }
    sum.rebuild(summ);
  }
  void build() {
    for (int i = n - 1; i > 0; i--)
      seg[i] = f(seg[i << 1], seg[i << 1 | 1]);
  }
  T eval(int k) {
    int c = breadth(k);
    int left = (k << c) - n;
    int right = left + (1 << c);
    return (lazy[k] == ee) ? seg[k]
                           : g(seg[k], lazy[k] * sum.query(left, right));
  }
  void waveto(int k) {
    for (int i = height; i > 0; i--)
      wave(k >> i);
  }
  void wave(int k) {
    seg[k] = eval(k);
    lazy[k << 1] = h(lazy[k << 1], lazy[k]);
    lazy[k << 1 | 1] = h(lazy[k << 1 | 1], lazy[k]);
    lazy[k] = ee;
  }
  void recalc(int k) {
    for (int i = 1; i <= height; i++) {
      int m = k >> i;
      seg[m] = f(eval(m << 1), eval(m << 1 | 1));
    }
  }
  void update(int a, int b, E x) {
    if (b <= a)
      return;
    waveto(a += n);
    waveto(b += n - 1);
    T L = ee, R = ee;
    int l = a, r = b;
    while (l <= r) {
      if (l & 1)
        lazy[l] = h(lazy[l], x), l++;
      if (!(r & 1))
        lazy[r] = h(lazy[r], x), r--;
      l >>= 1;
      r >>= 1;
    }
    recalc(a);
    recalc(b);
  }
  T query(int a, int b) {
    waveto(a += n);
    waveto(b += n - 1);
    T L = te, R = te;
    int l = a, r = b;
    while (l <= r) {
      if (l & 1)
        L = f(L, eval(l++));
      if (!(r & 1))
        R = f(eval(r--), R);
      l >>= 1;
      r >>= 1;
    }
    return f(L, R);
  }
  T operator[](int k) { return query(k, k + 1); };
  inline int breadth(int k) {
    int ret = height - 31 + __builtin_clz(k);
    return ret;
  }
};

//-------------------------------------------------

const int INF = 101010;

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, Q;
  cin >> N >> Q;

  LazySegmentTree<mint, mint> seg(N);
  seg.update(0, N, 1);
  while (Q--) {
    int l, r, d;
    cin >> l >> r >> d;
    l--;
    seg.update(l, r, d);
    cout << seg.query(0, N) << "\\n";
  }

  return 0;
}