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

#include <bits/stdc++.h>

#define ms(a, b) memset(a, b, sizeof(a))
#define rep(a, b, c) for (int a = (int)(b); a < (int)(c); a++)
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define m_p(a, b) make_pair(a, b)
#define lson l, mid, o << 1
#define rson mid + 1, r, o << 1 | 1
#define ls o << 1
#define rs o << 1 | 1
#define inf 0x3f3f3f3f

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int, int> pii;

inline bool isdigit(char &ch) { return ch >= '0' && ch <= '9'; }
template <class T> void read(T &x) {
  x = 0;
  ll f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x << 1) + (x << 3) + ch - '0';
  x *= f;
}
template <class T> inline void write(T x) {
  if (x == 0) {
    putchar('0');
    return;
  }
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  int _stk[65], _top = 0;
  for (; x; x /= 10)
    _stk[++_top] = x % 10 + 48;
  for (; _top; _top--)
    putchar(_stk[_top]);
}
const int MAXN = 300000 + 100;
const ll mod = 998244353;

int n, k;
ll sum[MAXN << 2], pw[MAXN << 2];
ll ch[MAXN << 2];
ll inv[50];

ll qpow(ll x, ll y) {
  ll r = 1;
  while (y) {
    if (y & 1)
      r = r * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return r;
}
void pushup(int o, int l, int r) {
  int mid = l + r >> 1;
  ll len = (r - mid);
  sum[o] = (sum[ls] * pw[len] % mod + sum[rs]) % mod;
}

void pushdown(int o, int l, int r) {
  int mid = l + r >> 1;
  int len2 = (r - mid);
  int len1 = (mid - l + 1);
  if (ch[o]) {
    ch[ls] = ch[o];
    sum[ls] = pw[len1] % mod;
    sum[ls] -= 1;
    if (sum[ls] < 0)
      sum[ls] += mod;
    sum[ls] = sum[ls] * inv[9] % mod * ch[ls] % mod;
  }
  if (ch[o]) {
    ch[rs] = ch[o];
    sum[rs] = pw[len2] % mod;
    sum[rs] -= 1;
    if (sum[rs] < 0)
      sum[rs] += mod;
    sum[rs] = sum[rs] * inv[9] % mod * ch[rs] % mod;
  }
  ch[o] = 0;
}

void build(int l, int r, int o) {
  if (l == r) {
    sum[o] = 1;
    return;
  }
  int mid = l + r >> 1;
  build(lson);
  build(rson);
  pushup(o, l, r);
}

void update(int L, int R, ll d, int l, int r, int o) {
  pushdown(o, l, r);
  if (L <= l && r <= R) {
    ch[o] = d;
    int len = r - l + 1;
    ll s = pw[len] % mod;
    s -= 1;
    if (s < 0)
      s += mod;
    sum[o] = s * inv[9] % mod * d % mod;
    //	cout << sum[o] << endl;
    return;
  }
  int mid = l + r >> 1;
  if (L <= mid)
    update(L, R, d, lson);
  if (mid < R)
    update(L, R, d, rson);
  pushup(o, l, r);
}

int main() {
  read(n);
  read(k);
  pw[0] = 1;
  rep(i, 1, n + 10) pw[i] = pw[i - 1] * 10 % mod;
  rep(i, 1, 12) inv[i] = qpow(i, mod - 2);
  build(1, n, 1);
  //	cout << sum[1] << endl;
  while (k--) {
    int l, r;
    ll d;
    read(l);
    read(r);
    read(d);
    update(l, r, d, 1, n, 1);
    //		cout << ch[5] << "A" << endl;
    //	cout << sum[5] << "a" << endl;
    write(sum[1]);
    putchar('\\n');
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define ms(a, b) memset(a, b, sizeof(a))
#define rep(a, b, c) for (int a = (int)(b); a < (int)(c); a++)
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define m_p(a, b) make_pair(a, b)
#define lson l, mid, o << 1
#define rson mid + 1, r, o << 1 | 1
#define ls o << 1
#define rs o << 1 | 1
#define inf 0x3f3f3f3f

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int, int> pii;

inline bool isdigit(char &ch) { return ch >= '0' && ch <= '9'; }
template <class T> void read(T &x) {
  x = 0;
  ll f = 1;
  char ch = getchar();
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      f = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x << 1) + (x << 3) + ch - '0';
  x *= f;
}
template <class T> inline void write(T x) {
  if (x == 0) {
    putchar('0');
    return;
  }
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  int _stk[65], _top = 0;
  for (; x; x /= 10)
    _stk[++_top] = x % 10 + 48;
  for (; _top; _top--)
    putchar(_stk[_top]);
}
const int MAXN = 200000 + 100;
const ll mod = 998244353;

int n, k;
ll sum[MAXN << 2], pw[MAXN + 10];
ll ch[MAXN << 2];
ll inv[15];

ll qpow(ll x, ll y) {
  ll r = 1;
  while (y) {
    if (y & 1)
      r = r * x % mod;
    x = x * x % mod;
    y >>= 1;
  }
  return r;
}
void pushup(int o, int l, int r) {
  int mid = l + r >> 1;
  int len = (r - mid);
  sum[o] = (sum[ls] * pw[len] % mod + sum[rs]) % mod;
}

void pushdown(int o, int l, int r) {
  int mid = l + r >> 1;
  int len2 = (r - mid);
  int len1 = (mid - l + 1);
  if (ch[o]) {
    ch[ls] = ch[o];
    sum[ls] = pw[len1] % mod;
    sum[ls] -= 1;
    if (sum[ls] < 0)
      sum[ls] += mod;
    sum[ls] = sum[ls] * inv[9] % mod * ch[ls] % mod;
  }
  if (ch[o]) {
    ch[rs] = ch[o];
    sum[rs] = pw[len2] % mod;
    sum[rs] -= 1;
    if (sum[rs] < 0)
      sum[rs] += mod;
    sum[rs] = sum[rs] * inv[9] % mod * ch[rs] % mod;
  }
  ch[o] = 0;
}

void build(int l, int r, int o) {
  if (l == r) {
    sum[o] = 1;
    return;
  }
  int mid = l + r >> 1;
  build(lson);
  build(rson);
  pushup(o, l, r);
}

void update(int L, int R, ll d, int l, int r, int o) {
  pushdown(o, l, r);
  if (L <= l && r <= R) {
    ch[o] = d;
    int len = r - l + 1;
    ll s = pw[len] % mod;
    s -= 1;
    if (s < 0)
      s += mod;
    sum[o] = s * inv[9] % mod * d % mod;
    //	cout << sum[o] << endl;
    return;
  }
  int mid = l + r >> 1;
  if (L <= mid)
    update(L, R, d, lson);
  if (mid < R)
    update(L, R, d, rson);
  pushup(o, l, r);
}

int main() {
  read(n);
  read(k);
  pw[0] = 1;
  rep(i, 1, n + 10) pw[i] = pw[i - 1] * 10 % mod;
  rep(i, 1, 12) inv[i] = qpow(i, mod - 2);
  build(1, n, 1);
  //	cout << sum[1] << endl;
  while (k--) {
    int l, r;
    ll d;
    read(l);
    read(r);
    read(d);
    update(l, r, d, 1, n, 1);
    //		cout << ch[5] << "A" << endl;
    //	cout << sum[5] << "a" << endl;
    write(sum[1]);
    putchar('\\n');
  }
  return 0;
}