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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define N 200005
vector<int> marked(4 * N, -1);
int t[4 * N];
int pws[N + 5];
#define mod 998244353
int n;
int mypower(int a, int b) {
  int ans = 1;
  while (b) {
    if (b % 2)
      ans = ans * a % mod;
    b = b / 2;
    a = a * a % mod;
  }
  return ans;
}
int inv9 = mypower(9, mod - 2);
int get_sum(int l, int r) {
  int ans = pws[n - l + 1] - pws[n - r];
  ans = ans * inv9 % mod;
  return ans;
}
void push(int v, int l, int r) {
  if (marked[v] != -1) {
    t[v] = get_sum(l, r) * marked[v] % mod;
    if (l != r)
      marked[2 * v] = marked[2 * v + 1] = marked[v];
    marked[v] = -1;
  }
}

void update(int v, int tl, int tr, int l, int r, int new_val) {
  push(v, tl, tr);
  if (l == tl && tr == r) {
    t[v] = get_sum(tl, tr) * new_val % mod;
    if (tl != tr)
      marked[2 * v] = marked[2 * v + 1] = new_val;
  } else {
    int tm = (tl + tr) / 2;
    if (r <= tm)
      update(2 * v, tl, tm, l, r, new_val), push(2 * v + 1, tm + 1, tr);
    else if (l > tm)
      update(2 * v + 1, tm + 1, tr, l, r, new_val), push(2 * v, tl, tm);
    else
      update(2 * v, tl, tm, l, tm, new_val),
          update(2 * v + 1, tm + 1, tr, tm + 1, r, new_val);
    t[v] = (t[2 * v] + t[2 * v + 1]) % mod;
  }
}

// int get(int v, int tl, int tr, int pos) {
//     if (tl == tr) {
//         return t[v];
//     }
//     push(v);
//     int tm = (tl + tr) / 2;
//     if (pos <= tm)
//         return get(v*2, tl, tm, pos);
//     else
//         return get(v*2+1, tm+1, tr, pos);
// }

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int q;
  cin >> n >> q;
  pws[0] = 1;
  for (int i = 1; i < N + 5; i++)
    pws[i] = (10 * pws[i - 1]) % mod;
  for (int i = 1; i <= n; i++) {
    update(1, 1, n, i, i, 1);
  }
  while (q--) {
    int l, r, d;
    cin >> l >> r >> d;
    update(1, 1, n, l, r, d);
    cout << (t[1] + mod) % mod << '\\n';
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define N 200005
vector<int> marked(4 * N, -1);
int t[4 * N];
#define mod 998244353
int n;
int mypower(int a, int b) {
  int ans = 1;
  while (b) {
    if (b % 2)
      ans = ans * a % mod;
    b = b / 2;
    a = a * a % mod;
  }
  return ans;
}
int inv9 = mypower(9, mod - 2);
int get_sum(int l, int r) {
  int ans = mypower(10, n - l + 1) - mypower(10, n - r);
  ans = ans * inv9 % mod;
  ans = (ans + mod) % mod;
  return ans;
}
void push(int v, int l, int r) {
  if (marked[v] != -1) {
    t[v] = get_sum(l, r) * marked[v] % mod;
    if (l != r)
      marked[2 * v] = marked[2 * v + 1] = marked[v];
    marked[v] = -1;
  }
}

void update(int v, int tl, int tr, int l, int r, int new_val) {
  push(v, tl, tr);
  if (l == tl && tr == r) {
    t[v] = get_sum(tl, tr) * new_val % mod;
    if (tl != tr)
      marked[2 * v] = marked[2 * v + 1] = new_val;
  } else {
    int tm = (tl + tr) / 2;
    if (r <= tm)
      update(2 * v, tl, tm, l, r, new_val), push(2 * v + 1, tm + 1, tr);
    else if (l > tm)
      update(2 * v + 1, tm + 1, tr, l, r, new_val), push(2 * v, tl, tm);
    else
      update(2 * v, tl, tm, l, tm, new_val),
          update(2 * v + 1, tm + 1, tr, tm + 1, r, new_val);
    t[v] = (t[2 * v] + t[2 * v + 1]) % mod;
  }
}

// int get(int v, int tl, int tr, int pos) {
//     if (tl == tr) {
//         return t[v];
//     }
//     push(v);
//     int tm = (tl + tr) / 2;
//     if (pos <= tm)
//         return get(v*2, tl, tm, pos);
//     else
//         return get(v*2+1, tm+1, tr, pos);
// }

signed main() {
  int q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    update(1, 1, n, i, i, 1);
  }
  while (q--) {
    int l, r, d;
    cin >> l >> r >> d;
    update(1, 1, n, l, r, d);
    cout << (t[1] + mod) % mod << '\\n';
  }
}