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

#include <atcoder/all>
#include <bits/stdc++.h>
using namespace std;
using namespace atcoder;
typedef long long ll;
const double pi = 3.141592653589793;
typedef unsigned long long ull;
typedef long double ldouble;
const ll INF = 1e18;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

// コンパイル方法
// g++ lazy_segment/___.cpp -std=c++17 -I .
// ./a.out

typedef modint998244353 mint;

const mint inv9 = mint(1) / 9;

struct S {
  mint x, w;
  S(mint x = 0, mint w = 1) : x(x), w(w) {}
};

S op(S a, S b) { return S(a.x * b.w + b.x, a.w * b.w); }
S e() { return S(); }

S mapping(int f, S a) {
  if (f == 0)
    return a;
  return S((a.w - 1) * inv9 * f, a.w);
}

int composition(int f, int g) {
  if (f == 0)
    return g;
  else
    return f;
}

int id() { return 0; }

int main() {
  int n, q;
  cin >> n >> q;

  lazy_segtree<S, op, e, int, mapping, composition, id> t(n);
  rep(i, n) t.set(i, S(1, 10));
  rep(qi, q) {
    int l, r, d;
    cin >> l >> r >> d;
    l--;
    t.apply(l, r, d);
    S ans = t.prod(0, n);
    cout << ans.x.val() << endl;
  }
  return 0;
}

Error: -6
double free or corruption (out)


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double pi = 3.141592653589793;
typedef unsigned long long ull;
typedef long double ldouble;
const ll INF = 1e18;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <int mod> struct ModInt {
  int val;
  ModInt() : val(0) {}
  ModInt(long long x) : val(x >= 0 ? x % mod : (mod - (-x) % mod) % mod) {}
  int getmod() { return mod; }
  ModInt &operator+=(const ModInt &p) {
    if ((val += p.val) >= mod) {
      val -= mod;
    }
    return *this;
  }
  ModInt &operator-=(const ModInt &p) {
    if ((val += mod - p.val) >= mod) {
      val -= mod;
    }
    return *this;
  }
  ModInt &operator*=(const ModInt &p) {
    val = (int)(1LL * val * p.val % mod);
    return *this;
  }
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
  ModInt operator-() const { return ModInt(-val); }
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
  bool operator==(const ModInt &p) const { return val == p.val; }
  bool operator!=(const ModInt &p) const { return val != p.val; }
  ModInt inverse() const {
    int a = val, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
  ModInt pow(long long n) const {
    ModInt ret(1), mul(val);
    while (n > 0) {
      if (n & 1)
        ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.val;
  }
  friend istream &operator>>(istream &is, ModInt &a) {
    long long t;
    is >> t;
    a = ModInt<mod>(t);
    return (is);
  }
  static int get_mod() { return mod; }
};
const int MOD = 998244353; // if inv is needed, this shold be prime.
using modint = ModInt<MOD>;

const int b = 500;

int main() {
  int n, q;
  cin >> n >> q;
  int c = (n + b - 1) / b;
  modint ki = 0, ks = 1;
  rep(i, 500) {
    ki += ks;
    ks *= 10;
  }
  vector<modint> ten(c);
  vector<modint> t(n);
  ks = 1;
  rep(i, n) {
    t[i] = ks;
    ks *= 10;
  }
  rep(i, c) { ten[i] = t[i * b] * ki; }
  vector<modint> g(b, 0);
  rep(i, c) { g[i] = ten[i]; }
  vector<int> f(n, 1);
  vector<int> y(b, 1);
  rep(i, q) {
    int l, r, d;
    cin >> l >> r >> d;
    swap(l, r);
    l = n - l;
    r = n - r;
    rep2(j, l, r + 1) {
      if (j % b == 0 && j + b <= r) {
        g[j / b] = ten[j / b] * d;
        y[(j / b)] = d;
        j += (b - 1);
      } else {
        if (y[(j / b)] == 0) {
          f[j] = d;
        } else if (y[(j / b)] != 0) {
          rep2(k, (int)(j / b) * b, (int)(j / b) * b + b) { f[k] = y[(j / b)]; }
          y[(j / b)] = 0;
          g[j / b] = 0;
          f[j] = d;
        }
      }
    }
    modint ans = 0;
    rep(j, c) {
      if (g[j] == 0) {
        rep(k, b) {
          int id = k + j * b;
          if (id >= n)
            break;
          ans += t[id] * f[id];
        }
      } else {
        ans += g[j];
      }
    }
    cout << ans << endl;
  }
}