Language: C++

#include <bits/stdc++.h>
using namespace std;
int main(){
  //入力
  int N;
  cin >> N;
  vector<pair<int,int>> LR(N);
  int MaxLen = 0;
  int MinLen = 1000000000;
  for (int i = 0; i < N; i++){
    cin >> LR[i].first >> LR[i].second;
    MaxLen = max(MaxLen,LR[i].second-LR[i].first+1);
    MinLen = min(MinLen,LR[i].second-LR[i].first+1);
  }
  //問題をソート
  sort(LR.begin(),LR.end());
  //他を含むものを消す
  vector<pair<int,int>> LR2;
  LR2.push_back(LR[0]);
  for (int i = 1; i < N; i++){
    LR2.push_back(LR[i]);
    int a,b,c,d;
    a = LR2[LR2.size()-2].first;
    b = LR2[LR2.size()-2].second;
    c = LR2[LR2.size()-1].first;
    d = LR2[LR2.size()-1].second;
    if (a<=c && d<=b){
      //[c,d]が[a,b]に含まれるので[a,b]を消す
      LR2.pop_back();
      LR2.pop_back();
      LR2.push_back(make_pair(c,d));
    } else if (c<=a && b<=d){
      //[a,b]が[c,d]に含まれるので[c,d]を消す
      LR2.pop_back();
    }
  }
  if (LR2.size()==1){
    cout << MaxLen + MinLen;
  } else {
    int M = LR2.size();
    long long Max = 0;
    for (int i = 0; i < M-1; i++){
      Max = max(Max,LR2[0].second-LR2[i].first+1+LR2[i+1].second-LR2[M-1].first+1);
    }
    cout << max(Max,MaxLen);
  }
}

Compiler output:
a.cc: In function 'int main()':
a.cc:43:16: error: no matching function for call to 'max(long long int&, int)'
   43 |       Max = max(Max,LR2[0].second-LR2[i].first+1+LR2[i+1].second-LR2[M-1].first+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: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:43:16: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   43 |       Max = max(Max,LR2[0].second-LR2[i].first+1+LR2[i+1].second-LR2[M-1].first+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:43:16: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   43 |       Max = max(Max,LR2[0].second-LR2[i].first+1+LR2[i+1].second-LR2[M-1].first+1);
      |             ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:45:16: error: no matching function for call to 'max(long long int&, int&)'
   45 |     cout << max(Max,MaxLen);
      |             ~~~^~~~~~~~~~~~
/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:45:16: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   45 |     cout << max(Max,MaxLen);
      |             ~~~^~~~~~~~~~~~
/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:45:16: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   45 |     cout << max(Max,MaxLen);
      |             ~~~^~~~~~~~~~~~