Problem p02758 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;
typedef vector<pi> vpi;
typedef long double ld;
#define pb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ALL(x) x.begin(), x.end()
#define SZ(x) (ll) x.size()
#define f first
#define s second
#define MAXN 200010
const ll MOD = 1e9 + 7;
vpi V;
ll N, a, b;

bool cmp(pi a, pi b) { return a.f + a.s < b.f + b.s; }

struct node {
  int s, e, m, v;
  node *l, *r;
  node(int _s, int _e) : s(_s), e(_e) {
    m = (s + e) / 2;
    v = 0;
    if (s != e) {
      l = new node(s, m);
      r = new node(m + 1, e);
    }
  }
  void up(int x, int val) {
    if (s == e) {
      v = max(v, val);
      return;
    }
    if (x <= m)
      l->up(x, val);
    else
      r->up(x, val);
    v = max(l->v, r->v);
  }
  int query(int x, int y) {
    if (s == x && e == y)
      return v;
    if (y <= m)
      return l->query(x, y);
    if (x > m)
      return r->query(x, y);
    return max(l->query(x, m), r->query(m + 1, y));
  }
} *root;

vi des;
int lst[MAXN];
ll dp[MAXN];
vi AL[MAXN];

int main() {
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> a >> b;
    --b;
    V.pb(a, a + b);
    des.pb(a);
    des.pb(a + b);
  }
  sort(ALL(des));
  des.resize(unique(ALL(des)) - des.begin());
  for (auto &i : V) {
    int a = i.f;
    int b = i.s;
    i = mp(lb(ALL(des), a) - des.begin() + 1,
           lb(ALL(des), b) - des.begin() + 1);
  }
  root = new node(1, SZ(des));
  sort(ALL(V));
  // for (auto i:V)cout<<i.f<<' '<<i.s<<'\n';
  lst[SZ(V) - 1] = SZ(V) - 1;
  AL[V.back().f].pb(V.back().s);
  root->up(V.back().f, lst[SZ(V) - 1]);
  for (int i = SZ(V) - 2; i >= 0; --i) {
    int l = root->query(V[i].f, V[i].s);
    l = max(l, i);
    lst[i] = l;
    root->up(V[i].f, l);
    AL[V[i].f].pb(V[l].s);
  }

  dp[0] = 1;
  for (int i = 1; i <= SZ(des); ++i) {
    dp[i] += dp[i - 1];
    dp[i] %= MOD;
    // cout<<"AL: ";
    for (auto x : AL[i]) {
      // cout<<x<<' ';
      dp[x] += dp[i - 1];
      dp[x] %= MOD;
    }
    // cout<<'\n';
  }
  cout << dp[SZ(des)];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> pi;
typedef vector<pi> vpi;
typedef long double ld;
#define pb emplace_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define ALL(x) x.begin(), x.end()
#define SZ(x) (ll) x.size()
#define f first
#define s second
#define MAXN 400010
const ll MOD = 998244353;
vpi V;
ll N, a, b;

bool cmp(pi a, pi b) { return a.f + a.s < b.f + b.s; }

struct node {
  int s, e, m, v;
  node *l, *r;
  node(int _s, int _e) : s(_s), e(_e) {
    m = (s + e) / 2;
    v = 0;
    if (s != e) {
      l = new node(s, m);
      r = new node(m + 1, e);
    }
  }
  void up(int x, int val) {
    if (s == e) {
      v = max(v, val);
      return;
    }
    if (x <= m)
      l->up(x, val);
    else
      r->up(x, val);
    v = max(l->v, r->v);
  }
  int query(int x, int y) {
    if (s == x && e == y)
      return v;
    if (y <= m)
      return l->query(x, y);
    if (x > m)
      return r->query(x, y);
    return max(l->query(x, m), r->query(m + 1, y));
  }
} *root;

vi des;
int lst[MAXN];
ll dp[MAXN];
vi AL[MAXN];

int main() {
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> a >> b;
    --b;
    V.pb(a, a + b);
    des.pb(a);
    des.pb(a + b);
  }
  sort(ALL(des));
  des.resize(unique(ALL(des)) - des.begin());
  for (auto &i : V) {
    int a = i.f;
    int b = i.s;
    i = mp(lb(ALL(des), a) - des.begin() + 1,
           lb(ALL(des), b) - des.begin() + 1);
  }
  root = new node(1, SZ(des));
  sort(ALL(V));
  // for (auto i:V)cout<<i.f<<' '<<i.s<<'\n';
  lst[SZ(V) - 1] = SZ(V) - 1;
  AL[V.back().f].pb(V.back().s);
  root->up(V.back().f, lst[SZ(V) - 1]);
  for (int i = SZ(V) - 2; i >= 0; --i) {
    int l = root->query(V[i].f, V[i].s);
    l = max(l, i);
    lst[i] = l;
    root->up(V[i].f, l);
    AL[V[i].f].pb(V[l].s);
  }

  dp[0] = 1;
  for (int i = 1; i <= SZ(des); ++i) {
    dp[i] += dp[i - 1];
    dp[i] %= MOD;
    // cout<<"AL: ";
    for (auto x : AL[i]) {
      // cout<<x<<' ';
      dp[x] += dp[i - 1];
      dp[x] %= MOD;
    }
    // cout<<'\n';
  }
  cout << dp[SZ(des)];
}