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

#include <iostream>

using namespace std;
typedef long long ll;
const int mod = 998244353;
const int N = 200010;
ll lazy[N << 2], tr[N << 2];
ll s[N];
ll sum[N];
void pushup(int l, int mid, int r, int u) {
  tr[u] = (tr[u << 1 | 1] + 1ll * s[r - mid] * tr[u << 1] % mod) % mod;
}
void init(int l, int r, int u) {
  if (l == r) {
    tr[u] = 1;
    return;
  }
  int mid = l + r >> 1;
  init(l, mid, u << 1);
  init(mid + 1, r, u << 1 | 1);
  pushup(l, mid, r, u);
}
void pushdown(int l, int r, int u) {
  int mid = l + r >> 1;
  if (lazy[u]) {
    tr[u << 1] = (1ll * sum[mid - l] * lazy[u]) % mod;
    tr[u << 1 | 1] = (1ll * sum[r - mid - 1] * lazy[u]) % mod;
    lazy[u << 1] = lazy[u << 1 | 1] = lazy[u];
    lazy[u] = 0;
  }
}
void update(int l, int r, int a, int b, int d, int u) {
  if (l <= a && b <= r) {
    lazy[u] = d;
    tr[u] = (sum[b - a] * d) % mod;
    //        cout<<a<<' '<<b<<' '<<tr[u]<<endl;
    return;
  }
  pushdown(a, b, u);
  int mid = a + b >> 1;
  if (l <= mid)
    update(l, r, a, mid, d, u << 1);
  if (r > mid)
    update(l, r, mid + 1, b, d, u << 1 | 1);
  pushup(a, mid, b, u);
}
int main() {
  s[0] = sum[0] = 1;
  int n, q;
  cin >> n >> q;

  for (int i = 1; i <= n; ++i)
    s[i] = 1ll * s[i - 1] * 10 % mod;
  for (int i = 1; i <= n; ++i)
    sum[i] = (sum[i - 1] + s[i]) % mod;
  init(1, n, 1);
  for (int i = 1; i <= q; ++i) {
    int l, r, d;
    cin >> l >> r >> d;
    //        cout<<l<<' '<<r<<endl;
    update(l, r, 1, n, d, 1);
    //		cout<<query(1,4,1,n,1)<<endl;
    cout << tr[1] << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <unordered_map>
using namespace std;
const int N = 100010;
unordered_map<int, int> mp;
int A[N];
int B[N];
int id[N];
int tr[N << 2];
void pushup(int u) { tr[u] = max(tr[u << 1], tr[u << 1 | 1]); }
void update(int l, int r, int a, int b, int d, int u) {
  if (l <= a && b <= r) {
    tr[u] = max(tr[u], d);
    return;
  }
  int mid = a + b >> 1;
  if (l < mid)
    update(l, r, a, mid, d, u << 1);
  if (r >= mid)
    update(l, r, mid + 1, r, d, u << 1 | 1);
  pushup(u);
}
int query(int l, int r, int a, int b, int u) {
  if (l <= a && b <= r) {
    return tr[u];
  }
  int mid = a + b >> 1;
  int maxv = 0;
  if (l < mid)
    maxv = max(maxv, query(l, r, a, mid, u << 1));
  if (r >= mid)
    maxv = max(maxv, query(l, r, mid + 1, r, u << 1 | 1));
  return maxv;
}
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> A[i];
    B[i] = A[i];
  }
  sort(B + 1, B + n + 1);
  int tot = 1;
  for (int i = 2; i <= n; ++i) {
    if (B[tot] != B[i])
      B[++tot] = B[i];
  }
  int num = 0;
  for (int i = 1; i <= tot; ++i)
    mp[B[i]] = ++num;
  for (int i = 1; i <= n; ++i) {
    id[i] = mp[A[i]];
  }
  for (int i = 1; i <= n; ++i) {
    int tl = A[i] - k, ttr = A[i] + k;
    int kl, kr;
    int l = 1, r = tot;
    while (l < r) {
      int mid = l + r >> 1;
      if (B[mid] >= tl)
        r = mid;
      else
        l = mid + 1;
    }
    kl = l;
    l = 1, r = tot;
    while (l < r) {
      int mid = l + r + 1 >> 1;
      if (B[mid] <= ttr)
        l = mid;
      else
        r = mid - 1;
    }
    kr = l;
    int maxv = query(1, tot, kl, kr, 1);
    update(1, tot, id[i], id[i], maxv, 1);
    cout << tr[1] << endl;
  }
}