Problem p02758 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define F first
#define S second
#define mkp make_pair
#define pii pair<int, int>
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 5;
const int mod = 998244353;
ll tree[maxn];
void build(int o, int l, int r) {
  tree[o] = 0;
  if (l == r) {
    return;
  } else {
    int mid = l + r >> 1;
    build(o << 1, l, mid);
    build(o << 1 | 1, mid + 1, r);
    tree[o] = max(tree[o << 1], tree[o << 1 | 1]);
  }
}
ll query(int o, int l, int r, int x, int y) {
  if (x <= l && y >= r) {
    return tree[o];
  }
  int mid = l + r >> 1;
  ll ans = 0;
  if (mid >= x)
    ans = max(ans, query(o << 1, l, mid, x, y));
  if (mid < y)
    ans = max(ans, query(o << 1 | 1, mid + 1, r, x, y));
  return ans;
}
void update(int o, int l, int r, int p, ll v) {
  if (l == r) {
    tree[o] = v;
  } else {
    int mid = l + r >> 1;
    if (mid >= p)
      update(o << 1, l, mid, p, v);
    else if (mid < p)
      update(o << 1 | 1, mid + 1, r, p, v);
    tree[o] = max(tree[o << 1], tree[o << 1 | 1]);
  }
}
pair<ll, ll> a[maxn];
int L[maxn];
ll b[maxn];
ll dp[maxn];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld%lld", &a[i].first, &a[i].second);
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= n; i++)
    b[i] = a[i].first;
  build(1, 1, n);
  for (int i = n; i >= 1; i--) {
    int tmp = upper_bound(b + 1, b + 1 + n, a[i].first + a[i].second - 1) - b;
    if (tmp == i + 1)
      L[i] = i;
    else
      L[i] = query(1, 1, n, i + 1, tmp - 1);
    update(1, 1, n, i, L[i]);
  }
  //	for(int i=1;i<=n;i++)cout<<L[i]<<" ";cout<<endl;
  dp[n + 1] = 1;
  for (int i = n; i >= 1; i--) {
    dp[i] = dp[i + 1] + dp[L[i] + 1];
    dp[i] %= mod;
  }
  printf("%lld\n", dp[1]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define F first
#define S second
#define mkp make_pair
#define pii pair<int, int>
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 5;
const int mod = 998244353;
ll tree[maxn << 2];
void build(int o, int l, int r) {
  tree[o] = 0;
  if (l == r) {
    return;
  } else {
    int mid = l + r >> 1;
    build(o << 1, l, mid);
    build(o << 1 | 1, mid + 1, r);
    tree[o] = max(tree[o << 1], tree[o << 1 | 1]);
  }
}
ll query(int o, int l, int r, int x, int y) {
  if (x <= l && y >= r) {
    return tree[o];
  }
  int mid = l + r >> 1;
  ll ans = 0;
  if (mid >= x)
    ans = max(ans, query(o << 1, l, mid, x, y));
  if (mid < y)
    ans = max(ans, query(o << 1 | 1, mid + 1, r, x, y));
  return ans;
}
void update(int o, int l, int r, int p, ll v) {
  if (l == r) {
    tree[o] = v;
  } else {
    int mid = l + r >> 1;
    if (mid >= p)
      update(o << 1, l, mid, p, v);
    else if (mid < p)
      update(o << 1 | 1, mid + 1, r, p, v);
    tree[o] = max(tree[o << 1], tree[o << 1 | 1]);
  }
}
pair<ll, ll> a[maxn];
int L[maxn];
ll b[maxn];
ll dp[maxn];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld%lld", &a[i].first, &a[i].second);
  sort(a + 1, a + 1 + n);
  for (int i = 1; i <= n; i++)
    b[i] = a[i].first;
  build(1, 1, n);
  for (int i = n; i >= 1; i--) {
    int tmp = upper_bound(b + 1, b + 1 + n, a[i].first + a[i].second - 1) - b;
    if (tmp == i + 1)
      L[i] = i;
    else
      L[i] = query(1, 1, n, i + 1, tmp - 1);
    update(1, 1, n, i, L[i]);
  }
  //	for(int i=1;i<=n;i++)cout<<L[i]<<" ";cout<<endl;
  dp[n + 1] = 1;
  for (int i = n; i >= 1; i--) {
    dp[i] = dp[i + 1] + dp[L[i] + 1];
    dp[i] %= mod;
  }
  printf("%lld\n", dp[1]);
  return 0;
}