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

#include <iostream>
using namespace std;
const int maxn = 2e5 + 10;
const long long mod = 998244353L;
long long t[maxn << 2], laz[maxn << 2], n, q, sum[maxn];
long long qpow(long long base, long long k) {
  long long res = 1;
  while (k) {
    if (k & 1) {
      res = res * base % mod;
    }
    k /= 2;
    base = base * base % mod;
  }
  return res % mod;
}
void make(int l, int r, int num) {
  if (l == r) {
    t[num] = qpow(10, n - l);
    laz[num] = 0;
    return;
  } else {
    int mid = (l + r) >> 1;
    make(l, mid, num << 1);
    make(mid + 1, r, num << 1 | 1);
    t[num] = (t[num << 1] + t[num << 1 | 1]) % mod;
  }
}
void update(int x, int y, int nv, int l, int r, int num) {
  int mid = (l + r) / 2;
  if (x <= l && y >= r) {
    laz[num] = nv;
    t[num] = (sum[n - l] - sum[n - r - 1] + 10 * mod) % mod * nv % mod;
    return;
  }
  if (laz[num]) {
    laz[num << 1] = laz[num << 1 | 1] = laz[num];
    t[num * 2] = laz[num] * (sum[n - l] - sum[n - mid - 1] + 10 * mod) % mod;
    t[num * 2 + 1] =
        laz[num] * (sum[n - mid - 1] - sum[n - r - 1] + 10 * mod) % mod;
    laz[num] = 0;
  }
  if (y <= mid)
    update(x, y, nv, l, mid, num * 2); /*注意找儿子的时候不能改动区间*/
  else if (x > mid)
    update(x, y, nv, mid + 1, r, num * 2 + 1);
  else {
    update(x, y, nv, l, mid, num * 2);
    update(x, y, nv, mid + 1, r, num * 2 + 1);
  }
  t[num] = (t[num << 1] + t[num << 1 | 1]) % mod;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> q;
  long long x = 1;
  sum[0] = 1;
  for (int i = 1; i <= n; i++) {
    x = x * 10 % mod;
    sum[i] = (sum[i - 1] + x) % mod;
  }
  make(1, n, 1);
  for (int i = 1, x, y, w; i <= q; i++) {
    cin >> x >> y >> w;
    update(x, y, w, 1, n, 1);
    cout << t[1] << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
const int maxn = 5e5 + 10;
const long long mod = 998244353L;
long long t[maxn << 2], ans;
int n, q;
long long qpow(long long base, long long k) {
  long long res = 1;
  while (k) {
    if (k & 1)
      res = res * base % mod;
    base = base * base % mod;
    k /= 2;
  }
  return res % mod;
}
void make(int l, int r, int num) {
  if (l == r) {
    t[num] = qpow(10, n - l);
    return;
  }
  int mid = (l + r) >> 1;
  make(l, mid, num << 1);
  make(mid + 1, r, num << 1 | 1);
  t[num] = (t[num << 1] + t[num << 1 | 1]) % mod;
}
long long query(int x, int y, int l, int r, int num) {
  if (l > y || r < x)
    return (long long)0;
  if (l >= x && r <= y)
    return t[num] % mod;
  int mid = (l + r) >> 1;
  return (query(x, y, l, mid, num << 1) +
          query(x, y, mid + 1, r, num << 1 | 1)) %
         mod;
}
void update(int x, int y, int nv, int l, int r, int num) {
  if (l > y || r < x)
    return;
  if (l == r) {
    t[num] = nv * qpow(10, n - l) % mod;
    return;
  }
  int mid = (l + r) >> 1;
  update(x, y, nv, l, mid, num << 1);
  update(x, y, nv, mid + 1, r, num << 1 | 1);
  t[num] = (t[num << 1] + t[num << 1 | 1]) % mod;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> q;
  make(1, n, 1);
  for (int i = 1, l, r, x; i <= q; i++) {
    cin >> l >> r >> x;
    update(l, r, x, 1, n, 1);
    cout << t[1] << endl;
  }
}