Language: C++

using namespace std;

ostream& operator<<(ostream &out, string str) {
	for(char c : str) out << c;
	return out;
}
 
template<class L, class R> ostream& operator<<(ostream &out, pair<L, R> p) {
	return out << "(" << p.first << ", " << p.second << ")";
}
 
template<class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) {
	out << "{";
	for(auto it = a.begin(); it != a.end(); it = next(it))
		out << (it != a.begin() ? ", " : "") << *it;
	return out << "}";
}
 
void dump() { cerr << "\n"; }
template<class T, class... Ts> void dump(T a, Ts... x) {
	cerr << a << ", ";
	dump(x...);
}
 
#ifdef DEBUG
#  define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__)
#else
#  define debug(...) false
#endif
 
#define REP(i, n) for(int i = 0; i < n; i++)
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define ST first
#define ND second
 
template<class T> int size(T && a) { return a.size(); }

using LL = long long;
using PII = pair<LL, LL>;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n;
	cin >> n;
	vector<PII> inv(n);
	LL ans = 0;
	REP(i, n) {
		LL l, r;
		cin >> l >> r;
		ans = max(ans, (LL) r - l + 1);
		inv[i] = {l, r};
	}

	sort(inv.begin(), inv.end());	
	LL min_end = 1e9 + 1;
	vector<LL> pref(n + 1);
	REP(i, n) {
		min_end = min(min_end, inv[i].ND);
		pref[i + 1] = max(0LL, min_end - inv[i].ST + 1);
	}

	min_end = 1e9 + 1;
	for(int i = n - 1; i >= 1; i--) {
		min_end = min(min_end, inv[i].ND);
		LL suff = max(0LL, min_end - inv[n - 1].ST + 1);
		ans = max(ans, pref[i] + suff);
	}

	cout << ans << "\n";
}


Compiler output:
a.cc:3:1: error: 'ostream' does not name a type
    3 | ostream& operator<<(ostream &out, string str) {
      | ^~~~~~~
a.cc:8:28: error: 'ostream' does not name a type
    8 | template<class L, class R> ostream& operator<<(ostream &out, pair<L, R> p) {
      |                            ^~~~~~~
a.cc:12:24: error: declaration of 'operator<<' as non-function
   12 | template<class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) {
      |                        ^~~~~~~~
a.cc:12:35: error: 'ostream' was not declared in this scope
   12 | template<class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) {
      |                                   ^~~~~~~
a.cc:1:1: note: 'std::ostream' is defined in header '<ostream>'; this is probably fixable by adding '#include <ostream>'
  +++ |+#include <ostream>
    1 | using namespace std;
a.cc:12:44: error: 'out' was not declared in this scope
   12 | template<class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) {
      |                                            ^~~
a.cc:12:51: error: expected primary-expression before 'a'
   12 | template<class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) {
      |                                                   ^
a.cc: In function 'void dump()':
a.cc:19:15: error: 'cerr' was not declared in this scope
   19 | void dump() { cerr << "\n"; }
      |               ^~~~
a.cc:1:1: note: 'std::cerr' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
  +++ |+#include <iostream>
    1 | using namespace std;
a.cc: In function 'void dump(T, Ts ...)':
a.cc:21:9: error: 'cerr' was not declared in this scope
   21 |         cerr << a << ", ";
      |         ^~~~
a.cc:21:9: note: 'std::cerr' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc: At global scope:
a.cc:39:13: error: 'pair' does not name a type
   39 | using PII = pair<LL, LL>;
      |             ^~~~
a.cc: In function 'int main()':
a.cc:42:9: error: 'ios_base' has not been declared
   42 |         ios_base::sync_with_stdio(0);
      |         ^~~~~~~~
a.cc:43:9: error: 'cin' was not declared in this scope
   43 |         cin.tie(0);
      |         ^~~
a.cc:43:9: note: 'std::cin' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'
a.cc:47:9: error: 'vector' was not declared in this scope
   47 |         vector<PII> inv(n);
      |         ^~~~~~
a.cc:1:1: note: 'std::vector' is defined in header '<vector>'; this is probably fixable by adding '#include <vector>'
  +++ |+#include <vector>
    1 | using namespace std;
a.cc:47:16: error: 'PII' was not declared in this scope
   47 |         vector<PII> inv(n);
      |                ^~~
a.cc:47:21: error: 'inv' was not declared in this scope; did you mean 'int'?
   47 |         vector<PII> inv(n);
      |                     ^~~
      |                     int
a.cc:52:23: error: 'max' was not declared in this scope
   52 |                 ans = max(ans, (LL) r - l + 1);
      |                       ^~~
a.cc:56:9: error: 'sort' was not declared in this scope; did you mean 'short'?
   56 |         sort(inv.begin(), inv.end());
      |         ^~~~
      |         short
a.cc:58:18: error: expected primary-expression before '>' token
   58 |         vector<LL> pref(n + 1);
      |                  ^
a.cc:58:20: error: 'pref' was not declared in this scope
   58 |         vector<LL> pref(n + 1);
      |                    ^~~~
a.cc:60:27: error: 'min' was not declared in this scope; did you mean 'main'?
   60 |                 min_end = min(min_end, inv[i].ND);
      |                           ^~~
      |                           main
a.cc:61:31: error: 'max' was not declared in this scope
   61 |                 pref[i + 1] = max(0LL, min_end - inv[i].ST + 1);
      |                               ^~~
a.cc:66:27: error: 'min' was not declared in this scope; did you mean 'main'?
   66 |                 min_end = min(min_end, inv[i].ND);
      |                           ^~~
      |                           main
a.cc:67:27: error: 'max' was not declared in this scope
   67 |                 LL suff = max(0LL, min_end - inv[n - 1].ST + 1);
      |                           ^~~
a.cc:71:9: error: 'cout' was not declared in this scope
   71 |         cout << ans << "\n";
      |         ^~~~
a.cc:71:9: note: 'std::cout' is defined in header '<iostream>'; this is probably fixable by adding '#include <iostream>'