Language: C++

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define REP(i, n) for(int i=0; i<(n); ++i)
#define FOR(i, a, b) for(int i=(a); i<(b); ++i)
#define FORR(i, a, b) for(int i=(b)-1; i>=(a); --i)

#define DEBUG(x) cout<<#x<<": "<<(x)<<'\n'
#define DEBUG_VEC(v) cout<<#v<<":";REP(i, v.size())cout<<' '<<v[i];cout<<'\n'
#define ALL(a) (a).begin(), (a).end()

template<typename T> inline void CHMAX(T& a, const T b) {if(a<b) a=b;}
template<typename T> inline void CHMIN(T& a, const T b) {if(a>b) a=b;}

constexpr ll MOD=1000000007ll;
// constexpr ll MOD=998244353ll;
#define FIX(a) ((a)%MOD+MOD)%MOD

const double EPS=1e-11;
#define EQ0(x) (abs((x))<EPS)
#define EQ(a, b) (abs((a)-(b))<EPS)

#define int long long

signed main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout<<setprecision(10)<<fixed;
  int n, l[125252], r[125252];
  cin>>n;
  int lma=0, lidx=0, rmi=1e9, ridx=0;
  int ma=0;
  REP(i, n){
    cin>>l[i]>>r[i];
    ++r[i];
    if(lma<l[i]){
      lma=l[i];
      lidx=i;
    }
    if(r[i]<rmi){
      rmi=r[i];
      ridx=i;
    }
    CHMAX(ma, r[i]-l[i]);
  }
  int ans=rmi-lma+ma;
  vi ord;
  REP(i, n){
    if(i!=lidx && i!=ridx){
      ord.push_back(i);
    }
  }
  sort(ALL(ord), [&l, &r](int x, int y){
    if(r[x]==r[y]) return l[x]<l[y];
    return r[x]>r[y];
  });
  ord.push_back(ridx);
  int lb=0;
  FORR(i, 0, n-1){
    CHMAX(lb, l[ord[i]]);
    // 1回目
    int tmp=max(0, r[ridx]-lb);
    // ２回目
    tmp+=max(0, min(r[lidx], (i>0 ? r[ord[i-1]] : r[lidx]))-l[lidx]);
    CHMAX(ans, tmp);
  }
  cout<<ans<<'\n';
  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:70:16: error: no matching function for call to 'max(int, long long int)'
   70 |     int tmp=max(0, r[ridx]-lb);
      |             ~~~^~~~~~~~~~~~~~~
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:1:
/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:70:16: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   70 |     int tmp=max(0, r[ridx]-lb);
      |             ~~~^~~~~~~~~~~~~~~
/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:70:16: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   70 |     int tmp=max(0, r[ridx]-lb);
      |             ~~~^~~~~~~~~~~~~~~
a.cc:72:13: error: no matching function for call to 'max(int, long long int)'
   72 |     tmp+=max(0, min(r[lidx], (i>0 ? r[ord[i-1]] : r[lidx]))-l[lidx]);
      |          ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/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:72:13: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
   72 |     tmp+=max(0, min(r[lidx], (i>0 ? r[ord[i-1]] : r[lidx]))-l[lidx]);
      |          ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/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
/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:72:13: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   72 |     tmp+=max(0, min(r[lidx], (i>0 ? r[ord[i-1]] : r[lidx]))-l[lidx]);
      |          ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~