Language: C++

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define MOD (int) (1e9+7)
#define INF (int) 2e9
#define LLINF (ll) 2e18

int main(){
    int n;
    cin >> n;
    P p[n];
    rep(i, n){
        ll l, r;
        cin >> l >> r;
        p[i].first = l;
        p[i].second = r;
    }

    sort(p, p+n);

    ll ul[n], ur[n], dl[n], dr[n];
    
    ul[0] = p[0].first;
    ur[0] = p[0].second;

    rep(i, n-1){
        ul[i+1] = max(ul[i], p[i+1].first);
        ur[i+1] = min(ur[i], p[i+1].second);
    }

    // rep(i, n-1){
    //     printf("%d %d \n",ul[i], ur[i]);
    // }

    dl[0] = p[n-1].first;
    dr[0] = p[n-1].second;
    rep(i, n-1){
        dl[i+1] = max(dl[i], p[n-i-2].first);
        dr[i+1] = min(dr[i], p[n-i-2].second);
    }

    // rep(i, n-1){
    //     printf("%d %d \n",dl[i], dr[i]);
    // }


    ll ans = 0;
    rep(i, n-1){
        chmax(ans, (ur[i]-ul[i]+1)+(dr[n-i-2]-dl[n-i-2]+1));
        // printf("%d %d \n",(ur[i]-ul[i]+1), (dr[n-i-2]-dl[n-i-2]+1));
    }
    cout << ans << endl;
    // printf("%d\n", N);
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:31:22: error: no matching function for call to 'max(ll&, int&)'
   31 |         ul[i+1] = max(ul[i], p[i+1].first);
      |                   ~~~^~~~~~~~~~~~~~~~~~~~~
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:31:22: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   31 |         ul[i+1] = max(ul[i], p[i+1].first);
      |                   ~~~^~~~~~~~~~~~~~~~~~~~~
/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:31:22: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   31 |         ul[i+1] = max(ul[i], p[i+1].first);
      |                   ~~~^~~~~~~~~~~~~~~~~~~~~
a.cc:32:22: error: no matching function for call to 'min(ll&, int&)'
   32 |         ur[i+1] = min(ur[i], p[i+1].second);
      |                   ~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:32:22: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   32 |         ur[i+1] = min(ur[i], p[i+1].second);
      |                   ~~~^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:32:22: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   32 |         ur[i+1] = min(ur[i], p[i+1].second);
      |                   ~~~^~~~~~~~~~~~~~~~~~~~~~
a.cc:42:22: error: no matching function for call to 'max(ll&, int&)'
   42 |         dl[i+1] = max(dl[i], p[n-i-2].first);
      |                   ~~~^~~~~~~~~~~~~~~~~~~~~~~
/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:42:22: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   42 |         dl[i+1] = max(dl[i], p[n-i-2].first);
      |                   ~~~^~~~~~~~~~~~~~~~~~~~~~~
/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:42:22: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   42 |         dl[i+1] = max(dl[i], p[n-i-2].first);
      |                   ~~~^~~~~~~~~~~~~~~~~~~~~~~
a.cc:43:22: error: no matching function for call to 'min(ll&, int&)'
   43 |         dr[i+1] = min(dr[i], p[n-i-2].second);
      |                   ~~~^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:43:22: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   43 |         dr[i+1] = min(dr[i], p[n-i-2].second);
      |                   ~~~^~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:43:22: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   43 |         dr[i+1] = min(dr[i], p[n-i-2].second);
      |                   ~~~^~~~~~~~~~~~~~~~~~~~~~~~