Problem p02758 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
using namespace std;
typedef long long LL;
const int SIZEN = 200010;
const int mod = 998244353;
struct Robot {
  int x, d;
} r[SIZEN];
int R[SIZEN];
int N;
int tmp[SIZEN];
int cnt = 0;
int mx[SIZEN] = {0};
void change(int rt, int l, int r, int x, int y) {
  if (l == r) {
    mx[rt] = y;
    return;
  }
  int mid = (l + r) >> 1;
  if (x <= mid)
    change(rt << 1, l, mid, x, y);
  else
    change(rt << 1 | 1, mid + 1, r, x, y);
  mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]);
}
int query(int rt, int l, int r, int x, int y) {
  if (x <= l && y >= r) {
    return mx[rt];
  }
  int mid = (l + r) >> 1;
  if (y <= mid)
    return query(rt << 1, l, mid, x, y);
  if (x > mid)
    return query(rt << 1 | 1, mid + 1, r, x, y);
  return max(query(rt << 1, l, mid, x, y),
             query(rt << 1 | 1, mid + 1, r, x, y));
}
bool cmp(const Robot &A, const Robot &B) { return A.x < B.x; }
int f[SIZEN];
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++)
    scanf("%d%d", &r[i].x, &r[i].d), tmp[++cnt] = r[i].x;
  sort(tmp + 1, tmp + 1 + cnt);
  sort(r + 1, r + 1 + N, cmp);
  cnt = unique(tmp + 1, tmp + 1 + cnt) - tmp - 1;
  change(1, 1, N, lower_bound(tmp + 1, tmp + 1 + cnt, r[N].x) - tmp, N);
  R[N] = N;
  for (int i = N - 1; i >= 1; i--) {
    int tr = lower_bound(tmp + 1, tmp + 1 + cnt, r[i].x + r[i].d) - tmp - 1;
    int tl = lower_bound(tmp + 1, tmp + 1 + cnt, r[i].x) - tmp;
    if (tl > tr) {
      R[i] = tr;
    } else {
      R[i] = max(tr, query(1, 1, N, tl, tr));
    }
    change(1, 1, N, tl, R[i]);
  }
  // for(int i = 1;i <= N;i++)printf("%d ",R[i]);printf("\n");
  f[N + 1] = 1;
  for (int i = N; i >= 1; i--) {
    f[i] = (f[i + 1] + f[R[i] + 1]) % mod;
  }
  printf("%d\n", f[1]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
using namespace std;
typedef long long LL;
const int SIZEN = 200010;
const int mod = 998244353;
struct Robot {
  int x, d;
} r[SIZEN];
int R[SIZEN];
int N;
int tmp[SIZEN];
int cnt = 0;
int mx[SIZEN << 2] = {0};
void change(int rt, int l, int r, int x, int y) {
  if (l == r) {
    mx[rt] = y;
    return;
  }
  int mid = (l + r) >> 1;
  if (x <= mid)
    change(rt << 1, l, mid, x, y);
  else
    change(rt << 1 | 1, mid + 1, r, x, y);
  mx[rt] = max(mx[rt << 1], mx[rt << 1 | 1]);
}
int query(int rt, int l, int r, int x, int y) {
  if (x <= l && y >= r) {
    return mx[rt];
  }
  int mid = (l + r) >> 1;
  if (y <= mid)
    return query(rt << 1, l, mid, x, y);
  if (x > mid)
    return query(rt << 1 | 1, mid + 1, r, x, y);
  return max(query(rt << 1, l, mid, x, y),
             query(rt << 1 | 1, mid + 1, r, x, y));
}
bool cmp(const Robot &A, const Robot &B) { return A.x < B.x; }
int f[SIZEN];
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; i++)
    scanf("%d%d", &r[i].x, &r[i].d), tmp[++cnt] = r[i].x;
  sort(tmp + 1, tmp + 1 + cnt);
  sort(r + 1, r + 1 + N, cmp);
  cnt = unique(tmp + 1, tmp + 1 + cnt) - tmp - 1;
  change(1, 1, N, lower_bound(tmp + 1, tmp + 1 + cnt, r[N].x) - tmp, N);
  R[N] = N;
  for (int i = N - 1; i >= 1; i--) {
    int tr = lower_bound(tmp + 1, tmp + 1 + cnt, r[i].x + r[i].d) - tmp - 1;
    int tl = lower_bound(tmp + 1, tmp + 1 + cnt, r[i].x) - tmp;
    if (tl > tr) {
      R[i] = tr;
    } else {
      R[i] = max(tr, query(1, 1, N, tl, tr));
    }
    change(1, 1, N, tl, R[i]);
  }
  // for(int i = 1;i <= N;i++)printf("%d ",R[i]);printf("\n");
  f[N + 1] = 1;
  for (int i = N; i >= 1; i--) {
    f[i] = (f[i + 1] + f[R[i] + 1]) % mod;
  }
  printf("%d\n", f[1]);
  return 0;
}