Problem p02758 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define N 500001
#define MOD 998244353
int n, m, t[N], mx[N], fa[N], cur, f[N];
map<int, int> id;
vector<int> d[N];
struct rua {
  int d, x, r;
  void read() {
    scanf("%d%d", &d, &x);
    id[d] = id[d + x - 1] = 1;
    r = d + x - 1;
  }
  bool operator<(const rua &t) { return d < t.d; }
} a[N];
#define ls x * 2
#define rs x * 2 + 1
void Build(int x, int l, int r) {
  if (l == r) {
    mx[x] = l;
    return;
  }
  int mid = l + r >> 1;
  Build(ls, l, mid);
  Build(rs, mid + 1, r);
  mx[x] = max(mx[ls], mx[rs]);
}
int getMax(int L, int R, int x, int l, int r) {
  if (L <= l && r <= R)
    return mx[x];
  int mid = l + r >> 1, res = 0;
  if (L <= mid)
    res = max(res, getMax(L, R, ls, l, mid));
  if (R > mid)
    res = max(res, getMax(L, R, rs, mid + 1, r));
  return res;
}
void Change(int i, int c, int x, int l, int r) {
  if (l == r) {
    mx[x] = c;
    return;
  }
  int mid = l + r >> 1;
  if (i <= mid)
    Change(i, c, ls, l, mid);
  else
    Change(i, c, rs, mid + 1, r);
  mx[x] = max(mx[ls], mx[rs]);
}
void dfs(int x) {
  f[x] = 1;
  for (auto y : d[x])
    dfs(y), f[x] = 1ll * f[x] * f[y] % MOD;
  if (x)
    f[x] = (f[x] + 1) % MOD;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    a[i].read();
  for (auto pi : id)
    id[pi.first] = ++m;
  sort(a + 1, a + n + 1);
  Build(1, 1, m);
  for (int i = n; i >= 1; i--)
    Change(id[a[i].d], a[i].r = getMax(id[a[i].d], id[a[i].r], 1, 1, m), 1, 1,
           m),
        a[i].d = id[a[i].d];
  stack<int> st;
  for (int i = 1; i <= n; i++) {
    while (!st.empty() && a[i].d > st.top())
      st.pop(), cur = fa[cur];
    d[cur].push_back(i);
    st.push(a[i].r);
    fa[i] = cur;
    cur = i;
  }
  dfs(0);
  printf("%d\n", f[0]);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define N 2000001
#define MOD 998244353
int n, m, t[N], mx[N], fa[N], cur, f[N];
map<int, int> id;
vector<int> d[N];
struct rua {
  int d, x, r;
  void read() {
    scanf("%d%d", &d, &x);
    id[d] = id[d + x - 1] = 1;
    r = d + x - 1;
  }
  bool operator<(const rua &t) { return d < t.d; }
} a[N];
#define ls x * 2
#define rs x * 2 + 1
void Build(int x, int l, int r) {
  if (l == r) {
    mx[x] = l;
    return;
  }
  int mid = l + r >> 1;
  Build(ls, l, mid);
  Build(rs, mid + 1, r);
  mx[x] = max(mx[ls], mx[rs]);
}
int getMax(int L, int R, int x, int l, int r) {
  if (L <= l && r <= R)
    return mx[x];
  int mid = l + r >> 1, res = 0;
  if (L <= mid)
    res = max(res, getMax(L, R, ls, l, mid));
  if (R > mid)
    res = max(res, getMax(L, R, rs, mid + 1, r));
  return res;
}
void Change(int i, int c, int x, int l, int r) {
  if (l == r) {
    mx[x] = c;
    return;
  }
  int mid = l + r >> 1;
  if (i <= mid)
    Change(i, c, ls, l, mid);
  else
    Change(i, c, rs, mid + 1, r);
  mx[x] = max(mx[ls], mx[rs]);
}
void dfs(int x) {
  f[x] = 1;
  for (auto y : d[x])
    dfs(y), f[x] = 1ll * f[x] * f[y] % MOD;
  if (x)
    f[x] = (f[x] + 1) % MOD;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    a[i].read();
  for (auto pi : id)
    id[pi.first] = ++m;
  sort(a + 1, a + n + 1);
  Build(1, 1, m);
  for (int i = n; i >= 1; i--)
    Change(id[a[i].d], a[i].r = getMax(id[a[i].d], id[a[i].r], 1, 1, m), 1, 1,
           m),
        a[i].d = id[a[i].d];
  stack<int> st;
  for (int i = 1; i <= n; i++) {
    while (!st.empty() && a[i].d > st.top())
      st.pop(), cur = fa[cur];
    d[cur].push_back(i);
    st.push(a[i].r);
    fa[i] = cur;
    cur = i;
  }
  dfs(0);
  printf("%d\n", f[0]);
}