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

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

const int sz = 262144;
long long seg[sz * 2];
int lazy[sz * 2];
const int mod = 998244353;
int n;
long long inv;
long long fp[500001];

long long fastpow(long long a, long long b) {
  if (b == 0) {
    return 1;
  }
  if (b % 2 == 1) {
    return (fastpow(a, b - 1) * a) % mod;
  }
  long long half = fastpow(a, b / 2);
  return (half * half) % mod;
}

void propagate(int node, int nodel, int noder) {
  if (lazy[node] != 0) {
    long long val = (fp[noder - nodel + 1] + mod - 1) * inv;
    val %= mod;
    val *= fp[n - 1 - noder];
    val %= mod;
    val *= lazy[node];
    val %= mod;
    seg[node] = val;
    if (node < sz) {
      lazy[node * 2] = lazy[node];
      lazy[node * 2 + 1] = lazy[node];
    }
    lazy[node] = 0;
  }
}

long long sum(int l, int r, int node = 1, int nodel = 0, int noder = sz - 1) {
  propagate(node, nodel, noder);
  if (r < nodel || l > noder) {
    return 0;
  }
  if (l <= nodel && noder <= r) {
    return seg[node];
  }
  int mid = (nodel + noder) / 2;
  return (sum(l, r, node * 2, nodel, mid) +
          sum(l, r, node * 2 + 1, mid + 1, noder)) %
         mod;
}

void update(int l, int r, int val, int node = 1, int nodel = 0,
            int noder = sz - 1) {
  propagate(node, nodel, noder);
  if (r < nodel || l > noder) {
    return;
  }
  if (l <= nodel && noder <= r) {
    lazy[node] = val;
    propagate(node, nodel, noder);
    return;
  }
  int mid = (nodel + noder) / 2;
  update(l, r, val, node * 2, nodel, mid);
  update(l, r, val, node * 2 + 1, mid + 1, noder);
  seg[node] = (seg[node * 2] + seg[node * 2 + 1]) % mod;
}

int main(void) {
  fp[0] = 1;
  for (int i = 1; i <= 500000; i++) {
    fp[i] = (fp[i - 1] * 10) % mod;
  }
  inv = fastpow(9, mod - 2);
  int q;
  scanf("%d %d", &n, &q);
  update(0, n - 1, 1);
  for (int i = 0; i < q; i++) {
    int l, r, d;
    scanf("%d %d %d", &l, &r, &d);
    l--;
    r--;
    update(l, r, d);
    printf("%lld\\n", sum(0, sz - 1));
  }
}

Error: TLE

Fixed submission:

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

const int sz = 262144;
long long seg[sz * 2];
int lazy[sz * 2];
const int mod = 998244353;
int n;

long long fastpow(long long a, long long b) {
  if (b == 0) {
    return 1;
  }
  if (b % 2 == 1) {
    return (fastpow(a, b - 1) * a) % mod;
  }
  long long half = fastpow(a, b / 2);
  return (half * half) % mod;
}

void propagate(int node, int nodel, int noder) {
  if (lazy[node] != 0) {
    long long val =
        (fastpow(10, noder - nodel + 1) + mod - 1) * fastpow(9, mod - 2);
    val %= mod;
    val *= fastpow(10, n - 1 - noder);
    val %= mod;
    val *= lazy[node];
    val %= mod;
    seg[node] = val;
    if (node < sz) {
      lazy[node * 2] = lazy[node];
      lazy[node * 2 + 1] = lazy[node];
    }
    lazy[node] = 0;
  }
}

long long sum(int l, int r, int node = 1, int nodel = 0, int noder = sz - 1) {
  propagate(node, nodel, noder);
  if (r < nodel || l > noder) {
    return 0;
  }
  if (l <= nodel && noder <= r) {
    return seg[node];
  }
  int mid = (nodel + noder) / 2;
  return (sum(l, r, node * 2, nodel, mid) +
          sum(l, r, node * 2 + 1, mid + 1, noder)) %
         mod;
}

void update(int l, int r, int val, int node = 1, int nodel = 0,
            int noder = sz - 1) {
  propagate(node, nodel, noder);
  if (r < nodel || l > noder) {
    return;
  }
  if (l <= nodel && noder <= r) {
    lazy[node] = val;
    propagate(node, nodel, noder);
    return;
  }
  int mid = (nodel + noder) / 2;
  update(l, r, val, node * 2, nodel, mid);
  update(l, r, val, node * 2 + 1, mid + 1, noder);
  seg[node] = (seg[node * 2] + seg[node * 2 + 1]) % mod;
}

int main(void) {
  int q;
  scanf("%d %d", &n, &q);
  update(0, n - 1, 1);
  for (int i = 0; i < q; i++) {
    int l, r, d;
    scanf("%d %d %d", &l, &r, &d);
    l--;
    r--;
    update(l, r, d);
    printf("%lld\\n", sum(0, sz - 1));
  }
}