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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define pb push_back
#define fs first
#define sc second
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;

const ll mod = 998244353;

ll egcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll d = egcd(b, a % b, x, y);
  x -= a / b * y;
  swap(x, y);
  return d;
}

ll mod_inv(ll a, ll m) {
  ll x, y, d = egcd(a, m, x, y);
  return d == 1 ? (x % m + m) % m : -1;
}

ll mod_pow(ll b, ll e, ll m) {
  ll res = 1;
  while (e) {
    if (e & 1)
      res = (res * b % m + m) % m;
    b *= b;
    b %= m;
    e >>= 1;
  }
  return res;
}

const ll inv9 = mod_inv(9, mod);
vector<ll> p10;

struct node {
  int l, r;
  int x, lazy;
  node() {}
  node(int _l, int _r) : l(_l), r(_r), x(0), lazy(0) {}
  node(int _l, int _r, int _x) : node(_l, _r) { x = _x; }
  node(node a, node b) : node(a.l, b.r) {
    x = (p10[b.r - b.l + 1] * a.x + b.x) % mod;
  }
  void update(int v) { x = v; }
  void range_update(int v) { lazy = v; }
  void apply() {
    if (lazy != 0)
      x = ((lazy * (p10[r - l + 1] - 1)) % mod) * inv9 % mod;
    lazy = 0;
  }
  void push(node &u) {
    if (lazy != 0)
      u.lazy = lazy;
  }
};

struct segment_tree {
  int n;
  vector<node> arr;
  segment_tree() {}
  segment_tree(const vector<ll> &a) : n(a.size()), arr(4 * n) {
    mk(a, 0, 0, n - 1);
  }
  node mk(const vector<ll> &a, int i, int l, int r) {
    int m = (l + r) / 2;
    return arr[i] = l > r    ? node(l, r)
                    : l == r ? node(l, r, a[l])
                             : node(mk(a, 2 * i + 1, l, m),
                                    mk(a, 2 * i + 2, m + 1, r));
  }
  node update(int at, ll v, int i = 0) {
    propagate(i);
    int hl = arr[i].l, hr = arr[i].r;
    if (at < hl || hr < at)
      return arr[i];
    if (hl == at && at == hr) {
      arr[i].update(v);
      return arr[i];
    }
    return arr[i] = node(update(at, v, 2 * i + 1), update(at, v, 2 * i + 2));
  }
  node query(int l, int r, int i = 0) {
    propagate(i);
    int hl = arr[i].l, hr = arr[i].r;
    if (r < hl || hr < l)
      return node(hl, hr);
    if (l <= hl && hr <= r)
      return arr[i];
    return node(query(l, r, 2 * i + 1), query(l, r, 2 * i + 2));
  }
  node range_update(int l, int r, ll v, int i = 0) {
    propagate(i);
    int hl = arr[i].l, hr = arr[i].r;
    if (r < hl || hr < l)
      return arr[i];
    if (l <= hl && hr <= r)
      return arr[i].range_update(v), propagate(i), arr[i];
    return arr[i] = node(range_update(l, r, v, 2 * i + 1),
                         range_update(l, r, v, 2 * i + 2));
  }
  void propagate(int i) {
    if (arr[i].l < arr[i].r)
      arr[i].push(arr[2 * i + 1]), arr[i].push(arr[2 * i + 2]);
    arr[i].apply();
  }
};

int main() {
  cin.sync_with_stdio(false);
  int n, q, l, r, d;
  cin >> n >> q;
  vector<ll> jnk(n, 1);
  p10.resize(n + 1);
  p10[0] = 1;
  rep(i, 1, n + 1) p10[i] = (10 * p10[i - 1]) % mod;
  segment_tree st(jnk);
  rep(i, 0, q) {
    cin >> l >> r >> d;
    l--;
    r--;
    st.range_update(l, r, d);
    cout << st.query(0, n - 1).x << '\\n';
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
#define pb push_back
#define fs first
#define sc second
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;

const ll mod = 998244353;

ll egcd(ll a, ll b, ll &x, ll &y) {
  if (b == 0) {
    x = 1;
    y = 0;
    return a;
  }
  ll d = egcd(b, a % b, x, y);
  x -= a / b * y;
  swap(x, y);
  return d;
}

ll mod_inv(ll a, ll m) {
  ll x, y, d = egcd(a, m, x, y);
  return d == 1 ? (x % m + m) % m : -1;
}

ll mod_pow(ll b, ll e, ll m) {
  ll res = 1;
  while (e) {
    if (e & 1)
      res = (res * b % m + m) % m;
    b *= b;
    b %= m;
    e >>= 1;
  }
  return res;
}

struct node {
  int l, r;
  int x, lazy;
  node() {}
  node(int _l, int _r) : l(_l), r(_r), x(0), lazy(0) {}
  node(int _l, int _r, int _x) : node(_l, _r) { x = _x; }
  node(node a, node b) : node(a.l, b.r) {
    x = (mod_pow(10, b.r - b.l + 1, mod) * a.x + b.x) % mod;
  }
  void update(int v) { x = v; }
  void range_update(int v) { lazy = v; }
  void apply() {
    if (lazy != 0)
      x = ((lazy * (mod_pow(10, r - l + 1, mod) - 1)) % mod) * mod_inv(9, mod) %
          mod;
    lazy = 0;
  }
  void push(node &u) {
    if (lazy != 0)
      u.lazy = lazy;
  }
};

struct segment_tree {
  int n;
  vector<node> arr;
  segment_tree() {}
  segment_tree(const vector<ll> &a) : n(a.size()), arr(4 * n) {
    mk(a, 0, 0, n - 1);
  }
  node mk(const vector<ll> &a, int i, int l, int r) {
    int m = (l + r) / 2;
    return arr[i] = l > r    ? node(l, r)
                    : l == r ? node(l, r, a[l])
                             : node(mk(a, 2 * i + 1, l, m),
                                    mk(a, 2 * i + 2, m + 1, r));
  }
  node update(int at, ll v, int i = 0) {
    propagate(i);
    int hl = arr[i].l, hr = arr[i].r;
    if (at < hl || hr < at)
      return arr[i];
    if (hl == at && at == hr) {
      arr[i].update(v);
      return arr[i];
    }
    return arr[i] = node(update(at, v, 2 * i + 1), update(at, v, 2 * i + 2));
  }
  node query(int l, int r, int i = 0) {
    propagate(i);
    int hl = arr[i].l, hr = arr[i].r;
    if (r < hl || hr < l)
      return node(hl, hr);
    if (l <= hl && hr <= r)
      return arr[i];
    return node(query(l, r, 2 * i + 1), query(l, r, 2 * i + 2));
  }
  node range_update(int l, int r, ll v, int i = 0) {
    propagate(i);
    int hl = arr[i].l, hr = arr[i].r;
    if (r < hl || hr < l)
      return arr[i];
    if (l <= hl && hr <= r)
      return arr[i].range_update(v), propagate(i), arr[i];
    return arr[i] = node(range_update(l, r, v, 2 * i + 1),
                         range_update(l, r, v, 2 * i + 2));
  }
  void propagate(int i) {
    if (arr[i].l < arr[i].r)
      arr[i].push(arr[2 * i + 1]), arr[i].push(arr[2 * i + 2]);
    arr[i].apply();
  }
};

int main() {
  cin.sync_with_stdio(false);
  int n, q, l, r, d;
  cin >> n >> q;
  vector<ll> jnk(n, 1);
  segment_tree st(jnk);
  rep(i, 0, q) {
    cin >> l >> r >> d;
    l--;
    r--;
    st.range_update(l, r, d);
    cout << st.query(0, n - 1).x << '\\n';
  }
}