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

#include <bits/stdc++.h>
#include <queue>
using namespace std;

#define what_is(x) cout << #x << " is " << x << endl;
#define PI acos(-1)
#define hell 1000000007
#define HELL 998244353
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define fix(n) cout << fixed << setprecision(n)
#define mset(a, n) memset(a, n, sizeof a)
#define rep(i, a, b) for (__typeof((b)) i = (a); i < (b); i++)
#define repp(i, a, b, p) for (__typeof((b)) i = (a); i < (b); i += p)
#define ren(i, a, b) for (__typeof((a)) i = (a); i >= (b); i--)
#define renn(i, a, b, p) for(__typeof((a) i=(a);i>=(b);i-=p)
#define ADD(a, b, c) ((a) % c + (b) % c) % c
#define SUB(a, b, c) ((a) % c - (b) % c + c) % c
#define MUL(a, b, c) (((a) % c) * ((b) % c)) % c
#define lbd lower_bound
#define ubd upper_bound
#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define all(v) (v).begin(), (v).end()
#define sz(x) (ll) x.size()
// #define out(n) cout<<n<<" "
#define outl(n) cout << n << endl
#define bug(n)                                                                 \\
  {                                                                            \\
    outl(n);                                                                   \\
    return;                                                                    \\
  }
#define pii pair<int, int>
#define lim 9223372036854775806
#define ninf -9223372036854775802
#define M 998244353
#define eps 1e-9
#define MAX 2
#define N 200001
#define db(a)                                                                  \\
  for (auto it : a) {                                                          \\
    cout << it << " ";                                                         \\
  }                                                                            \\
  cout << endl;
ll prime[MAX];
ll fact[MAX];

const long long INF64 = 1e18;
ll vpow(ll a, ll b, ll p) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  ll temp = vpow(a, b / 2, p);
  temp *= temp;
  temp %= p;
  if (b % 2 == 0)
    return temp;
  temp = temp * a;
  temp %= p;
  return temp;
}

ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

ll inverseMod(ll a, ll b, ll p) {
  if (b == 0)
    return 1;
  ll temp = vpow(b, p - 2, p);
  ll ans = temp * a;
  ans %= p;
  return ans;
}
void sieve() {
  prime[0] = prime[1] = 1;
  for (ll i = 2; i < MAX; i++) {
    if (prime[i] == 0) {
      for (int j = i * 2; j < MAX; j += i) {
        if (prime[j] == 0) {
          prime[j] = i;
        }
      }
      prime[i] = i;
    }
  }
}

void factorial() {
  fact[0] = 1;
  fact[1] = 1;
  rep(i, 2, MAX) {
    fact[i] = (fact[i - 1] * i);
    fact[i] %= M;
  }
}
ll bit[N];
ll seg[4 * N];
ll lazy[4 * N];
void pre() {
  bit[0] = 1;
  rep(i, 1, N) {
    bit[i] = 10 * bit[i - 1] + 1;
    bit[i] %= M;
    // what_is(bit[i]);
  }
}
void display(ll ind, ll left, ll right) {
  if (lazy[ind] == -1)
    cout << "Val = " << seg[ind] << " at " << left << " to " << right << endl;
  else
    cout << "Val to be updated by " << lazy[ind] << " from " << left << " to "
         << right << endl;
}
void build(ll ind, ll left, ll right) {
  if (left > right)
    return;
  if (left == right) {
    seg[ind] = bit[right];
    if (left > 0)
      seg[ind] -= bit[left - 1];
    if (seg[ind] < 0)
      seg[ind] += M;
    lazy[ind] = -1;
    // display(ind,left,right);
    return;
  }
  ll mid = left + (right - left) / 2;
  build(2 * ind, left, mid);
  build(2 * ind + 1, mid + 1, right);
  seg[ind] = seg[2 * ind] + seg[2 * ind + 1];
  seg[ind] %= M;
  lazy[ind] = -1;
  // display(ind,left,right);
}
void update(ll ind, ll left, ll right, ll l, ll r, ll d) {
  if (left > right)
    return;
  if (lazy[ind] != -1) {
    ll temp = bit[right];
    if (left > 0)
      temp -= bit[left - 1];
    if (temp < 0)
      temp += M;
    seg[ind] = (temp * lazy[ind]) % M;
    if (left != right) {
      // cout<<"Transfering "<<lazy[ind]<<" to child of "<<left<<"
      // "<<right<<endl;
      lazy[2 * ind] = lazy[ind];
      lazy[2 * ind + 1] = lazy[ind];
    }
    lazy[ind] = -1;
  }
  if (r < left || right < l)
    return;
  if (l <= left && right <= r) {
    ll temp = bit[right];
    if (left > 0)
      temp -= bit[left - 1];
    if (temp < 0)
      temp += M;
    seg[ind] = (temp * d) % M;
    if (left != right) {
      // cout<<"Transfering "<<d<<" to child of "<<left<<" "<<right<<endl;
      lazy[2 * ind] = d;
      lazy[2 * ind + 1] = d;
    }
    lazy[ind] = -1;
    // cout<<"Inside range "<<endl;
    // display(ind,left,right);
    return;
  }
  ll mid = left + (right - left) / 2;
  update(2 * ind, left, mid, l, r, d);
  update(2 * ind + 1, mid + 1, right, l, r, d);
  seg[ind] = seg[2 * ind] + seg[2 * ind + 1];
  seg[ind] %= M;
  // display(ind,left,right);
  return;
}
ll get(ll ind, ll left, ll right, ll l, ll r) {
  if (left > right)
    return 0;
  if (lazy[ind] != -1) {
    ll temp = bit[right];
    if (left > 0)
      temp -= bit[left - 1];
    if (temp < 0)
      temp += M;
    seg[ind] = (temp * lazy[ind]) % M;
    if (left != right) {
      lazy[2 * ind] = lazy[ind];
      lazy[2 * ind + 1] = lazy[ind];
    }
    lazy[ind] = -1;
  }
  if (r < left || right < l)
    return 0;
  if (l <= left && right <= r) {
    return seg[ind];
  }
  ll mid = left + (right - left) / 2;

  seg[ind] =
      get(2 * ind, left, mid, l, r) + get(2 * ind + 1, mid + 1, right, l, r);
  seg[ind] %= M;
  return seg[ind];
}
void displayTree(ll ind, ll left, ll right) {
  if (left > right)
    return;
  if (left == right) {
    display(ind, left, right);
    return;
  }
  ll mid = left + (right - left) / 2;
  displayTree(2 * ind, left, mid);
  displayTree(2 * ind + 1, mid + 1, right);
  display(ind, left, right);
}
void solve(ll test) {
  ll n, q;
  cin >> n >> q;
  build(1, 0, n - 1);
  ll ans;
  // ans=get(1,0,n-1,0,n-1);
  // cout<<ans<<endl;
  while (q--) {

    ll l, r, d;
    cin >> l >> r >> d;
    l--;
    r--;
    ll nl = n - r - 1;
    ll nr = n - l - 1;
    swap(nr, nl);
    l = nr;
    r = nl;
    // what_is(l);
    // what_is(r);
    // cout<<"Updating to "<<d<<endl;
    update(1, 0, n - 1, l, r, d);
    // cout<<"After update "<<endl<<endl;
    // displayTree(1,0,n-1);
    // cout<<"Tree Finished "<<endl<<endl;
    ans = get(1, 0, n - 1, 0, n - 1);
    cout << ans << endl;
  }
}
int main() {
  io;
  // sieve();
  pre();
  // cout<<"Re is calculated "<<endl;
  // factorial();
#ifndef ONLINE_JUDGE
  freopen("input1.txt", "r", stdin);
  // freopen("output1.txt","w",stdout);
#endif

  ll t = 1;
  // cin >> t;
  fix(12);
  rep(q, 1, t + 1) {
    // cout<<"Test case : "<<q<<endl;
    solve(q);
    // solve2();
  }
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#include <queue>
using namespace std;

#define what_is(x) cout << #x << " is " << x << endl;
#define PI acos(-1)
#define hell 1000000007
#define HELL 998244353
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define fix(n) cout << fixed << setprecision(n)
#define mset(a, n) memset(a, n, sizeof a)
#define rep(i, a, b) for (__typeof((b)) i = (a); i < (b); i++)
#define repp(i, a, b, p) for (__typeof((b)) i = (a); i < (b); i += p)
#define ren(i, a, b) for (__typeof((a)) i = (a); i >= (b); i--)
#define renn(i, a, b, p) for(__typeof((a) i=(a);i>=(b);i-=p)
#define ADD(a, b, c) ((a) % c + (b) % c) % c
#define SUB(a, b, c) ((a) % c - (b) % c + c) % c
#define MUL(a, b, c) (((a) % c) * ((b) % c)) % c
#define lbd lower_bound
#define ubd upper_bound
#define ll long long
#define ld long double
#define pb push_back
#define fi first
#define se second
#define vll vector<ll>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define all(v) (v).begin(), (v).end()
#define sz(x) (ll) x.size()
// #define out(n) cout<<n<<" "
#define outl(n) cout << n << endl
#define bug(n)                                                                 \\
  {                                                                            \\
    outl(n);                                                                   \\
    return;                                                                    \\
  }
#define pii pair<int, int>
#define lim 9223372036854775806
#define ninf -9223372036854775802
#define M 998244353
#define eps 1e-9
#define MAX 2
#define N 12
#define db(a)                                                                  \\
  for (auto it : a) {                                                          \\
    cout << it << " ";                                                         \\
  }                                                                            \\
  cout << endl;
ll prime[MAX];
ll fact[MAX];

const long long INF64 = 1e18;
ll vpow(ll a, ll b, ll p) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  ll temp = vpow(a, b / 2, p);
  temp *= temp;
  temp %= p;
  if (b % 2 == 0)
    return temp;
  temp = temp * a;
  temp %= p;
  return temp;
}

ll gcd(ll a, ll b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}

ll inverseMod(ll a, ll b, ll p) {
  if (b == 0)
    return 1;
  ll temp = vpow(b, p - 2, p);
  ll ans = temp * a;
  ans %= p;
  return ans;
}
void sieve() {
  prime[0] = prime[1] = 1;
  for (ll i = 2; i < MAX; i++) {
    if (prime[i] == 0) {
      for (int j = i * 2; j < MAX; j += i) {
        if (prime[j] == 0) {
          prime[j] = i;
        }
      }
      prime[i] = i;
    }
  }
}

void factorial() {
  fact[0] = 1;
  fact[1] = 1;
  rep(i, 2, MAX) {
    fact[i] = (fact[i - 1] * i);
    fact[i] %= M;
  }
}
ll bit[N];
ll seg[4 * N];
ll lazy[4 * N];
void pre() {
  bit[0] = 1;
  rep(i, 1, N) {
    bit[i] = 10 * bit[i - 1] + 1;
    bit[i] %= M;
    // what_is(bit[i]);
  }
}
void display(ll ind, ll left, ll right) {
  if (lazy[ind] == -1)
    cout << "Val = " << seg[ind] << " at " << left << " to " << right << endl;
  else
    cout << "Val to be updated by " << lazy[ind] << " from " << left << " to "
         << right << endl;
}
void build(ll ind, ll left, ll right) {
  if (left > right)
    return;
  if (left == right) {
    seg[ind] = bit[right];
    if (left > 0)
      seg[ind] -= bit[left - 1];
    if (seg[ind] < 0)
      seg[ind] += M;
    lazy[ind] = -1;
    // display(ind,left,right);
    return;
  }
  ll mid = left + (right - left) / 2;
  build(2 * ind, left, mid);
  build(2 * ind + 1, mid + 1, right);
  seg[ind] = seg[2 * ind] + seg[2 * ind + 1];
  seg[ind] %= M;
  lazy[ind] = -1;
  // display(ind,left,right);
}
void update(ll ind, ll left, ll right, ll l, ll r, ll d) {
  if (left > right)
    return;
  if (lazy[ind] != -1) {
    ll temp = bit[right];
    if (left > 0)
      temp -= bit[left - 1];
    if (temp < 0)
      temp += M;
    seg[ind] = (temp * lazy[ind]) % M;
    if (left != right) {
      // cout<<"Transfering "<<lazy[ind]<<" to child of "<<left<<"
      // "<<right<<endl;
      lazy[2 * ind] = lazy[ind];
      lazy[2 * ind + 1] = lazy[ind];
    }
    lazy[ind] = -1;
  }
  if (r < left || right < l)
    return;
  if (l <= left && right <= r) {
    ll temp = bit[right];
    if (left > 0)
      temp -= bit[left - 1];
    if (temp < 0)
      temp += M;
    seg[ind] = (temp * d) % M;
    if (left != right) {
      // cout<<"Transfering "<<d<<" to child of "<<left<<" "<<right<<endl;
      lazy[2 * ind] = d;
      lazy[2 * ind + 1] = d;
    }
    lazy[ind] = -1;
    // cout<<"Inside range "<<endl;
    // display(ind,left,right);
    return;
  }
  ll mid = left + (right - left) / 2;
  update(2 * ind, left, mid, l, r, d);
  update(2 * ind + 1, mid + 1, right, l, r, d);
  seg[ind] = seg[2 * ind] + seg[2 * ind + 1];
  seg[ind] %= M;
  // display(ind,left,right);
  return;
}
ll get(ll ind, ll left, ll right, ll l, ll r) {
  if (left > right)
    return 0;
  if (lazy[ind] != -1) {
    ll temp = bit[right];
    if (left > 0)
      temp -= bit[left - 1];
    if (temp < 0)
      temp += M;
    seg[ind] = (temp * lazy[ind]) % M;
    if (left != right) {
      lazy[2 * ind] = lazy[ind];
      lazy[2 * ind + 1] = lazy[ind];
    }
    lazy[ind] = -1;
  }
  if (r < left || right < l)
    return 0;
  if (l <= left && right <= r) {
    return seg[ind];
  }
  ll mid = left + (right - left) / 2;

  seg[ind] =
      get(2 * ind, left, mid, l, r) + get(2 * ind + 1, mid + 1, right, l, r);
  seg[ind] %= M;
  return seg[ind];
}
void displayTree(ll ind, ll left, ll right) {
  if (left > right)
    return;
  if (left == right) {
    display(ind, left, right);
    return;
  }
  ll mid = left + (right - left) / 2;
  displayTree(2 * ind, left, mid);
  displayTree(2 * ind + 1, mid + 1, right);
  display(ind, left, right);
}
void solve(ll test) {
  ll n, q;
  cin >> n >> q;
  build(1, 0, n - 1);
  ll ans;
  // ans=get(1,0,n-1,0,n-1);
  // cout<<ans<<endl;
  while (q--) {

    ll l, r, d;
    cin >> l >> r >> d;
    l--;
    r--;
    ll nl = n - r - 1;
    ll nr = n - l - 1;
    swap(nr, nl);
    l = nr;
    r = nl;
    // what_is(l);
    // what_is(r);
    // cout<<"Updating to "<<d<<endl;
    update(1, 0, n - 1, l, r, d);
    // cout<<"After update "<<endl<<endl;
    // displayTree(1,0,n-1);
    // cout<<"Tree Finished "<<endl<<endl;
    ans = get(1, 0, n - 1, 0, n - 1);
    cout << ans << endl;
  }
}
int main() {
  io;
  // sieve();
  pre();
  // cout<<"Re is calculated "<<endl;
  // factorial();
#ifndef ONLINE_JUDGE
  freopen("input1.txt", "r", stdin);
  // freopen("output1.txt","w",stdout);
#endif

  ll t = 1;
  // cin >> t;
  fix(12);
  rep(q, 1, t + 1) {
    // cout<<"Test case : "<<q<<endl;
    solve(q);
    // solve2();
  }
  return 0;
}