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

#pragma GCC optimize(2)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define read(a) scanf("%d", &a)
#define readf(a) scanf("%lf", &a)
#define pb push_back
#define mk make_pair
#define _rand mt19937 RAND(time(0))
#define mem(a) memset(a, 0, sizeof(a))
#define Buff ios::sync_with_stdio(false)
typedef long long ll;
using namespace std;
const int INF = 1e9 + 7;
const int N = 2e5 + 7;
const int M = 1e6 + 7;
// const ll mod = 998224353;
const ll mod = 998244353;

ll sum[N << 2], power[N << 2], pow10[N];
int color[N << 2];

#define lson rt << 1
#define rson rt << 1 | 1
#define ls lson, l, mid
#define rs rson, mid + 1, r

void pushUp(int rt) {
  sum[rt] = (sum[lson] + sum[rson]) % mod;
  if (color[lson] != color[rson])
    color[rt] = -1;
  else
    color[rt] = color[lson];
}

void build(int rt, int l, int r) {
  if (l == r) {
    color[rt] = 1;
    sum[rt] = 1;
    power[rt] = pow10[l];
    return;
  }
  int mid = l + r >> 1;
  build(ls);
  build(rs);
  pushUp(rt);
  power[rt] = (power[lson] + power[rson]) % mod;
}

void pushDown(int rt) {
  if (color[rt] == -1)
    return;
  color[lson] = color[rson] = color[rt];
  sum[lson] = power[lson] * (ll)color[rt] % mod;
  sum[rson] = power[rson] * (ll)color[rt] % mod;
  color[rt] = -1;
}

void update(int rt, int l, int r, int L, int R, int x) {
  if (L <= l && r <= R) {
    //        printf("power[%d] = %lld\\n", rt, power[rt]);
    sum[rt] = power[rt] * (ll)x % mod;
    color[rt] = x;
    return;
  }
  pushDown(rt);
  int mid = l + r >> 1;
  if (L <= mid)
    update(ls, L, R, x);
  if (R > mid)
    update(rs, L, R, x);
  pushUp(rt);
}
signed main() {
  //    freopen("rand.txt", "r", stdin);
  //    freopen("my.txt", "w", stdout);
  Buff;
  int n, q;
  cin >> n >> q;
  pow10[n] = 1ll;
  for (int i = n - 1; i; i--)
    pow10[i] = pow10[i + 1] * 10ll % mod;
  build(1, 1, n);
  int Case = 0;
  while (q--) {
    int l, r, x;
    cin >> l >> r >> x;
    update(1, 1, n, l, r, x);
    //        cout << "Case " << ++Case << ": ";
    cout << sum[1] << "\\n";
  }
  return 0;
}
/**
 **/


Error: TLE

Fixed submission:

#pragma GCC optimize(2)
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define read(a) scanf("%d", &a)
#define readf(a) scanf("%lf", &a)
#define readl(a) scanf("%lld", &a)
#define pb push_back
#define mk make_pair
#define _rand mt19937 RAND(time(0))
#define mem(a) memset(a, 0, sizeof(a))
#define Buff ios::sync_with_stdio(false)
typedef long long ll;
using namespace std;
const int INF = 1e9 + 7;
const int N = 2e5 + 7;
const int M = 1e6 + 7;
const ll mod = 998244353;
int n, q, block, num;
ll fact[N], infact[N];
int L[N], R[N], pos[N];
ll sum[N], add[N], add1[N], a[N];
ll ans = 0;
void build() {
  block = sqrt(n);
  num = block;
  for (int i = 1; i <= num; i++) {
    L[i] = (i - 1) * block + 1;
    R[i] = i * block;
  }
  R[num] = n;
  for (int i = 1; i <= num; i++) {
    for (int j = L[i]; j <= R[i]; j++) {
      a[j] = 1;
      pos[j] = i;
      add[j] = 0;
      sum[i] = (sum[i] + fact[j]) % mod;
    }
    ans = (ans + sum[i]) % mod;
  }
}
void change(int l, int r, ll k) {
  int p = pos[l];
  int q = pos[r];
  if (p == q) {
    for (int i = l; i <= r; i++) {
      if (add[p] != add1[i]) {
        a[i] = add[p];
        add1[i] = add[p];
      }
      ll d = k - a[i];
      ans = ans - sum[p];
      ans = (ans % mod + mod) % mod;
      sum[p] = sum[p] + (d * fact[i]);
      sum[p] = (sum[p] % mod + mod) % mod;
      a[i] = k;
      ans = ans + sum[p];
      ans = (ans % mod + mod) % mod;
    }
  } else {
    for (int i = l; i <= R[p]; i++) {
      if (add[p] != add1[i]) {
        a[i] = add[p];
        add1[i] = add[p];
      }
      ll d = k - a[i];
      ans = ans - sum[p];
      ans = (ans % mod + mod) % mod;
      sum[p] = sum[p] + (d * fact[i]);
      sum[p] = (sum[p] % mod + mod) % mod;
      a[i] = k;
      ans = ans + sum[p];
      ans = (ans % mod + mod) % mod;
    }
    for (int i = p + 1; i <= q - 1; i++) {
      add[i] = k;
      ans -= sum[i];
      cout << sum[i] << endl;
      ans = (ans % mod + mod) % mod;
      sum[i] = ((infact[R[i]] - infact[L[i] - 1]) % mod + mod) % mod * k;
      sum[i] = (sum[i] % mod + mod) % mod;
      cout << sum[i] << endl;
      ans += sum[i];
      ans = (ans % mod + mod) % mod;
    }
    for (int i = L[q]; i <= r; i++) {
      if (add[p] != add1[i]) {
        a[i] = add[p];
        add1[i] = add[p];
      }
      ll d = k - a[i];
      ans = ans - sum[q];
      ans = (ans % mod + mod) % mod;
      sum[q] = sum[q] + (d * fact[i]);
      sum[q] = (sum[q] % mod + mod) % mod;
      a[i] = k;
      ans = ans + sum[q];
      ans = (ans % mod + mod) % mod;
    }
  }
}
signed main() {
  read(n);
  read(q);
  fact[n] = 1;
  for (int i = n - 1; i >= 1; i--)
    fact[i] = fact[i + 1] * 10ll % mod;
  for (int i = 2; i <= n; i++)
    infact[i] = (((infact[i] + infact[i - 1]) % mod) + fact[i]) % mod;
  build();
  while (q--) {
    int l, r;
    ll d;
    read(l);
    read(r);
    readl(d);
    change(l, r, d);
    printf("%lld\\n", ans);
  }
  return 0;
}
/**
 **/