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

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

LL tag[800001], num[800001], qaq[200010], qp[200010];
LL QAQ = 0;

LL qpow(LL a, int b, LL p) { return qp[b]; }

LL lc(LL p) { return p << 1; }

LL rc(LL p) { return p << 1 | 1; }

void pushup(LL p, int l, int r) {
  int mid = (l + r) >> 1;
  num[p] = num[lc(p)] * qpow(10, (r - mid), 998244353) + num[rc(p)];
  num[p] %= 998244353;
}

void build(LL p, LL l, LL r) {
  if (l == r) {
    num[p] = 1;
    return;
  }
  LL mid = (l + r) >> 1;
  build(lc(p), l, mid);
  build(rc(p), mid + 1, r);
  pushup(p, l, r);
}

void upd(LL p, LL l, LL r, LL x) {
  tag[p] = x;
  num[p] = qaq[r - l + 1] * x % 998244353;
}

void pushdown(LL p, LL l, LL r) {
  LL mid = (l + r) >> 1;
  upd(lc(p), l, mid, tag[p]);
  upd(rc(p), mid + 1, r, tag[p]);
  tag[p] = 0;
}

void change(LL L, LL R, LL l, LL r, LL p, LL x) {
  if (L <= l && r <= R) {
    tag[p] = x;
    num[p] = qaq[r - l + 1] * x % 998244353;
    return;
  }
  if (tag[p])
    pushdown(p, l, r);
  LL mid = (l + r) >> 1;
  if (L <= mid)
    change(L, R, l, mid, lc(p), x);
  if (R >= mid + 1)
    change(L, R, mid + 1, r, rc(p), x);
  pushup(p, l, r);
}

void Q(LL L, LL R, LL l, LL r, LL p) {
  if (L <= l && r <= R) {
    QAQ *= qpow(10, r - l + 1, 998244353);
    QAQ %= 998244353;
    QAQ += num[p];
    return;
  }
  if (tag[p])
    pushdown(p, l, r);
  LL mid = (l + r) >> 1;
  if (L <= mid) {
    Q(L, R, l, mid, lc(p));
  }
  if (R >= mid + 1)
    Q(L, R, mid + 1, r, rc(p));
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  LL n, m;
  cin >> n >> m;
  qp[0] = 1;
  for (int i = 1; i <= n; i++) {
    qp[i] = qp[i - 1] * 10;
    qp[i] %= 998244353;
  }
  build(1, 1, n);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    qaq[i] = qaq[i - 1] + qpow(10, i - 1, 998244353);
    qaq[i] %= 998244353;
  }
  ans = qaq[n];
  for (LL i = 1; i <= m; i++) {
    QAQ = 0;
    LL l, r, x;
    cin >> l >> r >> x;
    Q(l, r, 1, n, 1);
    LL awa = QAQ;
    //		cout << awa << endl;
    ans -= awa * qpow(10, n - r, 998244353) % 998244353;
    while (ans < 0)
      ans += 998244353;
    QAQ = 0;
    ans %= 998244353;
    change(l, r, 1, n, 1, x);
    awa = qaq[r - l + 1] * x % 998244353;
    ans += awa * qpow(10, n - r, 998244353);
    ans %= 998244353;
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

LL tag[3200001], num[3200001], qaq[800010], qp[800010];
LL QAQ = 0;

LL qpow(LL a, int b, LL p) { return qp[b]; }

LL lc(LL p) { return p << 1; }

LL rc(LL p) { return p << 1 | 1; }

void pushup(LL p, int l, int r) {
  int mid = (l + r) >> 1;
  num[p] = num[lc(p)] * qpow(10, (r - mid), 998244353) + num[rc(p)];
  num[p] %= 998244353;
}

void build(LL p, LL l, LL r) {
  if (l == r) {
    num[p] = 1;
    return;
  }
  LL mid = (l + r) >> 1;
  build(lc(p), l, mid);
  build(rc(p), mid + 1, r);
  pushup(p, l, r);
}

void upd(LL p, LL l, LL r, LL x) {
  tag[p] = x;
  num[p] = qaq[r - l + 1] * x % 998244353;
}

void pushdown(LL p, LL l, LL r) {
  LL mid = (l + r) >> 1;
  upd(lc(p), l, mid, tag[p]);
  upd(rc(p), mid + 1, r, tag[p]);
  tag[p] = 0;
}

void change(LL L, LL R, LL l, LL r, LL p, LL x) {
  if (L <= l && r <= R) {
    tag[p] = x;
    num[p] = qaq[r - l + 1] * x % 998244353;
    return;
  }
  if (tag[p])
    pushdown(p, l, r);
  LL mid = (l + r) >> 1;
  if (L <= mid)
    change(L, R, l, mid, lc(p), x);
  if (R >= mid + 1)
    change(L, R, mid + 1, r, rc(p), x);
  pushup(p, l, r);
}

void Q(LL L, LL R, LL l, LL r, LL p) {
  if (L <= l && r <= R) {
    QAQ *= qpow(10, r - l + 1, 998244353);
    QAQ %= 998244353;
    QAQ += num[p];
    return;
  }
  if (tag[p])
    pushdown(p, l, r);
  LL mid = (l + r) >> 1;
  if (L <= mid) {
    Q(L, R, l, mid, lc(p));
  }
  if (R >= mid + 1)
    Q(L, R, mid + 1, r, rc(p));
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  LL n, m;
  cin >> n >> m;
  qp[0] = 1;
  for (int i = 1; i <= n; i++) {
    qp[i] = qp[i - 1] * 10;
    qp[i] %= 998244353;
  }
  build(1, 1, n);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    qaq[i] = qaq[i - 1] + qpow(10, i - 1, 998244353);
    qaq[i] %= 998244353;
  }
  ans = qaq[n];
  for (LL i = 1; i <= m; i++) {
    QAQ = 0;
    LL l, r, x;
    cin >> l >> r >> x;
    Q(l, r, 1, n, 1);
    LL awa = QAQ;
    //		cout << awa << endl;
    ans -= awa * qpow(10, n - r, 998244353);
    while (ans < 0)
      ans += 998244353;
    QAQ = 0;
    ans %= 998244353;
    change(l, r, 1, n, 1, x);
    awa = qaq[r - l + 1] * x % 998244353;
    ans += awa * qpow(10, n - r, 998244353);
    ans %= 998244353;
    cout << ans << endl;
  }
  return 0;
}