Problem p02796 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int f[N], n, ans = 1, b[N], tot; // f[i]表示i必选的最大价值.
struct node {
  int x, y;
} a[N];
struct Tree {
  int l, r, dat;
#define l(x) t[x].l
#define r(x) t[x].r
#define dat(x) t[x].dat
} t[N << 3];
inline bool cmp(node x, node y) { return x.x < y.x; }
inline int raw(int x) { return lower_bound(b + 1, b + tot + 1, x) - b; }
inline void build(int p, int l, int r) {
  l(p) = l, r(p) = r;
  if (l == r)
    return;
  int mid = l + r >> 1;
  build(p << 1, l, mid);
  build(p << 1 | 1, mid + 1, r);
}
inline void alter(int p, int x, int k) {
  if (l(p) == r(p)) {
    dat(p) = max(dat(p), k);
    return;
  }
  int mid = l(p) + r(p) >> 1;
  if (x <= mid)
    alter(p << 1, x, k);
  else
    alter(p << 1 | 1, x, k);
  dat(p) = max(dat(p << 1), dat(p << 1 | 1));
}
inline int ask(int p, int l, int r) {
  if (l <= l(p) && r >= r(p))
    return dat(p);
  int mid = l(p) + r(p) >> 1;
  int ans = 0;
  if (l <= mid)
    ans = max(ans, ask(p << 1, l, r));
  if (r >= mid + 1)
    ans = max(ans, ask(p << 1 | 1, l, r));
  return ans;
}
int main() {
  // freopen("1.in","r",stdin);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    int x, l;
    cin >> x >> l;
    b[i << 1] = a[i].x = x - l;
    b[i << 1 | 1] = a[i].y = x + l;
  }
  sort(b + 1, b + (n << 1) + 1);
  tot = unique(b + 1, b + (n << 1) + 1) - b - 1;
  build(1, 1, tot);
  sort(a + 1, a + n + 1, cmp);
  f[1] = 1;
  alter(1, raw(a[1].y), 1);
  for (int i = 2; i <= n; ++i) {
    f[i] = ask(1, 1, raw(a[i].x)) + 1;
    alter(1, raw(a[i].y), f[i]);
    //		for(int j=1;j<i;++j) if(a[j].y<=a[i].x) f[i]=max(f[i],f[j]);
    //		f[i]++;
    ans = max(ans, f[i]);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int f[N], n, ans = 1, b[N << 1], tot; // f[i]表示i必选的最大价值.
struct node {
  int x, y;
} a[N];
struct Tree {
  int l, r, dat;
#define l(x) t[x].l
#define r(x) t[x].r
#define dat(x) t[x].dat
} t[N << 3];
inline bool cmp(node x, node y) { return x.x < y.x; }
inline int raw(int x) { return lower_bound(b + 1, b + tot + 1, x) - b; }
inline void build(int p, int l, int r) {
  l(p) = l, r(p) = r;
  if (l == r)
    return;
  int mid = l + r >> 1;
  build(p << 1, l, mid);
  build(p << 1 | 1, mid + 1, r);
}
inline void alter(int p, int x, int k) {
  if (l(p) == r(p)) {
    dat(p) = max(dat(p), k);
    return;
  }
  int mid = l(p) + r(p) >> 1;
  if (x <= mid)
    alter(p << 1, x, k);
  else
    alter(p << 1 | 1, x, k);
  dat(p) = max(dat(p << 1), dat(p << 1 | 1));
}
inline int ask(int p, int l, int r) {
  if (l <= l(p) && r >= r(p))
    return dat(p);
  int mid = l(p) + r(p) >> 1;
  int ans = 0;
  if (l <= mid)
    ans = max(ans, ask(p << 1, l, r));
  if (r >= mid + 1)
    ans = max(ans, ask(p << 1 | 1, l, r));
  return ans;
}
int main() {
  // freopen("1.in","r",stdin);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    int x, l;
    cin >> x >> l;
    b[i << 1] = a[i].x = x - l;
    b[i << 1 | 1] = a[i].y = x + l;
  }
  sort(b + 1, b + (n << 1) + 1);
  tot = unique(b + 1, b + (n << 1) + 1) - b - 1;
  build(1, 1, tot);
  sort(a + 1, a + n + 1, cmp);
  f[1] = 1;
  alter(1, raw(a[1].y), 1);
  for (int i = 2; i <= n; ++i) {
    f[i] = ask(1, 1, raw(a[i].x)) + 1;
    alter(1, raw(a[i].y), f[i]);
    //		for(int j=1;j<i;++j) if(a[j].y<=a[i].x) f[i]=max(f[i],f[j]);
    //		f[i]++;
    ans = max(ans, f[i]);
  }
  cout << ans << endl;
}