Language: C++

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

#define F first
#define S second
#define pb push_back
#define int __int64_t
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;
}

multiset<int> s1;
multiset<int> s2;
multiset<int> s3;

int alp_salganda_newe(pii x) {
	s1.erase(s1.find(x.S));
    auto nxt = s1.end();
    int res = 0;
	if (nxt != s1.begin()) {
        nxt--;
        res = max(1ll * min(1ll * x.S, 1ll * (*nxt)) - x.F + 1, 0ll) +
                      max(1ll * min((s1.size() > 0 ? *s1.begin() : (int)MOD), min_R_2) - a[n].F + 1, 0ll);
	}
	s1.insert(x.S);
	return res;
}


pair<int, int> a[MAXN];

 main() {
  int n = readInt();
  for (int i = 1; i <= n; i++) {
    a[i] = {readInt(), readInt()};
  }
  sort(a + 1, a + 1 + n);
  
  for (int i = 1; i <= n; i++) {
    s2.insert(a[i].S);
  	s3.insert(a[i].F);
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
  	s2.erase(s2.find(a[i].S));
	s3.erase(s3.find(a[i].F));
	ans = max(ans, a[i].S - a[i].F + 1 + max(0ll, *s2.begin() - *s3.rbegin() + 1));	    	
	s2.insert(a[i].S);
  	s3.insert(a[i].F);
  }
  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();
    
    ans = max(ans, max(1ll * min_R_1 - a[i].F + 1, 0ll) +
                       max(1ll * min_R_2 - a[n].F + 1, 0ll));
  	
  	ans = max(ans, alp_salganda_newe(a[i]));
  }
  cout << ans;
  return 0;
}


Compiler output:
a.cc:45:23: error: 'pii' was not declared in this scope
   45 | int alp_salganda_newe(pii x) {
      |                       ^~~
a.cc:61:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   61 |  main() {
      |  ^~~~
a.cc: In function 'int main()':
a.cc:76:49: error: no matching function for call to 'max(long long int, long int)'
   76 |         ans = max(ans, a[i].S - a[i].F + 1 + max(0ll, *s2.begin() - *s3.rbegin() + 1));
      |                                              ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:4:
/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:76:49: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'long int')
   76 |         ans = max(ans, a[i].S - a[i].F + 1 + max(0ll, *s2.begin() - *s3.rbegin() + 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:
/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:76:49: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   76 |         ans = max(ans, a[i].S - a[i].F + 1 + max(0ll, *s2.begin() - *s3.rbegin() + 1));
      |                                              ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:91:41: error: 'alp_salganda_newe' cannot be used as a function
   91 |         ans = max(ans, alp_salganda_newe(a[i]));
      |                        ~~~~~~~~~~~~~~~~~^~~~~~