Problem p02538 (C++)

Buggy submission (status: Runtime Error):

// Problem : E - Replace Digits
// Contest : AtCoder - ACL Beginner Contest
// URL : https://atcoder.jp/contests/abl/tasks/abl_e
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long
#define db double
#define mod 998244353
#define pb push_back
#define pp pair<int, int>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repe(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = b; i >= a; i--)
#define N 200005
#define ss second
#define ff first
#define all(a) a.begin(), a.end()
#define lb lower_bound
#define ub upper_bound
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
int inv(int a, int b) { return 1 < a ? b - inv(b % a, a) * b / a : 1; }
int n, q;
int st[N * 4], lazy[N * 4], po[N];
void build(int p, int l, int r) {
  if (l == r) {
    st[p] = po[l];
    return;
  }
  int m = (l + r) / 2;
  build(2 * p, l, m);
  build(2 * p + 1, m + 1, r);
  st[p] = st[2 * p] + st[2 * p + 1];
}
void fun() {
  int s = 1;
  for (int i = 0; i < N; i++) {
    po[i] = s;
    s *= 10;
    s %= mod;
  }
}
int cal(int l, int r, int d) {
  int a = d * po[l];
  a %= mod;
  int len = r - l + 1;
  a *= (po[len] - 1);
  a %= mod;
  int t = inv(9, mod);
  a *= t;
  a %= mod;
  return a;
}
void push(int p, int l, int r) {
  if (lazy[p] == 0)
    return;
  int m = (l + r) / 2;
  lazy[2 * p] = lazy[p];
  lazy[2 * p + 1] = lazy[p];
  int d = lazy[p];
  lazy[p] = 0;

  st[2 * p] = cal(l, m, d);
  st[2 * p + 1] = cal(m + 1, r, d);
}
int query(int p, int l, int r, int i, int j) {
  if (i > r || j < l)
    return 0;
  push(p, l, r);
  if (i <= l and r <= j)
    return st[p];
  int m = (l + r) / 2;
  return (query(2 * p, l, m, i, j) + query(2 * p + 1, m + 1, r, i, j)) % mod;
}
void update(int p, int l, int r, int i, int j, int d) {
  if (i > r || j < l)
    return;
  push(p, l, r);
  if (i <= l and r <= j) {
    st[p] = cal(l, r, d);
    lazy[p] = d;
    return;
  }
  int m = (l + r) / 2;
  update(2 * p, l, m, i, j, d);
  update(2 * p + 1, m + 1, r, i, j, d);
  st[p] = (st[2 * p] + st[2 * p + 1]) % mod;
}
void solve() {
  fun();
  cin >> n;
  build(1, 0, n - 1);
  cin >> q;
  while (q--) {
    int l, r, d;
    cin >> l >> r >> d;
    l = n - l;
    r = n - r;
    swap(l, r);
    int num = st[1];
    int diff = query(1, 0, n - 1, l, r);
    num -= diff;
    num %= mod;
    num += mod;
    num %= mod;

    num += cal(l, r, d);
    num %= mod;
    cout << num << "\n";
    update(1, 0, n - 1, l, r, d);
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}


Fixed submission:

// Problem : E - Replace Digits
// Contest : AtCoder - ACL Beginner Contest
// URL : https://atcoder.jp/contests/abl/tasks/abl_e
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define int long long
#define db double
#define mod 998244353
#define pb push_back
#define pp pair<int, int>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repe(i, a, b) for (int i = a; i <= b; i++)
#define repr(i, a, b) for (int i = b; i >= a; i--)
#define N 300005
#define ss second
#define ff first
#define all(a) a.begin(), a.end()
#define lb lower_bound
#define ub upper_bound
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
int inv(int a, int b) { return 1 < a ? b - inv(b % a, a) * b / a : 1; }
int n, q;
int st[N * 4], lazy[N * 4], po[N];
void build(int p, int l, int r) {
  if (l == r) {
    st[p] = po[l];
    return;
  }
  int m = (l + r) / 2;
  build(2 * p, l, m);
  build(2 * p + 1, m + 1, r);
  st[p] = st[2 * p] + st[2 * p + 1];
}
void fun() {
  int s = 1;
  for (int i = 0; i < N; i++) {
    po[i] = s;
    s *= 10;
    s %= mod;
  }
}
int cal(int l, int r, int d) {
  int a = d * po[l];
  a %= mod;
  int len = r - l + 1;
  a *= (po[len] - 1);
  a %= mod;
  int t = inv(9, mod);
  a *= t;
  a %= mod;
  return a;
}
void push(int p, int l, int r) {
  if (lazy[p] == 0)
    return;
  int m = (l + r) / 2;
  lazy[2 * p] = lazy[p];
  lazy[2 * p + 1] = lazy[p];
  int d = lazy[p];
  lazy[p] = 0;

  st[2 * p] = cal(l, m, d);
  st[2 * p + 1] = cal(m + 1, r, d);
}
int query(int p, int l, int r, int i, int j) {
  if (i > r || j < l)
    return 0;
  push(p, l, r);
  if (i <= l and r <= j)
    return st[p];
  int m = (l + r) / 2;
  return (query(2 * p, l, m, i, j) + query(2 * p + 1, m + 1, r, i, j)) % mod;
}
void update(int p, int l, int r, int i, int j, int d) {
  if (i > r || j < l)
    return;
  push(p, l, r);
  if (i <= l and r <= j) {
    st[p] = cal(l, r, d);
    lazy[p] = d;
    return;
  }
  int m = (l + r) / 2;
  update(2 * p, l, m, i, j, d);
  update(2 * p + 1, m + 1, r, i, j, d);
  st[p] = (st[2 * p] + st[2 * p + 1]) % mod;
}
void solve() {
  fun();
  cin >> n;
  build(1, 0, n - 1);
  cin >> q;
  while (q--) {
    int l, r, d;
    cin >> l >> r >> d;
    l = n - l;
    r = n - r;
    swap(l, r);
    int num = st[1];
    int diff = query(1, 0, n - 1, l, r);
    num -= diff;
    num %= mod;
    num += mod;
    num %= mod;

    num += cal(l, r, d);
    num %= mod;
    cout << num << "\n";
    update(1, 0, n - 1, l, r, d);
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}