Problem p02874 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define P pair<int, int>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
constexpr int INF = 1000000000000000000;
constexpr int mod = 1000000007;
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int kaijo[2000010];
struct edge {
  int to, cost;
};
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
bool prime(int a) {
  if (a == 1)
    return false;
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}
void init_fact() {
  kaijo[0] = 1;
  for (int i = 1; i <= 2000000; i++) {
    kaijo[i] = kaijo[i - 1] * i;
    kaijo[i] %= mod;
  }
}
int modpow(int a, int b) {
  if (b == 0)
    return 1;
  if (b % 2)
    return modpow(a, b - 1) * a % mod;
  int memo = modpow(a, b / 2);
  return memo * memo % mod;
}
int comb(int a, int b) {
  if (!kaijo[0])
    init_fact();
  return kaijo[a] * modpow(kaijo[a - b], mod - 2) % mod *
         modpow(kaijo[b], mod - 2) % mod;
}
int inv(int x) {
  x = modpow(x, mod - 2);
  return x;
}
bool kosa(double ax, double ay, double bx, double by, double cx, double cy,
          double dx, double dy) {
  double ta = (cx - dx) * (ay - cy) + (cy - dy) * (cx - ax);
  double tb = (cx - dx) * (by - cy) + (cy - dy) * (cx - bx);
  double tc = (ax - bx) * (cy - ay) + (ay - by) * (ax - cx);
  double td = (ax - bx) * (dy - ay) + (ay - by) * (ax - dx);
  return tc * td < 0 && ta * tb < 0;
}
int n, ans;
P p[100010], R[100010];
struct LmaQ {
  int siz = 1;
  vector<int> dat;
  LmaQ(int n) {
    while (siz < n)
      siz *= 2;
    dat.resize(siz * 2 - 1, 0);
    rep(i, n) dat[i + siz - 1] = p[i].first;
    for (int i = siz - 2; i >= 0; i--)
      dat[i] = max(dat[i * 2 + 1], dat[i * 2 + 2]);
  }
  int query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r == -1)
      r = siz;
    if (a <= l && r <= b)
      return dat[k];
    if (r <= a || b <= l)
      return 0;
    int vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
    int vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
};
struct RmiQ {
  int siz = 1;
  vector<int> dat;
  RmiQ(int n) {
    while (siz < n)
      siz *= 2;
    dat.resize(siz * 2 - 1, INF);
    rep(i, n) dat[i + siz - 1] = p[i].second;
    for (int i = siz - 2; i >= 0; i--)
      dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]);
  }
  int query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r == -1)
      r = siz;
    if (a <= l && r <= b)
      return dat[k];
    if (r <= a || b <= l)
      return INF;
    int vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
    int vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
};
signed main() {
  cin >> n;
  rep(i, n) {
    cin >> p[i].first >> p[i].second;
    p[i].second++;
    R[i] = {p[i].second, i};
  }
  assert(n != 2);
  sort(p, p + n);
  sort(R, R + n);
  rep(i, n) {
    int A = p[i].second - p[i].first;
    int Lmax = (i == n - 1 ? p[n - 2].first : p[n - 1].first);
    int Rmin = (i == R[0].second ? R[1].first : R[0].first);
    int B = max(0ll, Rmin - Lmax);
    chmax(ans, A + B);
  }
  LmaQ L(n);
  RmiQ R(n);
  rep(i, n - 1) {
    int A = R.query(0, i + 1) - L.query(0, i + 1);
    int B = R.query(i + 1, n) - L.query(i + 1, n);
    chmax(A, 0ll);
    chmax(B, 0ll);
    chmax(ans, A + B);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define P pair<int, int>
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}
constexpr int INF = 1000000000000000000;
constexpr int mod = 1000000007;
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int kaijo[2000010];
struct edge {
  int to, cost;
};
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int lcm(int a, int b) { return a / gcd(a, b) * b; }
bool prime(int a) {
  if (a == 1)
    return false;
  for (int i = 2; i * i <= a; i++) {
    if (a % i == 0)
      return false;
  }
  return true;
}
void init_fact() {
  kaijo[0] = 1;
  for (int i = 1; i <= 2000000; i++) {
    kaijo[i] = kaijo[i - 1] * i;
    kaijo[i] %= mod;
  }
}
int modpow(int a, int b) {
  if (b == 0)
    return 1;
  if (b % 2)
    return modpow(a, b - 1) * a % mod;
  int memo = modpow(a, b / 2);
  return memo * memo % mod;
}
int comb(int a, int b) {
  if (!kaijo[0])
    init_fact();
  return kaijo[a] * modpow(kaijo[a - b], mod - 2) % mod *
         modpow(kaijo[b], mod - 2) % mod;
}
int inv(int x) {
  x = modpow(x, mod - 2);
  return x;
}
bool kosa(double ax, double ay, double bx, double by, double cx, double cy,
          double dx, double dy) {
  double ta = (cx - dx) * (ay - cy) + (cy - dy) * (cx - ax);
  double tb = (cx - dx) * (by - cy) + (cy - dy) * (cx - bx);
  double tc = (ax - bx) * (cy - ay) + (ay - by) * (ax - cx);
  double td = (ax - bx) * (dy - ay) + (ay - by) * (ax - dx);
  return tc * td < 0 && ta * tb < 0;
}
int n, ans;
P p[100010], R[100010];
struct LmaQ {
  int siz = 1;
  vector<int> dat;
  LmaQ(int n) {
    while (siz < n)
      siz *= 2;
    dat.resize(siz * 2 - 1, 0);
    rep(i, n) dat[i + siz - 1] = p[i].first;
    for (int i = siz - 2; i >= 0; i--)
      dat[i] = max(dat[i * 2 + 1], dat[i * 2 + 2]);
  }
  int query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r == -1)
      r = siz;
    if (a <= l && r <= b)
      return dat[k];
    if (r <= a || b <= l)
      return 0;
    int vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
    int vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return max(vl, vr);
  }
};
struct RmiQ {
  int siz = 1;
  vector<int> dat;
  RmiQ(int n) {
    while (siz < n)
      siz *= 2;
    dat.resize(siz * 2 - 1, INF);
    rep(i, n) dat[i + siz - 1] = p[i].second;
    for (int i = siz - 2; i >= 0; i--)
      dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]);
  }
  int query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r == -1)
      r = siz;
    if (a <= l && r <= b)
      return dat[k];
    if (r <= a || b <= l)
      return INF;
    int vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
    int vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
};
signed main() {
  cin >> n;
  rep(i, n) {
    cin >> p[i].first >> p[i].second;
    p[i].second++;
    R[i] = {p[i].second, i};
  }
  if (n == 2) {
    int A = max(0ll, p[0].second - p[0].first);
    int B = max(0ll, p[1].second - p[1].first);
    cout << A + B << endl;
    return 0;
  }
  sort(p, p + n);
  sort(R, R + n);
  rep(i, n) {
    int A = p[i].second - p[i].first;
    int Lmax = (i == n - 1 ? p[n - 2].first : p[n - 1].first);
    int Rmin = (i == R[0].second ? R[1].first : R[0].first);
    int B = max(0ll, Rmin - Lmax);
    chmax(ans, A + B);
  }
  LmaQ L(n);
  RmiQ R(n);
  rep(i, n - 1) {
    int A = R.query(0, i + 1) - L.query(0, i + 1);
    int B = R.query(i + 1, n) - L.query(i + 1, n);
    chmax(A, 0ll);
    chmax(B, 0ll);
    chmax(ans, A + B);
  }
  cout << ans << endl;
  return 0;
}