Problem p02556 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)

namespace IO {
#define gc getchar()
template <typename T> inline void read(T &x) {
  x = 0;
  bool f = 1;
  char ch;
  for (ch = gc; ch < '0' || '9' < ch; ch = gc)
    if (ch == '-')
      f ^= 1;
  for (; '0' <= ch && ch <= '9'; ch = gc)
    x = (x << 3) + (x << 1) + (ch ^ 48);
  x = f ? x : -x;
}
#undef gc
} // namespace IO

typedef long long ll;
typedef pair<ll, ll> pii;

const int MAXN = 2e5 + 10;

int n;
ll ans, tmp[MAXN], num[MAXN];
pii arr[MAXN];

struct BIT {
  ll bit[MAXN + 100];
  void chkmin(ll &a, ll b) {
    if (a < b)
      a = b;
  }
  BIT() { rep(i, 0, MAXN) bit[i] = -INT_MAX; }
  void clear() { rep(i, 0, MAXN) bit[i] = -INT_MAX; }
  void add(int x, ll val) {
    for (; x < MAXN; x += x & -x)
      chkmin(bit[x], val);
  }
  ll query(int x) {
    ll ret = -INT_MAX;
    for (; x; x -= x & -x)
      chkmin(ret, bit[x]);
    return ret;
  }
  void radd(int x, ll val) {
    for (; x; x -= x & -x)
      chkmin(bit[x], val);
  }
  ll rquery(int x) {
    ll ret = -INT_MAX;
    for (; x < MAXN; x += x & -x)
      chkmin(ret, bit[x]);
    return ret;
  }
} bit1, bit2;

int main() {
  IO::read(n);
  rep(i, 1, n) IO::read(arr[i].first), IO::read(arr[i].second),
      tmp[i] = arr[i].second;
  sort(arr + 1, arr + n + 1), sort(tmp + 1, tmp + n + 1);
  int _n = unique(tmp + 1, tmp + n + 1) - tmp - 1;
  rep(i, 1, n) num[i] = *lower_bound(tmp + 1, tmp + _n + 1, arr[i].second);
  rep(i, 1, n) {
    if (i > 1)
      ans = max(ans, arr[i].first + max(arr[i].second + bit1.query(num[i]),
                                        bit2.rquery(num[i]) - arr[i].second));
    bit1.add(num[i], -(arr[i].first + arr[i].second)),
        bit2.radd(num[i], -arr[i].first + arr[i].second);
  }
  bit1.clear(), bit2.clear();
  per(i, n, 1) {
    if (i < n)
      ans = max(ans, -arr[i].first + max(arr[i].second + bit1.query(num[i]),
                                         bit2.rquery(num[i]) - arr[i].second));
    bit1.add(num[i], -(-arr[i].first + arr[i].second)),
        bit2.radd(num[i], arr[i].first + arr[i].second);
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = (a), i##end = (b); i <= i##end; ++i)
#define per(i, a, b) for (int i = (a), i##end = (b); i >= i##end; --i)

namespace IO {
#define gc getchar()
template <typename T> inline void read(T &x) {
  x = 0;
  bool f = 1;
  char ch;
  for (ch = gc; ch < '0' || '9' < ch; ch = gc)
    if (ch == '-')
      f ^= 1;
  for (; '0' <= ch && ch <= '9'; ch = gc)
    x = (x << 3) + (x << 1) + (ch ^ 48);
  x = f ? x : -x;
}
#undef gc
} // namespace IO

typedef long long ll;
typedef pair<ll, ll> pii;

const int MAXN = 2e5 + 10;

int n;
ll ans, tmp[MAXN], num[MAXN];
pii arr[MAXN];

struct BIT {
  ll bit[MAXN + 100];
  void chkmin(ll &a, ll b) {
    if (a < b)
      a = b;
  }
  BIT() { rep(i, 0, MAXN) bit[i] = -INT_MAX; }
  void clear() { rep(i, 0, MAXN) bit[i] = -INT_MAX; }
  void add(int x, ll val) {
    for (; x < MAXN; x += x & -x)
      chkmin(bit[x], val);
  }
  ll query(int x) {
    ll ret = -INT_MAX;
    for (; x; x -= x & -x)
      chkmin(ret, bit[x]);
    return ret;
  }
  void radd(int x, ll val) {
    for (; x; x -= x & -x)
      chkmin(bit[x], val);
  }
  ll rquery(int x) {
    ll ret = -INT_MAX;
    for (; x < MAXN; x += x & -x)
      chkmin(ret, bit[x]);
    return ret;
  }
} bit1, bit2;

int main() {
  IO::read(n);
  rep(i, 1, n) IO::read(arr[i].first), IO::read(arr[i].second),
      tmp[i] = arr[i].second;
  sort(arr + 1, arr + n + 1), sort(tmp + 1, tmp + n + 1);
  int _n = unique(tmp + 1, tmp + n + 1) - tmp - 1;
  rep(i, 1, n) num[i] = lower_bound(tmp + 1, tmp + _n + 1, arr[i].second) - tmp;
  rep(i, 1, n) {
    if (i > 1)
      ans = max(ans, arr[i].first + max(arr[i].second + bit1.query(num[i]),
                                        bit2.rquery(num[i]) - arr[i].second));
    bit1.add(num[i], -(arr[i].first + arr[i].second)),
        bit2.radd(num[i], -arr[i].first + arr[i].second);
  }
  bit1.clear(), bit2.clear();
  per(i, n, 1) {
    if (i < n)
      ans = max(ans, -arr[i].first + max(arr[i].second + bit1.query(num[i]),
                                         bit2.rquery(num[i]) - arr[i].second));
    bit1.add(num[i], -(-arr[i].first + arr[i].second)),
        bit2.radd(num[i], arr[i].first + arr[i].second);
  }
  printf("%lld\n", ans);
  return 0;
}