Problem p02758 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define repr(i, a, b) for (ll i = a; i >= b; i--)
const long long mod = 998244353;
// Range_Minimum_Query
//(0-indexed)
const int max_n = 1 << 17;
const long long int_max = 1e9 + 7;
int nn;
long long dat[2 * max_n - 1];
void rmq_init(int initn) {
  nn = 1;
  while (nn < initn)
    nn *= 2;
  for (int i = 0; i < 2 * nn - 1; i++)
    dat[i] = int_max;
}
// k番目の値(0-indexed)をaに変更
void update(int k, long long a) {
  k += nn - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

//[a,b)の最小値を求める
// query(a, b, 0, 0, n)で呼ぶ
long long query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return int_max;
  if (a <= l && r <= b)
    return dat[k];
  else {
    long long vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    long long vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
}

int main() {
  // your code goes here
  ll n, x, d, dp[200005] = {0}, sw[200005] = {0}, swm[200005] = {0}, pb = n - 1;
  vector<pair<ll, ll>> v;
  vector<ll> zh;
  cin >> n;
  rmq_init(n);
  rep(i, 0, n - 1) {
    cin >> x >> d;
    v.push_back({x, d});
    zh.push_back(x);
  }
  sort(v.begin(), v.end());
  sort(zh.begin(), zh.end());
  rep(i, 0, n - 1) {
    auto it = lower_bound(zh.begin(), zh.end(), v[i].first + v[i].second);
    it--;
    sw[i] = it - zh.begin();
    update(i, -sw[i]);
  }
  swm[n - 1] = n - 1;
  repr(i, n - 2, 0) {
    swm[i] = -query(i, sw[i] + 1, 0, 0, nn);
    update(i, -swm[i]);
  }
  dp[n] = 1;
  dp[n - 1] = 2;
  repr(i, n - 2, 0) { dp[i] = (dp[i + 1] + dp[swm[i] + 1]) % mod; }
  cout << dp[0] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (ll i = a; i <= b; i++)
#define repr(i, a, b) for (ll i = a; i >= b; i--)
const long long mod = 998244353;
// Range_Minimum_Query
//(0-indexed)
const int max_n = 300000;
const long long int_max = 1e9 + 7;
int nn;
long long dat[2 * max_n - 1];
void rmq_init(int initn) {
  nn = 1;
  while (nn < initn)
    nn *= 2;
  for (int i = 0; i < 2 * nn - 1; i++)
    dat[i] = int_max;
}
// k番目の値(0-indexed)をaに変更
void update(int k, long long a) {
  k += nn - 1;
  dat[k] = a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
  }
}

//[a,b)の最小値を求める
// query(a, b, 0, 0, n)で呼ぶ
long long query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return int_max;
  if (a <= l && r <= b)
    return dat[k];
  else {
    long long vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
    long long vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
}

int main() {
  // your code goes here
  ll n, x, d, dp[200005] = {0}, sw[200005] = {0}, swm[200005] = {0}, pb = n - 1;
  vector<pair<ll, ll>> v;
  vector<ll> zh;
  cin >> n;
  rmq_init(n);
  rep(i, 0, n - 1) {
    cin >> x >> d;
    v.push_back({x, d});
    zh.push_back(x);
  }
  sort(v.begin(), v.end());
  sort(zh.begin(), zh.end());
  rep(i, 0, n - 1) {
    auto it = lower_bound(zh.begin(), zh.end(), v[i].first + v[i].second);
    it--;
    sw[i] = it - zh.begin();
    update(i, -sw[i]);
  }
  swm[n - 1] = n - 1;
  repr(i, n - 2, 0) {
    swm[i] = -query(i, sw[i] + 1, 0, 0, nn);
    update(i, -swm[i]);
  }
  dp[n] = 1;
  dp[n - 1] = 2;
  repr(i, n - 2, 0) { dp[i] = (dp[i + 1] + dp[swm[i] + 1]) % mod; }
  cout << dp[0] << endl;
  return 0;
}