Language: C++

#include<bits/stdc++.h>

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define dline    cerr << __LINE__ << endl;

using ll = long long;
template<class T, class U> using P = pair<T,U>;
template<class T> using Heap = priority_queue<T>;
template<class T> using heaP = priority_queue<T,vector<T>,greater<T>>;
template<class T,class U> using umap = unordered_map<T,U>;
template<class T> using uset = unordered_set<T>;

template<class T>
bool ChangeMax(T&a,const T&b){
  if(a >= b) return false;
  a = b;    return true;
}

template<class T>
bool ChangeMin(T&a,const T&b){
  if(a <= b) return false;
  a = b;    return true;  
}

template<class T, size_t N, class U>
void Fill(T (&a)[N], const U&v){
    fill((U*)a,(U*)(a+N),v);
}

template<class T>
istream& operator >> (istream&is, vector<T>&v){
  for(auto&e:v)is >> e;
  return is;
}

struct LR{
  ll l,r;
  LR(ll l, ll r):l(l),r(r){};
  LR(){}
  friend bool operator<(const LR&a, const LR&b){
    if(a.l == b.l) return a.r < b.r;
    return a.l < b.l;
  }
};

int main(){
  ll n; cin >> n;
  vector<LR> v(n);
  ll maxL = -1, minR = 1001001001, maxLi, minRi;
  for(ll i = 0; i < n; ++i){
    ll l, r; cin >> l >> r;r++;
    v[i] = {l,r};
    if(ChangeMax(maxL, l))maxLi = i;
    if(ChangeMin(minR, r))minRi = i;
  }
  
  ll maxLR = 0;
  for(ll i = 0; i < n; ++i) ChangeMax(maxLR, v[i].r - v[i].l);

  if(maxLi == minRi){
    cout << maxLR + minR - maxL << endl;
    return 0;
  }

  vector<LR> w;
  for(ll i = 0; i < n; ++i){
    if(i != maxLi && i != minRi) w.push_back(v[i]);    
  }

  sort(w.begin(), w.end());

  vector<ll> minRlist(n-2), maxLlist(n-2);
  for(ll i = 0; i < n-2; ++i){
    if(i == 0)maxLlist[i] = w[i].l;
    else maxLlist[i] = max(maxLlist[i-1], w[i].l);
  }
  for(ll i = n-3; i >= 0; --i){
    if(i == n-3)minRlist[i] = w[i].r;
    else minRlist[i] = min(minRlist[i+1], w[i].r);
  }
  debug(minR);
  debug(maxL);  
  for(ll i = -1; i < n-2; ++i){
    ll l1=0,r1=minR,l2=maxL,r2=0;
    if(i == -1)l1 = v[minRi].l;
    else l1 = maxLlist[i];
    if(i == n-3)r2 = v[maxLi].r;
    else r2 = minRlist[i+1];
    debug(i);
    debug(l1);
    debug(r1);
    debug(l2);
    debug(r2);
    ChangeMax(maxLR, max(r1-l1,0)+max(r2-l2,0));
  }

  cout << maxLR << endl;
  
  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:96:25: error: no matching function for call to 'max(ll, int)'
   96 |     ChangeMax(maxLR, max(r1-l1,0)+max(r2-l2,0));
      |                      ~~~^~~~~~~~~
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:96:25: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   96 |     ChangeMax(maxLR, max(r1-l1,0)+max(r2-l2,0));
      |                      ~~~^~~~~~~~~
/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:96:25: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   96 |     ChangeMax(maxLR, max(r1-l1,0)+max(r2-l2,0));
      |                      ~~~^~~~~~~~~
a.cc:96:38: error: no matching function for call to 'max(ll, int)'
   96 |     ChangeMax(maxLR, max(r1-l1,0)+max(r2-l2,0));
      |                                   ~~~^~~~~~~~~
/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:96:38: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   96 |     ChangeMax(maxLR, max(r1-l1,0)+max(r2-l2,0));
      |                                   ~~~^~~~~~~~~
/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:96:38: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   96 |     ChangeMax(maxLR, max(r1-l1,0)+max(r2-l2,0));
      |                                   ~~~^~~~~~~~~