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

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

template <typename T, typename U> bool cmin(T &a, U b) {
  return a > b && (a = b, true);
}
template <typename T, typename U> bool cmax(T &a, U b) {
  return a < b && (a = b, true);
}

template <unsigned int mod> class modint {
private:
  unsigned int v;
  static unsigned int norm(const unsigned int &x) {
    return x < mod ? x : x - mod;
  }
  static modint make(const unsigned int &x) {
    modint m;
    return m.v = x, m;
  }
  static modint inv(const modint &x) { return make(inverse(x.v, mod)); }
  static unsigned int inverse(int a, int m) {
    int u[] = {a, 1, 0}, v[] = {m, 0, 1}, t;
    while (*v) {
      t = *u / *v;
      swap(u[0] -= t * v[0], v[0]), swap(u[1] -= t * v[1], v[1]),
          swap(u[2] -= t * v[2], v[2]);
    }
    return (u[1] % m + m) % m;
  }

public:
  modint() : v{0} {}
  modint(const long val) : v{norm(val % mod + mod)} {}
  modint(const modint<mod> &n) : v{n()} {}
  explicit operator bool() const noexcept { return v != 0; }
  bool operator!() const noexcept { return !static_cast<bool>(*this); }
  modint &operator=(const modint &n) { return v = n(), (*this); }
  modint &operator=(const long val) {
    return v = norm(val % mod + mod), (*this);
  }
  modint operator+() const { return *this; }
  modint operator-() const { return v == 0 ? make(0) : make(mod - v); }
  modint operator+(const modint &val) const { return make(norm(v + val())); }
  modint operator-(const modint &val) const {
    return make(norm(v + mod - val()));
  }
  modint operator*(const modint &val) const {
    return make((long)v * val() % mod);
  }
  modint operator/(const modint &val) const { return *this * inv(val); }
  modint &operator+=(const modint &val) { return *this = *this + val; }
  modint &operator-=(const modint &val) { return *this = *this - val; }
  modint &operator*=(const modint &val) { return *this = *this * val; }
  modint &operator/=(const modint &val) { return *this = *this / val; }
  modint operator+(const long val) const { return modint{v + val}; }
  modint operator-(const long val) const { return modint{v - val}; }
  modint operator*(const long val) const {
    return modint{(long)(v * (val % mod))};
  }
  modint operator/(const long val) const { return modint{(long)v * inv(val)}; }
  modint &operator+=(const long val) { return *this = *this + val; }
  modint &operator-=(const long val) { return *this = *this - val; }
  modint &operator*=(const long val) { return *this = *this * val; }
  modint &operator/=(const long val) { return *this = *this / val; }
  bool operator==(const modint &val) const { return v == val.v; }
  bool operator!=(const modint &val) const { return !(*this == val); }
  bool operator==(const long val) const { return v == norm(val % mod + mod); }
  bool operator!=(const long val) const { return !(*this == val); }
  unsigned int operator()() const { return v; }
  friend modint operator+(const long val, const modint &n) { return n + val; }
  friend modint operator-(const long val, const modint &n) {
    return modint{val - n()};
  }
  friend modint operator*(const long val, const modint &n) { return n * val; }
  friend modint operator/(const long val, const modint &n) {
    return modint{val} / n;
  }
  friend bool operator==(const long val, const modint &n) { return n == val; }
  friend bool operator!=(const long val, const modint &n) {
    return !(val == n);
  }
  friend istream &operator>>(istream &is, modint &n) {
    unsigned int v;
    return is >> v, n = v, is;
  }
  friend ostream &operator<<(ostream &os, const modint &n) {
    return (os << n());
  }
  friend modint mod_pow(modint x, long n) {
    modint ans = 1;
    while (n) {
      if (n & 1)
        ans *= x;
      x *= x, n >>= 1;
    }
    return ans;
  }
};

// constexpr int MOD = 1000000007;
constexpr int MOD = 998244353;
using mint = modint<MOD>;

template <typename T, T (*op)(T, T), T (*i_el)(), typename U, T (*mapp)(U, T),
          U (*comp)(U, U), U (*i_ty)()>
class LazySegmentTree {
private:
  int N, size, log;
  vector<T> D;
  vector<U> L;
  void apply(int k) { D.at(k) = op(D.at(2 * k), D.at(2 * k + 1)); }
  void update_all(int k, U f) {
    D.at(k) = mapp(f, D.at(k));
    if (k < size)
      L.at(k) = comp(L.at(k), f);
  }
  void push(int k) {
    update_all(2 * k, L.at(k));
    update_all(2 * k + 1, L.at(k));
    L.at(k) = i_ty();
  }
  int ceil_pow(int n) {
    int x = 0;
    while ((1U << x) < (unsigned int)(n))
      x++;
    return x;
  }

public:
  LazySegmentTree() : LazySegmentTree(0) {}
  LazySegmentTree(int n) : LazySegmentTree(vector<T>(n, i_el())) {}
  LazySegmentTree(const vector<T> &v) : N(int(v.size())) {
    log = ceil_pow(N);
    size = 1 << log;
    D = vector<T>(2 * size, i_el());
    L = vector<U>(size, i_ty());
    for (int i = 0; i < N; i++)
      D.at(size + i) = v.at(i);
    for (int i = size - 1; i >= 1; i--)
      apply(i);
  }
  void set(int p, T x) {
    p += size;
    for (int i = log; i >= 1; i--)
      push(p >> i);
    D.at(p) = x;
    for (int i = 1; i <= log; i++)
      apply(p >> i);
  }
  T get(int p) {
    p += size;
    for (int i = log; i >= 1; i--)
      push(p >> i);
    return D.at(p);
  }
  T query(int l, int r) {
    r++;
    if (l == r)
      return i_el();
    l += size, r += size;
    for (int i = log; i >= 1; i--) {
      if (((l >> i) << i) != l)
        push(l >> i);
      if (((r >> i) << i) != r)
        push(r >> i);
    }
    T sml = i_el(), smr = i_el();
    while (l < r) {
      if (l & 1)
        sml = op(sml, D.at(l++));
      if (r & 1)
        smr = op(D.at(--r), smr);
      l >>= 1, r >>= 1;
    }
    return op(sml, smr);
  }
  T query_all() { return D.at(1); }
  void update(int p, U f) {
    p += size;
    for (int i = log; i >= 1; i--)
      push(p >> i);
    D.at(p) = mapp(f, D.at(p));
    for (int i = 1; i <= log; i++)
      apply(p >> i);
  }
  void update(int l, int r, U f) {
    r++;
    if (l == r)
      return;
    l += size, r += size;
    for (int i = log; i >= 1; i--) {
      if (((l >> i) << i) != l)
        push(l >> i);
      if (((r >> i) << i) != r)
        push((r - 1) >> i);
    }
    {
      int l2 = l, r2 = r;
      while (l < r) {
        if (l & 1)
          update_all(l++, f);
        if (r & 1)
          update_all(--r, f);
        l >>= 1, r >>= 1;
      }
      l = l2, r = r2;
    }
    for (int i = 1; i <= log; i++) {
      if (((l >> i) << i) != l)
        apply(l >> i);
      if (((r >> i) << i) != r)
        apply((r - 1) >> i);
    }
  }
  template <bool (*g)(T)> int max_right(int l) {
    return max_right(l, [](T x) { return g(x); });
  }
  template <class G> int max_right(int l, G g) {
    if (l == N)
      return N;
    l += size;
    for (int i = log; i >= 1; i--)
      push(l >> i);
    T sm = i_el();
    do {
      while (l % 2 == 0)
        l >>= 1;
      if (!g(op(sm, D.at(l)))) {
        while (l < size) {
          push(l);
          l = (2 * l);
          if (g(op(sm, D.at(l))))
            sm = op(sm, D.at(l++));
        }
        return l - size;
      }
      sm = op(sm, D.at(l++));
    } while ((l & -l) != l);
    return N;
  }
  template <bool (*g)(T)> int min_left(int r) {
    return min_left(r, [](T x) { return g(x); });
  }
  template <class G> int min_left(int r, G g) {
    if (r == 0)
      return 0;
    r += size;
    for (int i = log; i >= 1; i--)
      push((r - 1) >> i);
    T sm = i_el();
    do {
      r--;
      while (r > 1 && (r % 2))
        r >>= 1;
      if (!g(op(D.at(r), sm))) {
        while (r < size) {
          push(r);
          r = (2 * r + 1);
          if (g(op(D.at(r), sm)))
            sm = op(D.at(r--), sm);
        }
        return r + 1 - size;
      }
      sm = op(D.at(r), sm);
    } while ((r & -r) != r);
    return 0;
  }
};

long mpow(long a, long b) {
  long res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}

// monoid setting

using el = pair<mint, int>;
el op(el l, el r) {
  mint val = l.first * mpow(10, r.second) + r.first;
  int len = l.second + r.second;
  return {val, len};
}
el i_el() { return {0, 0}; }

// mapping setting

using type = int;
el mapp(type f, el e) {
  if (f == 0)
    return e;
  e.first = (mint(mpow(10, e.second)) - 1) / 9 * f;
  return e;
}
type comp(type f1, type f2) {
  if (f2 == 0)
    return f1;
  return f2;
}
type i_ty() { return 0; }

// setting end

signed main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  int N, Q;
  cin >> N >> Q;

  vector<el> V(N, {1, 1});
  LazySegmentTree<el, op, i_el, type, mapp, comp, i_ty> ST(V);

  for (int i = 0; i < Q; i++) {
    int L, R, D;
    cin >> L >> R >> D, L--, R--;
    ST.update(L, R, D);
    cout << ST.query_all().first << "\\n";
  }
}

Error: TLE

Fixed submission:

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

#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;

using bint = cpp_int;
using bdec = cpp_dec_float_100;
// using bdec = number<cpp_dec_float<10000>>;

// constexpr int MOD = 1000000007;
constexpr int MOD = 998244353;

signed main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  int N, Q;
  cin >> N >> Q;
  string S(N, '1');
  for (int i = 0; i < Q; i++) {
    int L, R, D;
    cin >> L >> R >> D, L--, R--;
    string tmp(R - L + 1, '0' + D);
    S = S.substr(0, L) + tmp + S.substr(R + 1);
    bint ans(S);
    cout << ans % MOD << "\\n";
  }
}