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

#include <iostream>
#define M 998244353
#define ll long long
#define MAX 2000005
using namespace std;

ll pre[MAX];
ll power(ll x, ll y) {
  if (y == 0)
    return 1ll;
  ll val = power(x, y / 2);
  if (y % 2 == 1)
    return ((val * val) % M * x) % M;
  return (val * val) % M;
}
void buildTree(ll *a, ll *tree, ll s, ll e, ll i) {
  if (s == e) {
    tree[i] = pre[e - s];
    return;
  }
  tree[i] = pre[e - s];
  ll m = (s + e) / 2;
  buildTree(a, tree, s, m, 2 * i);
  buildTree(a, tree, m + 1, e, 2 * i + 1);
}

void updateTreeLazy(ll *lazy, ll *tree, ll s, ll e, ll l, ll r, ll i, ll val) {
  if (lazy[i] != -1) {
    tree[i] = (lazy[i] * pre[e - s]) % M;
    if (s != e) {
      lazy[2 * i] = lazy[i];
      lazy[2 * i + 1] = lazy[i];
    }
    lazy[i] = -1;
  }
  if (s > e || s > r || e < l)
    return;
  // Complete Overlap
  if (s >= l && e <= r) {
    tree[i] = (val * pre[e - s]) % M;
    if (s != e) {
      lazy[2 * i] = val;
      lazy[2 * i + 1] = val;
    }
    return;
  }
  ll m = (s + e) / 2;
  updateTreeLazy(lazy, tree, s, m, l, r, 2 * i, val);
  updateTreeLazy(lazy, tree, m + 1, e, l, r, 2 * i + 1, val);
  tree[i] = (tree[2 * i] * power(10, e - m)) % M + tree[2 * i + 1] % M;
  tree[i] %= M;
}
int main() {
  ll n, q;
  cin >> n >> q;
  ll arr[n];
  for (ll i = 0; i < n; i++) {
    arr[i] = 1;
  }
  pre[0] = 1;
  for (ll i = 1; i < MAX; i++) {
    pre[i] = ((pre[i - 1] * 10) % M + 1) % M;
  }
  ll *tree = new ll[4 * n]();
  ll *lazy = new ll[4 * n];
  for (ll i = 0; i < 4 * n; i++)
    lazy[i] = -1;
  buildTree(arr, tree, 0, n - 1, 1);
  for (ll i = 0; i < q; i++) {
    ll l, r, x;
    cin >> l >> r >> x;
    updateTreeLazy(lazy, tree, 0, n - 1, l - 1, r - 1, 1, x);
    cout << tree[1] % M << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#define M 998244353
#define ll long long
using namespace std;

ll power(ll x, ll y) {
  if (y == 0)
    return 1ll;
  ll val = power(x, y / 2);
  if (y % 2 == 1)
    return (val * val * x) % M;
  return (val * val) % M;
}
void buildTree(ll *a, ll *tree, ll s, ll e, ll i) {
  if (s == e) {
    tree[i] = a[s];
    return;
  }
  ll m = (s + e) / 2;
  buildTree(a, tree, s, m, 2 * i);
  buildTree(a, tree, m + 1, e, 2 * i + 1);
  tree[i] = (tree[2 * i] * power(10, e - m)) % M + tree[2 * i + 1] % M;
  tree[i] %= M;
}

void updateTreeLazy(ll *lazy, ll *tree, ll s, ll e, ll l, ll r, ll i, ll val) {
  if (lazy[i] != -1) {
    tree[i] = lazy[i];
    if (s != e) {
      lazy[2 * i] = lazy[i];
      lazy[2 * i + 1] = lazy[i];
    }
    lazy[i] = -1;
  }
  if (s > e || s > r || e < l)
    return;
  // Complete Overlap
  if (s >= l && e <= r) {
    tree[i] = val;
    if (s != e) {
      lazy[2 * i] = val;
      lazy[2 * i + 1] = val;
    }
    return;
  }
  ll m = (s + e) / 2;
  updateTreeLazy(lazy, tree, m + 1, e, l, r, 2 * i + 1, val);
  updateTreeLazy(lazy, tree, s, m, l, r, 2 * i, val);
  tree[i] = (tree[2 * i] * power(10, e - m)) % M + tree[2 * i + 1] % M;
  tree[i] %= M;
}
ll query(ll *lazy, ll *tree, ll s, ll e, ll i) {
  if (lazy[i] != -1) {
    tree[i] = lazy[i];
    if (s != e) {
      lazy[2 * i] = lazy[i];
      lazy[2 * i + 1] = lazy[i];
    }
    lazy[i] = -1;
  }
  if (s == e)
    return tree[i];
  ll m = (s + e) / 2;
  return ((query(lazy, tree, s, m, 2 * i) * power(10, e - m)) % M +
          query(lazy, tree, m + 1, e, 2 * i + 1)) %
         M;
}
int main() {
  ll n, q;
  cin >> n >> q;
  ll arr[n];
  for (ll i = 0; i < n; i++) {
    arr[i] = 1;
  }
  ll *tree = new ll[3 * n]();
  ll *lazy = new ll[3 * n];
  for (ll i = 0; i < 3 * n; i++)
    lazy[i] = -1;
  buildTree(arr, tree, 0, n - 1, 1);
  for (ll i = 0; i < q; i++) {
    ll l, r, x;
    cin >> l >> r >> x;
    updateTreeLazy(lazy, tree, 0, n - 1, l - 1, r - 1, 1, x);
    cout << query(lazy, tree, 0, n - 1, 1) << endl;
  }
  return 0;
}