Problem p02758 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100010, P = 998244353;
struct Ro {
  int x, d;
} rb[MAXN];
int n, mx, eg, r[MAXN], hd[MAXN], ver[2 * MAXN], nx[2 * MAXN], dp[MAXN],
    fa[MAXN], val[MAXN * 4];
bool cmp(Ro a, Ro b) { return a.x < b.x; }
void modify(int p, int l, int r, int pos, int v) {
  if (l == r) {
    val[p] = v;
    return;
  }
  int mid = (l + r) >> 1;
  if (pos <= mid) {
    modify(p << 1, l, mid, pos, v);
  } else {
    modify((p << 1) | 1, mid + 1, r, pos, v);
  }
  val[p] = max(val[p << 1], val[(p << 1) | 1]);
  return;
}
int query(int p, int l, int r, int xl, int xr) {
  if (xr < l || r < xl) {
    return 0;
  }
  if (xl <= l && r <= xr) {
    return val[p];
  }
  int mid = (l + r) >> 1;
  return max(query(p << 1, l, mid, xl, xr),
             query((p << 1) | 1, mid + 1, r, xl, xr));
}
void add_edge(int x, int y) {
  ver[++eg] = y;
  nx[eg] = hd[x];
  hd[x] = eg;
  return;
}
void dfs(int x, int fa) {
  int res = 1;
  for (int i = hd[x]; i; i = nx[i]) {
    if (ver[i] == fa) {
      continue;
    }
    dfs(ver[i], x);
    res = (1ll * res * dp[ver[i]]) % P;
  }
  dp[x] = (res + 1) % P;
  return;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &rb[i].x, &rb[i].d);
  }
  sort(rb + 1, rb + n + 1, cmp);
  for (int i = n; i >= 1; i--) {
    int l = i, rr = n;
    while (l < rr) {
      int mid = (l + rr + 1) >> 1;
      if (rb[i].x + rb[i].d > rb[mid].x) {
        l = mid;
      } else {
        rr = mid - 1;
      }
    }
    r[i] = query(1, 1, n, i, l);
    if (!r[i]) {
      r[i] = i;
    }
    modify(1, 1, n, i, r[i]);
  }
  int p = n + 1;
  r[n + 1] = n;
  for (int i = 1; i <= n; i++) {
    while (r[i] > r[p]) {
      p = fa[p];
    }
    fa[i] = p;
    add_edge(i, p), add_edge(p, i);
    p = i;
  }
  dfs(n + 1, 0);
  printf("%d\n", (dp[n + 1] - 1 + P) % P);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200010, P = 998244353;
struct Ro {
  int x, d;
} rb[MAXN];
int n, mx, eg, r[MAXN], hd[MAXN], ver[2 * MAXN], nx[2 * MAXN], dp[MAXN],
    fa[MAXN], val[MAXN * 4];
bool cmp(Ro a, Ro b) { return a.x < b.x; }
void modify(int p, int l, int r, int pos, int v) {
  if (l == r) {
    val[p] = v;
    return;
  }
  int mid = (l + r) >> 1;
  if (pos <= mid) {
    modify(p << 1, l, mid, pos, v);
  } else {
    modify((p << 1) | 1, mid + 1, r, pos, v);
  }
  val[p] = max(val[p << 1], val[(p << 1) | 1]);
  return;
}
int query(int p, int l, int r, int xl, int xr) {
  if (xr < l || r < xl) {
    return 0;
  }
  if (xl <= l && r <= xr) {
    return val[p];
  }
  int mid = (l + r) >> 1;
  return max(query(p << 1, l, mid, xl, xr),
             query((p << 1) | 1, mid + 1, r, xl, xr));
}
void add_edge(int x, int y) {
  ver[++eg] = y;
  nx[eg] = hd[x];
  hd[x] = eg;
  return;
}
void dfs(int x, int fa) {
  int res = 1;
  for (int i = hd[x]; i; i = nx[i]) {
    if (ver[i] == fa) {
      continue;
    }
    dfs(ver[i], x);
    res = (1ll * res * dp[ver[i]]) % P;
  }
  dp[x] = (res + 1) % P;
  return;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &rb[i].x, &rb[i].d);
  }
  sort(rb + 1, rb + n + 1, cmp);
  for (int i = n; i >= 1; i--) {
    int l = i, rr = n;
    while (l < rr) {
      int mid = (l + rr + 1) >> 1;
      if (rb[i].x + rb[i].d > rb[mid].x) {
        l = mid;
      } else {
        rr = mid - 1;
      }
    }
    r[i] = query(1, 1, n, i, l);
    if (!r[i]) {
      r[i] = i;
    }
    modify(1, 1, n, i, r[i]);
  }
  int p = n + 1;
  r[n + 1] = n;
  for (int i = 1; i <= n; i++) {
    while (r[i] > r[p]) {
      p = fa[p];
    }
    fa[i] = p;
    add_edge(i, p), add_edge(p, i);
    p = i;
  }
  dfs(n + 1, 0);
  printf("%d\n", (dp[n + 1] - 1 + P) % P);
  return 0;
}