Language: C++

#include <bits/stdc++.h>

using namespace std;

int main(){
    const int INF = 1e9;

    int n;
    cin >> n;
    vector<array<int64_t, 2>> A(n);
    for (auto &a: A)
        cin >> a[0] >> a[1];

    sort(A.begin(), A.end());
    if (n == 2){
        cout << A[0][1] - A[0][0] + A[1][1] - A[1][0] + 2 << endl;
        return 0;
    }

    vector<int64_t> front(n - 1), back(n - 1);

    int64_t r = 1e9, l = 0;
    for (int i = 0; i < n - 1; i++){
        r = min(r, A[i][1]);
        l = max(l, A[i][0]);
        front[i] = max(0L, r - l + 1);
    }

    r = 1e9, l = 0;
    for (int i = n - 1; i > 0; i--){        r = min(r, A[i][1]);
        l = max(l, A[i][0]);
        back[i - 1] = max(0L, r - l + 1);
    }

    int64_t ans = 0;
    for (int i = 0; i < n - 1; i++)
        ans = max(ans, front[i] + back[i]);
    
    int maxi = 0;
    int ma = 0;
    for (int i = 0; i < n; i++){
        if (ma < A[i][1] - A[i][0]){
            ma = A[i][1] - A[i][0];
            maxi = i;
        }
    }
  
    int max_l = 0, min_r = 1e9;
    for (int i = 0; i < n; i++){
        if (i == maxi)
            continue;
        max_l = max(max_l, A[i][0]);
        min_r = min(min_r, A[i][0]);
    }
  
    ans = max(ans, ma + min_r - max_l + 2);
  
    cout << ans << endl;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:52:20: error: no matching function for call to 'max(int&, std::array<long int, 2>::value_type&)'
   52 |         max_l = max(max_l, A[i][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:52:20: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'std::array<long int, 2>::value_type' {aka 'long int'})
   52 |         max_l = max(max_l, A[i][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:52:20: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   52 |         max_l = max(max_l, A[i][0]);
      |                 ~~~^~~~~~~~~~~~~~~~
a.cc:53:20: error: no matching function for call to 'min(int&, std::array<long int, 2>::value_type&)'
   53 |         min_r = min(min_r, A[i][0]);
      |                 ~~~^~~~~~~~~~~~~~~~
/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:53:20: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'std::array<long int, 2>::value_type' {aka 'long int'})
   53 |         min_r = min(min_r, A[i][0]);
      |                 ~~~^~~~~~~~~~~~~~~~
/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:53:20: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
   53 |         min_r = min(min_r, A[i][0]);
      |                 ~~~^~~~~~~~~~~~~~~~
a.cc:56:14: error: no matching function for call to 'max(int64_t&, int)'
   56 |     ans = max(ans, ma + min_r - max_l + 2);
      |           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/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:56:14: note:   deduced conflicting types for parameter 'const _Tp' ('long int' and 'int')
   56 |     ans = max(ans, ma + min_r - max_l + 2);
      |           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/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:56:14: note:   mismatched types 'std::initializer_list<_Tp>' and 'long int'
   56 |     ans = max(ans, ma + min_r - max_l + 2);
      |           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~