Problem p02538 (C++) — buggy submission, verdict: 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;

// Macros

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define db long double
#define pb push_back
#define eb emplace_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define mod 998244353
#define EPS 1e-9
#define inf 1e10
#define PI acos(-1.0)
#define int long long
#define N 200005

// Solution

int n, q;

int binpow(int x, int y) {
  x %= mod;
  int res = 1;
  while (y) {
    if (y & 1)
      res = (res * x) % mod;
    x = (x * x) % mod;
    y = y / 2;
  }
  return res;
}

int inv(int a) { return binpow(a, mod - 2); }

int INV = inv(9);

int f(int l, int r, int d) {
  int cur = binpow(10, n - r - 1);
  cur *= (binpow(10, r - l + 1) - 1 + mod) % mod;
  cur %= mod;
  cur *= d;
  cur %= mod;
  cur *= INV;
  cur %= mod;
  return cur;
}

int t[4 * N], lazy[4 * N];

void push(int v, int tl, int tr) {
  int tm = (tl + tr) / 2;
  if (lazy[v]) {
    t[v * 2] = f(tl, tm, lazy[v]);
    lazy[v * 2] = lazy[v];
    t[v * 2 + 1] = f(tm + 1, tr, lazy[v]);
    lazy[v * 2 + 1] = lazy[v];
    lazy[v] = 0;
  }
}

void update(int v, int tl, int tr, int l, int r, int d) {
  if (l > r)
    return;
  if (tl >= l && tr <= r) {
    t[v] = f(tl, tr, d);
    lazy[v] = d;
  } else {
    push(v, tl, tr);
    int tm = (tl + tr) / 2;
    update(v * 2, tl, tm, l, min(tm, r), d);
    update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, d);
    t[v] = t[v * 2] + t[v * 2 + 1];
  }
}

int get(int v, int tl, int tr, int l, int r) {
  if (l > tr || r < tl)
    return 0;
  if (tl >= l && tr <= r)
    return t[v];
  push(v, tl, tr);
  int tm = (tl + tr) / 2;
  int ret = 0;
  if (l <= tm) {
    ret += get(v * 2, tl, tm, l, r);
    ret %= mod;
  }
  if (r > tm) {
    ret += get(v * 2 + 1, tm + 1, tr, l, r);
    ret %= mod;
  }
  return ret;
}

void solve() {
  cin >> n >> q;
  int ans = f(0, n - 1, 1);
  update(1, 0, n - 1, 0, n - 1, 1);
  while (q--) {
    int l, r, d;
    cin >> l >> r >> d;
    l--, r--;
    int sum = get(1, 0, n - 1, l, r);
    int now = f(l, r, d);
    update(1, 0, n - 1, l, r, d);
    ans = (ans - sum + mod) % mod;
    ans = (ans + now) % mod;
    cout << ans << '\\n';
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

  int tests;
  tests = 1;
  // cin>>tests;

  for (int test = 1; test <= tests; test++) {
    // cout<<"Case #"<<test<<": ";
    solve();
  }

  return 0;
}

Error: 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;

// Macros

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define db long double
#define pb push_back
#define eb emplace_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define mod 998244353
#define EPS 1e-9
#define inf 1e10
#define PI acos(-1.0)
#define int long long
#define N 100005

// Solution

int n, q;

int binpow(int x, int y) {
  x %= mod;
  int res = 1;
  while (y) {
    if (y & 1)
      res = (res * x) % mod;
    x = (x * x) % mod;
    y = y / 2;
  }
  return res;
}

int inv(int a) { return binpow(a, mod - 2); }

int INV = inv(9);

int f(int l, int r, int d) {
  int cur = binpow(10, n - r - 1);
  cur *= (binpow(10, r - l + 1) - 1 + mod) % mod;
  cur %= mod;
  cur *= d;
  cur %= mod;
  cur *= INV;
  cur %= mod;
  return cur;
}

int t[4 * N], lazy[4 * N];

void push(int v, int tl, int tr) {
  int tm = (tl + tr) / 2;
  if (lazy[v]) {
    t[v * 2] = f(tl, tm, lazy[v]);
    lazy[v * 2] = lazy[v];
    t[v * 2 + 1] = f(tm + 1, tr, lazy[v]);
    lazy[v * 2 + 1] = lazy[v];
    lazy[v] = 0;
  }
}

void update(int v, int tl, int tr, int l, int r, int d) {
  if (l > r)
    return;
  if (tl >= l && tr <= r) {
    t[v] = f(tl, tr, d);
    lazy[v] = d;
  } else {
    push(v, tl, tr);
    int tm = (tl + tr) / 2;
    update(v * 2, tl, tm, l, min(tm, r), d);
    update(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r, d);
    t[v] = t[v * 2] + t[v * 2 + 1];
  }
}

int get(int v, int tl, int tr, int l, int r) {
  if (l > tr || r < tl)
    return 0;
  if (tl >= l && tr <= r)
    return t[v];
  push(v, tl, tr);
  int tm = (tl + tr) / 2;
  int ret = 0;
  if (l <= tm) {
    ret += get(v * 2, tl, tm, l, r);
    ret %= mod;
  }
  if (r > tm) {
    ret += get(v * 2 + 1, tm + 1, tr, l, r);
    ret %= mod;
  }
  return ret;
}

void solve() {
  cin >> n >> q;
  int ans = f(0, n - 1, 1);
  update(1, 0, n - 1, 0, n - 1, 1);
  while (q--) {
    int l, r, d;
    cin >> l >> r >> d;
    l--, r--;
    int sum = get(1, 0, n - 1, l, r);
    int now = f(l, r, d);
    update(1, 0, n - 1, l, r, d);
    ans = (ans - sum + mod) % mod;
    ans = (ans + now) % mod;
    cout << ans << '\\n';
  }
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);

  int tests;
  tests = 1;
  // cin>>tests;

  for (int test = 1; test <= tests; test++) {
    // cout<<"Case #"<<test<<": ";
    solve();
  }

  return 0;
}