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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define re register
#define ls (o << 1)
#define m (l + r) / 2
#define rs (o << 1 | 1)
#define pb push_back
const double PI = acos(-1.0);
const int M = 1e6 + 7;
/*
int head[M],cnt=1;
void init(int n){cnt=1;for(int i=0;i<=n;i++)head[i]=0;}
struct EDGE{int to,nxt,w;}ee[M*2];
void add(int x,int y,int
w){ee[++cnt].nxt=head[x],ee[cnt].w=w,ee[cnt].to=y,head[x]=cnt;}
*/
const int mod = 998244353;
struct node {
  ll val, len;
};
ll s[M], qp[M];
ll sm[M << 2]; // 区间数字组成的数的值。
ll tg[M << 2];
void bd(int o, int l, int r) {
  if (l == r) {
    sm[o] = 1;
    return;
  }
  bd(ls, l, m);
  bd(rs, m + 1, r);
  sm[o] = (sm[ls] * qp[r - m] % mod + sm[rs]) % mod;
}
void pd(int o, int l, int r) {
  if (tg[o] == 0)
    return;
  sm[ls] = s[m - l + 1] * tg[o] % mod;
  sm[rs] = s[r - m] * tg[o] % mod;
  tg[ls] = tg[rs] = tg[o];
  tg[o] = 0;
}
void up(int o, int l, int r, int x, int y, ll d) // 区间覆盖
{
  if (x <= l && r <= y) {
    sm[o] = s[r - l + 1] * d % mod;
    tg[o] = d;
    return;
  }
  pd(o, l, r);
  if (x <= m)
    up(ls, l, m, x, y, d);
  if (y > m)
    up(rs, m + 1, r, x, y, d);

  sm[o] = (sm[ls] * qp[r - m] % mod + sm[rs]) % mod;
  // cout<<" = = "<<l<<"  "<<r<<" "<<sm[ls]<<" "<<sm[rs]<<"  "<<sm[o]<<endl;
}
node qu(int o, int l, int r, int x, int y) {
  if (x <= l && r <= y) {
    return node{sm[o], r - l + 1};
  }
  pd(o, l, r);
  node a = node{0, 0}, b = node{0, 0};
  if (x <= m)
    a = qu(ls, l, m, x, y);
  if (y > m)
    b = qu(rs, m + 1, r, x, y);
  //	cout<<l<<"   "<<r<<"   =  "<<a.val<<" "<<a.len<<"  b: "<<b.val<<" "
  //<<b.len<<endl;
  return node{a.val * qp[b.len] % mod + b.val, a.len + b.len};
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  s[1] = qp[0] = 1;
  qp[1] = 10;
  for (int i = 2; i <= n; i++)
    s[i] = (s[i - 1] + qp[i - 1]) % mod, qp[i] = qp[i - 1] * 10 % mod;
  bd(1, 1, n);
  while (q--) {
    int l, r, opt;
    ll d;
    cin >> l >> r >> d;
    up(1, 1, n, l, r, d);
    cout << qu(1, 1, n, 1, n).val << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define re register
#define ls (o << 1)
#define m (l + r) / 2
#define rs (o << 1 | 1)
#define pb push_back
const double PI = acos(-1.0);
const int M = 1e5 + 7;
/*
int head[M],cnt=1;
void init(int n){cnt=1;for(int i=0;i<=n;i++)head[i]=0;}
struct EDGE{int to,nxt,w;}ee[M*2];
void add(int x,int y,int
w){ee[++cnt].nxt=head[x],ee[cnt].w=w,ee[cnt].to=y,head[x]=cnt;}
*/
const int mod = 998244353;
struct node {
  ll val, len;
};
ll s[M], qp[M];
ll sm[M << 2]; // 区间数字组成的数的值。
ll tg[M << 2];
void bd(int o, int l, int r) {
  if (l == r) {
    sm[o] = 1;
    return;
  }
  bd(ls, l, m);
  bd(rs, m + 1, r);
  sm[o] = (sm[ls] * qp[r - m] % mod + sm[rs]) % mod;
}
void pd(int o, int l, int r) {
  if (tg[o] == 0)
    return;
  sm[ls] = s[m - l + 1] * tg[o] % mod;
  sm[rs] = s[r - m] * tg[o] % mod;
  tg[ls] = tg[rs] = tg[o];
  tg[o] = 0;
}
void up(int o, int l, int r, int x, int y, ll d) // 区间覆盖
{
  if (x <= l && r <= y) {
    sm[o] = s[r - l + 1] * d % mod;
    tg[o] = d;
    return;
  }
  pd(o, l, r);
  if (x <= m)
    up(ls, l, m, x, y, d);
  if (y > m)
    up(rs, m + 1, r, x, y, d);

  sm[o] = (sm[ls] * qp[r - m] % mod + sm[rs]) % mod;
  // cout<<" = = "<<l<<"  "<<r<<" "<<sm[ls]<<" "<<sm[rs]<<"  "<<sm[o]<<endl;
}
node qu(int o, int l, int r, int x, int y) {
  if (x <= l && r <= y) {
    return node{sm[o], r - l + 1};
  }
  pd(o, l, r);
  node a = node{0, 0}, b = node{0, 0};
  if (x <= m)
    a = qu(ls, l, m, x, y);
  if (y > m)
    b = qu(rs, m + 1, r, x, y);
  //	cout<<l<<"   "<<r<<"   =  "<<a.val<<" "<<a.len<<"  b: "<<b.val<<" "
  //<<b.len<<endl;
  return node{a.val * qp[b.len] % mod + b.val, a.len + b.len};
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  s[1] = qp[0] = 1;
  qp[1] = 10;
  for (int i = 2; i <= n; i++)
    s[i] = (s[i - 1] + qp[i - 1]) % mod, qp[i] = qp[i - 1] * 10 % mod;
  bd(1, 1, n);
  while (q--) {
    int l, r, opt;
    ll d;
    cin >> l >> r >> d;
    up(1, 1, n, l, r, d);
    cout << qu(1, 1, n, 1, n).val << endl;
  }
  return 0;
}