Language: C++

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

#define FOR(i, m, n) for(int (i) = (m); (i) < (n); (i)++)
#define rep(i, n) FOR(i, 0, n)
#define repn(i, n) FOR(i, 1, n+1)
#define co(n) cout << (n) << endl
#define cosp(n) cout << (n) << ' '
#define setp(n) cout << fixed << setprecision(n);
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
typedef long long ll;
typedef pair<ll, ll> P;

const ll INF = 1e9+1;
const ll LINF = 1e18+1;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const double PI = acos(-1);
const double EPS = 1e-9;
const int MAX_N = 1e5;

ll N;
ll data[MAX_N*4];

bool asc(const P &a, const P &b){
	if(a.fs == b.fs) return a.sc > b.sc;
	else return a.fs < b.fs;
}

void init(){
  rep(i, MAX_N*4) data[i] = LINF;
}

void update(int i, int x){
  i += N-1;
  data[i] = x;
  while(i > 0){
    i = (i-1)/2;
    data[i] = min(data[i*2+1], data[i*2+2]);
  }
}

ll rmq(ll a, ll b, ll k, ll l, ll r){
  if(r <= a || b <= l) return LINF;
  if(a <= l && r <= b) return data[k];
  else{
    ll vl = rmq(a, b, k*2+1, l, (l+r)/2);
    ll vr = rmq(a, b, k*2+2, (l+r)/2, r);
    return min(vl, vr);
  }
}

int main(void){
	int n;
	cin >> n;

	N = 1;
	while(N < n) N <<= 1;
	init();

	vector<P> r(n);
	rep(i, n) cin >> r[i].fs >> r[i].sc;

	sort(all(r));

	rep(i, n) update(i, r[i].sc);

	ll ans = 0;
	repn(i, n-1){
		ans = max(ans, max(ans, max(0LL, rmq(0, i, 0, 0, N)-r[i-1].fs+1)+max(0LL, rmq(i, n, 0, 0, N)-r[n-1].fs+1)));
	}

	vector<int> max_num;
	ll max_r = 0;
	rep(i, n){
		range = r[i].sc - r[i].fs+1;
		if(range > max_r){
			max_r = range;
			max_num.clear();
			max_num.pb(i);
		}else if(range == max_r){
			max_num.pb(i);
		}
	}

	rep(i, max_num.size()){
		ll max_l = -1, min_r = LINF;
		rep(j, n){
			if(j != max_num[i]){
				max_l = max(max_l, r[j].fs);
				min_r = min(min_r, r[j].sc);
			}
		}

		ans = max(ans, max_r+min_r-max_r+1);
	}

	co(ans);

	return 0;
}


Compiler output:
a.cc: In function 'void init()':
a.cc:35:19: error: reference to 'data' is ambiguous
   35 |   rep(i, MAX_N*4) data[i] = LINF;
      |                   ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:27:4: note:                 'll data [400000]'
   27 | ll data[MAX_N*4];
      |    ^~~~
a.cc: In function 'void update(int, int)':
a.cc:40:3: error: reference to 'data' is ambiguous
   40 |   data[i] = x;
      |   ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:27:4: note:                 'll data [400000]'
   27 | ll data[MAX_N*4];
      |    ^~~~
a.cc:43:5: error: reference to 'data' is ambiguous
   43 |     data[i] = min(data[i*2+1], data[i*2+2]);
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:27:4: note:                 'll data [400000]'
   27 | ll data[MAX_N*4];
      |    ^~~~
a.cc:43:19: error: reference to 'data' is ambiguous
   43 |     data[i] = min(data[i*2+1], data[i*2+2]);
      |                   ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:27:4: note:                 'll data [400000]'
   27 | ll data[MAX_N*4];
      |    ^~~~
a.cc:43:32: error: reference to 'data' is ambiguous
   43 |     data[i] = min(data[i*2+1], data[i*2+2]);
      |                                ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:27:4: note:                 'll data [400000]'
   27 | ll data[MAX_N*4];
      |    ^~~~
a.cc: In function 'll rmq(ll, ll, ll, ll, ll)':
a.cc:49:31: error: reference to 'data' is ambiguous
   49 |   if(a <= l && r <= b) return data[k];
      |                               ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:27:4: note:                 'll data [400000]'
   27 | ll data[MAX_N*4];
      |    ^~~~
a.cc: In function 'int main()':
a.cc:80:17: error: 'range' was not declared in this scope
   80 |                 range = r[i].sc - r[i].fs+1;
      |                 ^~~~~