Problem p02758 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fo(i, a, b) for (int i = (a); i <= (b); ++i)
#define rv(i, a, b) for (int i = (a); i >= (b); --i)
#define ls (rt << 1)
#define rs (rt << 1 | 1)
using namespace std;
const int M = 220000;
typedef long long ll;
int n, m, k;
char s[M];
int c[M] = {0};
int nx[M];
struct node {
  int x, d;
  bool operator<(const node &b) { return x < b.x; }
} e[M];
int mx[M] = {0};
void upd(int rt, int l, int r, int p, int val) {
  if (l == r) {
    mx[rt] = val;
    return;
  }
  int mid = (l + r) >> 1;
  if (p <= mid)
    upd(ls, l, mid, p, val);
  else
    upd(rs, mid + 1, r, p, val);
  mx[rt] = max(mx[ls], mx[rs]);
}
int query(int rt, int l, int r, int L, int R) {
  if (l == L && r == R)
    return mx[rt];
  int mid = (l + r) >> 1;
  if (L > mid)
    return query(rs, mid + 1, r, L, R);
  if (R <= mid)
    return query(ls, l, mid, L, R);
  return max(query(ls, l, mid, L, mid), query(rs, mid + 1, r, mid + 1, R));
}
int dp[M] = {0};
const int md = 998244353;
int main() {
  cin >> n;
  fo(i, 1, n) { cin >> e[i].x >> e[i].d; }
  sort(e + 1, e + 1 + n);
  nx[n] = n;
  upd(1, 1, n, n, n);
  rv(i, n - 1, 1) {
    int lo = i, hi = n;
    int tar = e[i].x + e[i].d - 1;
    for (int mid = (lo + hi) >> 1; lo < hi; mid = (lo + hi) >> 1) {
      if (e[mid].x < tar)
        lo = mid + 1;
      else
        hi = mid;
    }
    if (lo > n)
      lo = n;
    if (e[lo].x > tar)
      --lo;
    nx[i] = query(1, 1, n, i, lo);
    nx[i] = max(nx[i], i);
    upd(1, 1, n, i, nx[i]);
  }
  dp[0] = 1;
  fo(i, 1, n) {
    dp[nx[i]] = (dp[nx[i]] + dp[i - 1]) % md;
    dp[i] = (dp[i] + dp[i - 1]) % md;
  }
  cout << dp[n] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define fo(i, a, b) for (int i = (a); i <= (b); ++i)
#define rv(i, a, b) for (int i = (a); i >= (b); --i)
#define ls (rt << 1)
#define rs (rt << 1 | 1)
using namespace std;
const int M = 220000;
typedef long long ll;
int n, m, k;
char s[M];
int c[M] = {0};
int nx[M];
struct node {
  int x, d;
  bool operator<(const node &b) { return x < b.x; }
} e[M];
int mx[M << 2] = {0};
void upd(int rt, int l, int r, int p, int val) {
  if (l == r) {
    mx[rt] = val;
    return;
  }
  int mid = (l + r) >> 1;
  if (p <= mid)
    upd(ls, l, mid, p, val);
  else
    upd(rs, mid + 1, r, p, val);
  mx[rt] = max(mx[ls], mx[rs]);
}
int query(int rt, int l, int r, int L, int R) {
  if (l == L && r == R)
    return mx[rt];
  int mid = (l + r) >> 1;
  if (L > mid)
    return query(rs, mid + 1, r, L, R);
  if (R <= mid)
    return query(ls, l, mid, L, R);
  return max(query(ls, l, mid, L, mid), query(rs, mid + 1, r, mid + 1, R));
}
int dp[M] = {0};
const int md = 998244353;
int main() {
  cin >> n;
  fo(i, 1, n) { cin >> e[i].x >> e[i].d; }
  sort(e + 1, e + 1 + n);
  nx[n] = n;
  upd(1, 1, n, n, n);
  rv(i, n - 1, 1) {
    int lo = i, hi = n;
    int tar = e[i].x + e[i].d - 1;
    for (int mid = (lo + hi) >> 1; lo < hi; mid = (lo + hi) >> 1) {
      if (e[mid].x < tar)
        lo = mid + 1;
      else
        hi = mid;
    }
    if (lo > n)
      lo = n;
    if (e[lo].x > tar)
      --lo;
    nx[i] = query(1, 1, n, i, lo);
    nx[i] = max(nx[i], i);
    upd(1, 1, n, i, nx[i]);
  }
  dp[0] = 1;
  fo(i, 1, n) {
    dp[nx[i]] = (dp[nx[i]] + dp[i - 1]) % md;
    dp[i] = (dp[i] + dp[i - 1]) % md;
  }
  cout << dp[n] << endl;
  return 0;
}