Language: C++

/**
SXR0aXAkI0JwbXptI3FhI3Z3I293bCNqY2IjUG0jMCNicG0jVHFkcXZvLyNCcG0jQW10bjBhY2phcWFicXZvLyNNYm16dml0MSNWdyNhdGN1am16I2tpdiNhbXF9bSNQcXUjVnd6I0F0bW14MSNQcWEjaXptI2l0dCNicHF2b2EjUXYjYnBtI3BtaWRtdmEjaXZsI3d2I21pemJwMSNFcHcjcWEjYnBtem0ja2l2I3F2Ym16a21sbSNRdiNQcWEjeHptYW12a20jbXtrbXhiI0lhI3BtI3htenVxYmJtYnBHI1BtI3N2d2VtYnAjRXBpYiMraXh4bWl6bWJwI2J3I1BxYSNrem1pYmN6bWEjSWEsI0ptbnd6bSN3eiNJbmJteiN3eiNKbXBxdmwjYnBtdTEjVnd6I2FwaXR0I2JwbXwja3d1eGlhYSNJY29wYiN3biNwcWEjc3Z3ZXRtbG9tI017a214YiNpYSNQbSNlcXR0bWJwMSNQcWEjYnB6d3ZtI2x3YnAjbXtibXZsI1dkbXojYnBtI3BtaWRtdmEjSXZsI3d2I21pemJwLyNpdmwjUG0jbm1tdG1icCNWdyNuaWJxb2NtI3F2I29jaXpscXZvI0l2bCN4em1hbXpkcXZvI2JwbXUvI053eiNQbSNxYSNicG0jVXdhYiNQcW9wMSNCcG0jQWN4em11bSMrcXYjb3R3enwsMQ==
*/
#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back

const long long MOD = 1e9 + 7;
const long long MAXN = 1e6 + 1;
using namespace std;

typedef long long ll;

long long readInt() {
    bool minus1 = false;
    long long result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus1 = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' || ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus1)
        return -result;
    else
        return result;
}

pair<int,int> a[MAXN];

int main() {
    int n = readInt();
    for (int i = 1; i <= n; i++) {
        a[i] = {readInt(), readInt()};
    }
    sort(a + 1, a + 1 + n);
    multiset <int> s1;
    multiset <int> s2;
    for (int i = 1; i <= n; i++) {
        s2.insert(a[i].S);
    }
    ll ans = 0;
    for (int i = 1; i < n; i ++) {
        /// min(R) - max(L) + 1
        s2.erase(s2.find(a[i].S));
        s1.insert(a[i].S);
        int min_R_1 = *s1.begin();
        int min_R_2 = *s2.begin();
        if (min_R_1 <= min_R_2) {
            ///checking for >
            auto nxt = s1.upper_bound(min_R_2);
            if (nxt == s1.end()) {
                ans = max(ans, max(1ll * min_R_1 - a[i].F + 1, 0ll) + max(1ll * min_R_2 - a[n].F + 1, 0ll));
            } else {
                ans = max(ans, max(1ll * (*nxt) - a[i].F + 1, 0ll) + max(1ll * min_R_2 - a[n].F + 1, 0ll));
            }
            /// checking for =
            auto nxt = s1.lower_bound(min_R_2);
            if (nxt != st.end()) {
            	int tmp = *nxt;
            	if (tmp == min_R_2 && s1.count(tmp) > 1) {
					ans = max(ans, max(1ll * tmp - a[i].F + 1, 0ll) + max(1ll * min_R_2 - a[n].F + 1, 0ll));            		
            	}
            }
        } else {
            ans = max(ans, max(1ll * min_R_1 - a[i].F + 1, 0ll) + max(1ll * min_R_2 - a[n].F + 1, 0ll));
        }
        if (a[i].F == a[n].F) assert(0);
    }
    cout << ans;
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:67:18: error: conflicting declaration 'auto nxt'
   67 |             auto nxt = s1.lower_bound(min_R_2);
      |                  ^~~
a.cc:60:18: note: previous declaration as 'std::_Rb_tree_const_iterator<int> nxt'
   60 |             auto nxt = s1.upper_bound(min_R_2);
      |                  ^~~
a.cc:68:24: error: 'st' was not declared in this scope; did you mean 's2'?
   68 |             if (nxt != st.end()) {
      |                        ^~
      |                        s2