Problem p02758 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define int long long
#define REP(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define mp make_pair
#define MOD 998244353
#define IDE -1

using namespace std;

const int MAX_N = 1 << 17;
int n_max, n, dat[2 * MAX_N - 1];

void init(int n) {
  n_max = 1;
  while (n_max < n)
    n_max *= 2;
  REP(i, 2 * n_max - 1) dat[i] = IDE;
}

int segfunc(int l, int m) { return max(l, m); }

// k番目の値(0-indexed)をaに変更
void update(int k, int a) {
  k += n_max - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = segfunc(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

int query(int a, int b, int k, int l, int r) {
  // cout << "query: " << a << " " << b << " " << k << " " <<  l << " " << r <<
  // "\n";
  if (r <= a || b <= l) {
    // cout << "-1\n";
    return IDE;
  }
  if (a <= l && r <= b) {
    // cout << dat[k] << "\n";
    return dat[k];
  } else {
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return segfunc(vl, vr);
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  init(n);
  vector<pair<int, int>> robots;
  REP(i, n) {
    int xt, dt;
    cin >> xt >> dt;
    robots.pb(mp(xt, dt));
  }
  sort(robots.begin(), robots.end());
  vector<int> x(n), d(n);
  REP(i, n) {
    x[i] = robots[i].first;
    d[i] = robots[i].second;
  }
  REP(i, n) {
    int j = i;
    update(j, j);
  }
  for (int i = n - 1; i >= 0; i--) {
    int idx_direct = lower_bound(x.begin(), x.end(), x[i] + d[i]) - x.begin();
    // cout << "idx_direct " << i << ":" << idx_direct << "\n";
    int q = query(i + 1, idx_direct, 0, 0, n_max);
    int upval = max(i, q);
    if (upval >= 0)
      update(i, upval);

    for (int j = 0; j < n + n_max - 1; j++) {
      // cout << dat[j] << " ";
    }
    // cout << "\n";
  }

  vector<int> dp(n + 1);
  dp[n] = 1;
  for (int i = n - 1; i >= 0; i--) {
    dp[i] = (dp[i + 1] + dp[dat[i + n_max - 1] + 1]) % MOD;
  }
  cout << dp[0] << "\n";
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define int long long
#define REP(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define mp make_pair
#define MOD 998244353
#define IDE -1

using namespace std;

const int MAX_N = 1 << 20;
int n_max, n, dat[2 * MAX_N - 1];

void init(int n) {
  n_max = 1;
  while (n_max < n)
    n_max *= 2;
  REP(i, 2 * n_max - 1) dat[i] = IDE;
}

int segfunc(int l, int m) { return max(l, m); }

// k番目の値(0-indexed)をaに変更
void update(int k, int a) {
  k += n_max - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = segfunc(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

int query(int a, int b, int k, int l, int r) {
  // cout << "query: " << a << " " << b << " " << k << " " <<  l << " " << r <<
  // "\n";
  if (r <= a || b <= l) {
    // cout << "-1\n";
    return IDE;
  }
  if (a <= l && r <= b) {
    // cout << dat[k] << "\n";
    return dat[k];
  } else {
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return segfunc(vl, vr);
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n;
  init(n);
  vector<pair<int, int>> robots;
  REP(i, n) {
    int xt, dt;
    cin >> xt >> dt;
    robots.pb(mp(xt, dt));
  }
  sort(robots.begin(), robots.end());
  vector<int> x(n), d(n);
  REP(i, n) {
    x[i] = robots[i].first;
    d[i] = robots[i].second;
  }
  REP(i, n) {
    int j = i;
    update(j, j);
  }
  for (int i = n - 1; i >= 0; i--) {
    int idx_direct = lower_bound(x.begin(), x.end(), x[i] + d[i]) - x.begin();
    // cout << "idx_direct " << i << ":" << idx_direct << "\n";
    int q = query(i + 1, idx_direct, 0, 0, n_max);
    int upval = max(i, q);
    if (upval >= 0)
      update(i, upval);

    for (int j = 0; j < n + n_max - 1; j++) {
      // cout << dat[j] << " ";
    }
    // cout << "\n";
  }

  vector<int> dp(n + 1);
  dp[n] = 1;
  for (int i = n - 1; i >= 0; i--) {
    dp[i] = (dp[i + 1] + dp[dat[i + n_max - 1] + 1]) % MOD;
  }
  cout << dp[0] << "\n";
  return 0;
}