Problem p02874 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> P;
typedef vector<P> vpl;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int mod = 1e9 + 7;
const int MAX = 510000;
const int V = 100005;
ll dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};
ll dx[8] = {0, 1, 0, -1, 1, -1, -1, 1};
const double pi = acos(-1);
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <typename T1, typename T2> inline void print2(T1 a, T2 b) {
  cout << a << " " << b << endl;
}

struct lr {
  ll l;
  ll r;
  bool operator<(const lr &t) const { return l == t.l ? r < t.r : l > t.l; }
};

int main() {
  ll n;
  cin >> n;
  vector<struct lr> v(n);
  ll ans = 0;
  rep(i, n) {
    cin >> v[i].l >> v[i].r;
    ans = max(v[i].r - v[i].l + 1, ans);
  }
  sort(all(v));
  multiset<ll> fil, fir, sel, ser;
  rep(i, n) {
    sel.insert(v[i].l);
    ser.insert(v[i].r);
  }
  rep(i, n - 1) {
    auto itrl = sel.find(v[i].l);
    auto itrr = ser.find(v[i].r);
    fil.insert(v[i].l);
    fir.insert(v[i].r);
    sel.erase(itrl);
    ser.erase(itrr);
    auto itr = --fil.end();
    auto itr2 = --sel.end();
    ans = max(ans, *fir.begin() - *itr + 1 + *ser.begin() - *itr2 + 1);
    // cout << *fir.begin() << " " << *ser.begin() << endl;
  }
  for (ll i = n - 2; i >= 1; i--) {
    auto itrl = fil.find(v[i].l);
    auto itrr = fir.find(v[i].r);
    fil.insert(v[i + 1].l);
    fir.insert(v[i + 1].r);
    sel.erase(itrl);
    ser.erase(itrr);
    auto itr = --fil.end();
    ans = max(ans, *fir.begin() - *itr + 1 + v[i].r - v[i].l + 1);
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>

using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef pair<ll, ll> P;
typedef vector<P> vpl;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int mod = 1e9 + 7;
const int MAX = 510000;
const int V = 100005;
ll dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};
ll dx[8] = {0, 1, 0, -1, 1, -1, -1, 1};
const double pi = acos(-1);
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <typename T1, typename T2> inline void print2(T1 a, T2 b) {
  cout << a << " " << b << endl;
}

struct lr {
  ll l;
  ll r;
  bool operator<(const lr &t) const { return l == t.l ? r < t.r : l > t.l; }
};

int main() {
  ll n;
  cin >> n;
  vector<struct lr> v(n);
  ll ans = 0;
  rep(i, n) {
    cin >> v[i].l >> v[i].r;
    ans = max(v[i].r - v[i].l + 1, ans);
  }
  sort(all(v));
  multiset<ll> fil, fir, sel, ser;
  rep(i, n) {
    sel.insert(v[i].l);
    ser.insert(v[i].r);
  }
  rep(i, n - 1) {
    auto itrl = sel.find(v[i].l);
    auto itrr = ser.find(v[i].r);
    fil.insert(v[i].l);
    fir.insert(v[i].r);
    sel.erase(itrl);
    ser.erase(itrr);
    auto itr = --fil.end();
    auto itr2 = --sel.end();
    ans = max(ans, *fir.begin() - *itr + 1 + *ser.begin() - *itr2 + 1);
    // cout << *fir.begin() << " " << *ser.begin() << endl;
  }
  for (ll i = n - 2; i >= 1; i--) {
    auto itrl = fil.find(v[i].l);
    auto itrr = fir.find(v[i].r);
    fil.insert(v[i + 1].l);
    fir.insert(v[i + 1].r);
    fil.erase(itrl);
    fir.erase(itrr);
    auto itr = --fil.end();
    ans = max(ans, *fir.begin() - *itr + 1 + v[i].r - v[i].l + 1);
  }
  cout << ans << endl;
}