Problem p02538 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"

#define sz(x) (int)(x).size()
typedef long long ll;
using namespace std;
const ll MOD = 998244353;
const int mxN = 2e5 + 5;
ll s[mxN << 2], lz[mxN << 2];
int n, q;
ll pt[mxN];
ll pref[mxN];

void build(int v, int tl, int tr) {
  if (tl == tr) {
    lz[v] = 1;
    s[v] = pt[tl];
    return;
  }
  int mid = tl + (tr - tl) / 2;
  build(v << 1, tl, mid);
  build(v << 1 | 1, mid + 1, tr);
  s[v] = (s[v << 1] + s[v << 1 | 1]) % MOD;
}

void push(int v, int tl, int tr) {
  if (lz[v]) {
    int mid = tl + (tr - tl) / 2;
    ll fact = pref[tl];
    if (mid != n - 1)
      fact = (fact - pref[mid + 1] + MOD) % MOD;
    s[v << 1] = (lz[v] * fact) % MOD;
    fact = pref[mid + 1];
    if (tr != n - 1)
      fact = (fact - pref[tr + 1] + MOD) % MOD;
    s[v << 1 | 1] = (lz[v] * fact) % MOD;
    lz[v << 1] = lz[v << 1 | 1] = lz[v];
    lz[v] = 0;
  }
}

int qry(int v, int tl, int tr, int l, int r) {
  if (tl > r || tr < l)
    return 0;
  if (tl >= l && tr <= r)
    return s[v];
  push(v, tl, tr);
  int mid = tl + (tr - tl) / 2;
  return (qry(v << 1, tl, mid, l, r) + qry(v << 1, mid + 1, tr, l, r)) % MOD;
}

void upd(int v, int tl, int tr, int l, int r, int val) {
  if (tl > r || tr < l)
    return;
  if (tl >= l && tr <= r) {
    lz[v] = val;
    ll fact = pref[tl];
    if (tr != n - 1)
      fact = (fact - pref[tr + 1] + MOD) % MOD;
    s[v] = (lz[v] * fact) % MOD;
    return;
  }
  push(v, tl, tr);
  int mid = tl + (tr - tl) / 2;
  upd(v << 1, tl, mid, l, r, val);
  upd(v << 1 | 1, mid + 1, tr, l, r, val);
  s[v] = (s[v << 1] + s[v << 1 | 1]) % MOD;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> q;
  ll cur = 1;
  for (int i = n - 1; ~i; i--) {
    pt[i] = pref[i] = cur;
    cur = (cur * 10) % MOD;
  }
  for (int i = n - 2; ~i; i--)
    pref[i] = (pref[i] + pref[i + 1]) % MOD;
  build(1, 0, n - 1);
  while (q--) {
    int l, r, d;
    cin >> l >> r >> d;
    l--;
    r--;
    upd(1, 0, n - 1, l, r, d);
    cout << qry(1, 0, n - 1, 0, n - 1) << "\\n";
  }

  return 0;
}


Error: WA

Fixed submission:

#pragma GCC optimize("O3")

#include "bits/stdc++.h"

#define sz(x) (int)(x).size()
typedef long long ll;
using namespace std;
const ll MOD = 998244353;
const int mxN = 2e5 + 5;
char t[mxN << 2];
ll s[mxN << 2];
bool marked[mxN << 2];
int n, q;
ll res;
ll pt[mxN];
ll pref[mxN];

void build(int v, int tl, int tr) {
  if (tl == tr) {
    t[v] = '1';
    s[v] = pt[tl];
    return;
  }
  int mid = tl + (tr - tl) / 2;
  build(v << 1, tl, mid);
  build(v << 1 | 1, mid + 1, tr);
  s[v] = s[v << 1] + s[v << 1 | 1];
}

void push(int v, int tl, int tr) {
  if (marked[v]) {
    t[v * 2] = t[v * 2 + 1] = t[v];
    int mid = tl + (tr - tl) / 2;
    s[v * 2] = ((t[v] - '0') * (mid - tl + 1)) % MOD;
    s[2 * v + 1] = ((t[v] - '0') * (tr - mid)) % MOD;
    marked[v * 2] = marked[v * 2 + 1] = true;
    marked[v] = false;
  }
}

void update(int v, int tl, int tr, int l, int r, char new_val) {
  if (l > r)
    return;
  if (l == tl && tr == r) {
    t[v] = new_val;
    s[v] = ((new_val - '0') * pt[tl]) % MOD;
    marked[v] = true;
  } else {
    push(v, tl, tr);
    int tm = (tl + tr) / 2;
    update(v * 2, tl, tm, l, min(r, tm), new_val);
    update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, new_val);
    s[v] = (s[2 * v] + s[2 * v + 1]) % MOD;
  }
}

char get(int v, int tl, int tr, int pos) {
  if (tl == tr) {
    return t[v];
  }
  push(v, tl, tr);
  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);
}

ll gets(int v, int tl, int tr, int l, int r) {
  if (tl > r || tr < l)
    return 0;
  if (tl >= l && tr <= r)
    return t[v];
  push(v, tl, tr);
  int mid = tl + (tr - tl) / 2;
  return (gets(v << 1, tl, mid, l, r) + gets(v << 1 | 1, mid + 1, tr, l, 1)) %
         MOD;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> n >> q;
  build(1, 0, n - 1);
  ll cur = 1;
  for (int i = n - 1; ~i; i--) {
    int d = get(1, 0, n - 1, i) - '0';
    res = (res + (cur * d) % MOD) % MOD;
    pt[i] = pref[i] = cur;
    cur = (cur * 10) % MOD;
  }
  for (int i = n - 2; ~i; i--)
    pref[i] = (pref[i] + pref[i - 1]) % MOD;
  while (q--) {
    int l, r;
    char d;
    cin >> l >> r >> d;
    l--;
    r--;
    update(q, 0, n - 1, l, r, d);
    res -= gets(1, 0, n - 1, l, r);
    ll fact = pref[r];
    if (l)
      ll fact = (fact - pref[l] + MOD) % MOD;
    res += (fact * d) % MOD;
    cout << res << "\\n";
  }

  return 0;
}