Language: C++

#define _USE_MATH_DEFINES

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <unordered_map> 
#include <unordered_set>
#include <functional>

using namespace std;

typedef pair<long long int, long long int> P;
typedef tuple<int, int, int> T;

long long int INF = 1e18;
long long int MOD = 1e9 + 7;

int main(){
	int N;
	cin >> N;
	vector<long long int> L(N), R(N);
	int pos1, pos2;
	P Lmax = P(-INF, -INF), Rmin = P(INF, INF);
	long long int ans = 0;
	for(int i = 0; i < N; i++){
		cin >> L[i] >> R[i];
		if(P(L[i], R[i]) > Lmax){
			Lmax = P(L[i], R[i]);
			pos1 = i;
		}
		if(P(R[i], L[i]) < Rmin){
			Rmin = P(R[i], L[i]);
			pos2 = i;
		}
		ans = max(ans, R[i] - L[i] + 1);
	}
	ans = ans + max(0, (R[pos2] - L[pos1] + 1));
	if(pos1 == pos2){
		cout << ans << endl;
		return 0;
	}
	vector<P> sec;
	for(int i = 0; i < N; i++){
		if(i != pos2){
			sec.push_back(P(R[i], L[i]));
		}
	}
	sort(sec.begin(), sec.end());
	long long int L1 = L[pos2], R1 = R[pos2], L2 = L[pos1];
	for(P p : sec){
		long long int R2 = p.first, L_ = p.second;
		//cout << L_ << " " << R2 << endl;
		//cout << "sec : " << L1 << " " << R2 << endl;
		ans = max(ans, max(0LL, R1 - L1 + 1) + max(0LL, R2 - L2 + 1));
		L1 = max(L1, L_);
	}
	ans = max(ans, max(0LL, R1 - L1 + 1) + max(0LL, R[pos1] - L2 + 1));
	cout << ans << endl;
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:44:24: error: no matching function for call to 'max(int, __gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type)'
   44 |         ans = ans + max(0, (R[pos2] - L[pos1] + 1));
      |                     ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/string:51,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:5:
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:44:24: note:   deduced conflicting types for parameter 'const _Tp' ('int' and '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'})
   44 |         ans = ans + max(0, (R[pos2] - L[pos1] + 1));
      |                     ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:8:
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:44:24: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   44 |         ans = ans + max(0, (R[pos2] - L[pos1] + 1));
      |                     ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~