Language: C++

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

using ll = long long;
#define FOR(i, m, n) for (int i = (m); i < (n); i++)
#define FORR(i, m, n) for (int i = (m); i >= (n); i--)
#define REP(i, n) FOR(i, 0, (n))
#define REPR(i, n) FORR(i, (n) - 1, 0)
#define REP1(i, n) FOR(i, 1, (n) + 1)
#define ALL(c) (c).begin(), (c).end()
#define sz(v) (int)v.size()
template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b; return true;} return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b; return true;} return false;}
const int MOD = 1000000007;
const int INF = 1000000001;
const ll LINF = 1000000001000000001LL;
const char EOL = '\n';

void solve();

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(numeric_limits<double>::max_digits10);
    solve();
    return 0;
}

void solve() {
    int n;
    cin >> n;
    vector<pair<int, int>> v(n);
    vector<pair<int, int>> w(n);
    int l, r;
    int isomax = -1;
    int lmax = -1;
    int rmin = INF;
    REP(i, n) {
        cin >> l >> r;
        l--; r--;
		chmax(lmax, l);
		chmin(rmin, r);
        v[i] = make_pair(l, r);
        w[i] = make_pair(r, l);
        chmax(isomax, r - l + 1);
    }
    sort(ALL(v));
    vector<int> lmin(n);
    vector<int> rmin(n);
    int mn = INF;
    REP(i, n) {
        chmin(mn, v[i].second);
        lmin[i] = mn;
    }
    mn = INF;
    REPR(i, n) {
        chmin(mn, v[i].second);
        rmin[i] = mn;
    }
    int ans = 0;
    REP(i, n - 1) {
        int x = lmin[i] - v[i].first + 1;
        if (x <= 0) x = 0;
        int y = rmin[i + 1] - v[n - 1].first + 1;
        if (y <= 0) y = 0;
        chmax(ans, x + y);
    }

    // ?????
    sort(ALL(w), greater<pair<int, int>>());
    vector<int> lmax(n);
    vector<int> rmax(n);
    int mx = -1;
    REP(i, n) {
        chmax(mx, w[i].second);
        lmax[i] = mx;
    }
    mx = -1;
    REPR(i, n) {
        chmax(mx, w[i].second);
        rmax[i] = mx;
    }
    REP(i, n - 1) {
        int x = w[i].first - lmax[i] + 1;
        if (x <= 0) x = 0;
        int y = w[n - 1].first - rmax[i + 1] + 1;
        if (y <= 0) y = 0;
        chmax(ans, x + y);
    }

    cout << max(ans, isomax + max(0, rmin - lmax + 1)) << EOL;
}



Compiler output:
a.cc: In function 'void solve()':
a.cc:49:17: error: conflicting declaration 'std::vector<int> rmin'
   49 |     vector<int> rmin(n);
      |                 ^~~~
a.cc:37:9: note: previous declaration as 'int rmin'
   37 |     int rmin = INF;
      |         ^~~~
a.cc:58:13: error: invalid types 'int[int]' for array subscript
   58 |         rmin[i] = mn;
      |             ^
a.cc:64:21: error: invalid types 'int[int]' for array subscript
   64 |         int y = rmin[i + 1] - v[n - 1].first + 1;
      |                     ^
a.cc:71:17: error: conflicting declaration 'std::vector<int> lmax'
   71 |     vector<int> lmax(n);
      |                 ^~~~
a.cc:36:9: note: previous declaration as 'int lmax'
   36 |     int lmax = -1;
      |         ^~~~
a.cc:76:13: error: invalid types 'int[int]' for array subscript
   76 |         lmax[i] = mx;
      |             ^
a.cc:84:34: error: invalid types 'int[int]' for array subscript
   84 |         int x = w[i].first - lmax[i] + 1;
      |                                  ^