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

#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 998244353;

int n, q, l, r, k;
int tag[2000005], num[200005], all_1[200005];

struct segment_tree {
  int l, r;
  int num;
} tree[2000005];

inline void pushup(int rt) // 5 5 6 6
{
  tree[rt].num = (tree[2 * rt].num * num[tree[2 * rt + 1].r - tree[2 * rt].r] +
                  tree[2 * rt + 1].num) %
                 mod;
}

inline void get_lr(int l, int r, int rt) {
  tree[rt].l = l, tree[rt].r = r;
  if (l == r)
    return;
  int mid = (l + r) >> 1;
  get_lr(l, mid, 2 * rt);
  get_lr(mid + 1, r, 2 * rt + 1);
}

inline void build_tree(int l, int r, int rt) {
  if (l == r) {
    tree[rt].num = 1;
    return;
  }
  int mid = (l + r) >> 1;
  build_tree(l, mid, 2 * rt);
  build_tree(mid + 1, r, 2 * rt + 1);
  pushup(rt);
}

inline void f(int l, int r, int rt, int k) // 3 4 5 3
{
  tag[rt] = k;
  tree[rt].num = (k * all_1[tree[rt].r - tree[rt].l + 1]) % mod;
}

inline void pushdown(int rt, int l, int r) {
  int mid = (l + r) >> 1;
  if (tag[rt] != 0) {
    f(l, mid, 2 * rt, tag[rt]);
    f(mid + 1, r, 2 * rt + 1, tag[rt]);
  }
  tag[rt] = 0;
}

inline void change(int nl, int nr, int l, int r, int rt, int k) {
  pushdown(rt, l, r);

  int mid = (l + r) >> 1;
  if (nl <= l && r <= nr) {
    f(l, r, rt, k);
    return;
  }
  if (nl <= mid)
    change(nl, nr, l, mid, 2 * rt, k);
  if (nr > mid)
    change(nl, nr, mid + 1, r, 2 * rt + 1, k);

  pushup(rt);
}

inline int query(int nl, int nr, int l, int r, int rt) {
  pushdown(rt, l, r);

  int mid = (l + r) >> 1, lson = 0, rson = 0, tot = 0;
  if (nl <= l && r <= nr)
    return tree[rt].num;

  if (nl <= mid)
    lson = query(nl, nr, l, mid, 2 * rt);
  if (nr > mid)
    rson = query(nl, nr, mid + 1, r, 2 * rt + 1);
  tot = (lson * (n - tree[2 * rt].r) + rson) % mod;

  return tot;
}

inline int read() {
  int s = 0, w = 1;
  char ch = getchar();

  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -w;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    s = (s << 1) + (s << 3) + (ch ^ '0');
    ch = getchar();
  }
  return s * w;
}

signed main() {
  n = read(), q = read();
  num[0] = 1;
  for (int i = 1; i <= n; i++)
    num[i] = (num[i - 1] * 10) % mod;

  all_1[1] = 1;
  for (int i = 2; i <= n; i++)
    all_1[i] = (all_1[i - 1] * 10 + 1) % mod;

  get_lr(1, n, 1);
  build_tree(1, n, 1);
  while (q--) {
    l = read(), r = read(), k = read();
    change(l, r, 1, n, 1, k);
    cout << query(1, n, 1, n, 1) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define rg register
#pragma GCC optimize(2)
using namespace std;
const int mod = 998244353;

int n, q, l, r, k;
int tag[2000005], num[200005], all_1[200005];

struct segment_tree {
  int l, r;
  int num;
} tree[2000005];

inline void pushup(int rt) // 5 5 6 6
{
  tree[rt].num = (tree[2 * rt].num * num[tree[2 * rt + 1].r - tree[2 * rt].r] +
                  tree[2 * rt + 1].num) %
                 mod;
}

inline void get_lr(int l, int r, int rt) {
  tree[rt].l = l, tree[rt].r = r;
  if (l == r)
    return;
  int mid = (l + r) >> 1;
  get_lr(l, mid, 2 * rt);
  get_lr(mid + 1, r, 2 * rt + 1);
}

inline void build_tree(rg int l, rg int r, rg int rt) {
  if (l == r) {
    tree[rt].num = 1;
    return;
  }
  int mid = (l + r) >> 1;
  build_tree(l, mid, 2 * rt);
  build_tree(mid + 1, r, 2 * rt + 1);
  pushup(rt);
}

inline void f(rg int l, rg int r, rg int rt, rg int k) // 3 4 5 3
{
  tag[rt] = k;
  tree[rt].num = k * all_1[r - l + 1];
}

inline void pushdown(rg int rt, rg int l, rg int r) {
  int mid = (l + r) >> 1;
  if (tag[rt] != 0) {
    f(l, mid, 2 * rt, k);
    f(mid + 1, r, 2 * rt + 1, k);
  }
  tag[rt] = 0;
}

inline void change(rg int nl, rg int nr, rg int l, rg int r, rg int rt,
                   rg int k) {
  pushdown(rt, l, r);

  int mid = (l + r) >> 1;
  if (nl <= l && r <= mid) {
    f(l, r, rt, k);
    return;
  }
  if (nl <= mid)
    change(nl, nr, l, mid, 2 * rt, k);
  if (nr > mid)
    change(nl, nr, mid + 1, r, 2 * rt + 1, k);

  pushup(rt);
}

inline int query(rg int nl, rg int nr, rg int l, rg int r, rg int rt) {
  pushdown(rt, l, r);

  int mid = (l + r) >> 1, lson = 0, rson = 0, tot = 0;
  if (nl <= l && r <= nr)
    return tree[rt].num;
  if (nl <= mid)
    lson = query(nl, nr, l, mid, 2 * rt);
  if (nr > mid)
    rson = query(nl, nr, mid + 1, r, 2 * rt + 1);

  tot = (lson * (n - tree[2 * rt].r) + rson) % mod;

  return tot;
}

inline int read() {
  int s = 0, w = 1;
  char ch = getchar();

  while (ch < '0' || ch > '9') {
    if (ch == '-')
      w = -w;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    s = (s << 1) + (s << 3) + (ch ^ '0');
    ch = getchar();
  }
  return s * w;
}

signed main() {
  n = read(), q = read();
  num[0] = 1;
  for (int i = 1; i <= n; i++)
    num[i] = (num[i - 1] * 10) % mod;

  all_1[1] = 1;
  for (int i = 2; i <= n; i++)
    all_1[i] = (all_1[i - 1] * 10 + 1) % mod;

  get_lr(1, n, 1);
  build_tree(1, n, 1);

  while (q--) {
    l = read(), r = read(), k = read();
    change(l, r, 1, n, 1, k);
    printf("%lld\\n", tree[1].num);
  }
  return 0;
}