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

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

const int MOD = 998244353;

vector<int> ten;
int nine_inverse = 443664157;

int power(int a, int b) {
  if (b == 0)
    return 1;
  int result = power(a, b / 2);
  return (ll)result * result % MOD * (b % 2 == 1 ? a : 1) % MOD;
}

int f(int d, int length) {
  return (ll)(ten[length] - 1) * nine_inverse % MOD * d % MOD;
}

class segment {
public:
  int n;
  vector<int> tree, lazy;

  segment(int n) {
    this->n = n;
    tree.resize(4 * n);
    lazy.resize(4 * n, -1);
  }

  void propagate(int v, int l, int r) {
    if (lazy[v] == -1)
      return;

    int d = lazy[v];
    int mid = (l + r) / 2;
    tree[2 * v] = f(d, mid - l);
    tree[2 * v + 1] = f(d, r - mid);
    lazy[2 * v] = d;
    lazy[2 * v + 1] = d;

    lazy[v] = -1;
  }

  void update(int p, int q, int d, int l, int r, int v) {
    if (q <= l || p >= r)
      return;
    if (p <= l && r <= q) {
      int length = r - l;
      tree[v] = f(d, length);
      lazy[v] = d;
      return;
    }

    propagate(v, l, r);

    int mid = (l + r) / 2;
    update(p, q, d, l, mid, 2 * v);
    update(p, q, d, mid, r, 2 * v + 1);
    tree[v] = ((ll)ten[r - mid] * tree[2 * v] % MOD + tree[2 * v + 1]) % MOD;
  }

  void update(int p, int q, int d) { update(p, q, d, 0, n, 1); }
};

int main() {
  int n, q;
  cin >> n >> q;

  ten.resize(n + 1);
  ten[0] = 1;
  for (int i = 1; i <= n; i++) {
    ten[i] = (ll)10 * ten[i - 1] % MOD;
  }

  segment tree(n);
  tree.update(0, n, 1);
  for (int i = 0; i < q; i++) {
    int l, r, d;
    cin >> l >> r >> d;
    l--;
    tree.update(l, r, d);
    cout << tree.tree[1] << '\\n';
  }
  return 0;
}

Error: TLE

Fixed submission:

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

const int MOD = 998244353;

int power(int a, int b) {
  if (b == 0)
    return 1;
  int result = power(a, b / 2);
  return (ll)result * result % MOD * (b % 2 == 1 ? a : 1) % MOD;
}

int f(int d, int length) {
  return (ll)(power(10, length) - 1) * power(9, MOD - 2) % MOD * d % MOD;
}

class segment {
public:
  int n;
  vector<int> tree, lazy;

  segment(int n) {
    this->n = n;
    tree.resize(4 * n);
    lazy.resize(4 * n, -1);
  }

  void propagate(int v, int l, int r) {
    if (lazy[v] == -1)
      return;

    int d = lazy[v];
    int mid = (l + r) / 2;
    tree[2 * v] = f(d, mid - l);
    tree[2 * v + 1] = f(d, r - mid);
    lazy[2 * v] = d;
    lazy[2 * v + 1] = d;

    lazy[v] = -1;
  }

  void update(int p, int q, int d, int l, int r, int v) {
    if (q <= l || p >= r)
      return;
    if (p <= l && r <= q) {
      int length = r - l;
      tree[v] = f(d, length);
      lazy[v] = d;
      return;
    }

    propagate(v, l, r);

    int mid = (l + r) / 2;
    update(p, q, d, l, mid, 2 * v);
    update(p, q, d, mid, r, 2 * v + 1);
    tree[v] =
        ((ll)power(10, r - mid) * tree[2 * v] % MOD + tree[2 * v + 1]) % MOD;
  }

  void update(int p, int q, int d) { update(p, q, d, 0, n, 1); }
};

int main() {
  int n, q;
  cin >> n >> q;
  segment tree(n);
  tree.update(0, n, 1);
  for (int i = 0; i < q; i++) {
    int l, r, d;
    cin >> l >> r >> d;
    l--;
    tree.update(l, r, d);
    cout << tree.tree[1] << '\\n';
  }
  return 0;
}