Language: C++

/**
SXR0aXAkI0JwbXptI3FhI3Z3I293bCNqY2IjUG0jMCNicG0jVHFkcXZvLyNCcG0jQW10bjBhY2phcWFicXZvLyNNYm16dml0MSNWdyNhdGN1am16I2tpdiNhbXF9bSNQcXUjVnd6I0F0bW14MSNQcWEjaXptI2l0dCNicHF2b2EjUXYjYnBtI3BtaWRtdmEjaXZsI3d2I21pemJwMSNFcHcjcWEjYnBtem0ja2l2I3F2Ym16a21sbSNRdiNQcWEjeHptYW12a20jbXtrbXhiI0lhI3BtI3htenVxYmJtYnBHI1BtI3N2d2VtYnAjRXBpYiMraXh4bWl6bWJwI2J3I1BxYSNrem1pYmN6bWEjSWEsI0ptbnd6bSN3eiNJbmJteiN3eiNKbXBxdmwjYnBtdTEjVnd6I2FwaXR0I2JwbXwja3d1eGlhYSNJY29wYiN3biNwcWEjc3Z3ZXRtbG9tI017a214YiNpYSNQbSNlcXR0bWJwMSNQcWEjYnB6d3ZtI2x3YnAjbXtibXZsI1dkbXojYnBtI3BtaWRtdmEjSXZsI3d2I21pemJwLyNpdmwjUG0jbm1tdG1icCNWdyNuaWJxb2NtI3F2I29jaXpscXZvI0l2bCN4em1hbXpkcXZvI2JwbXUvI053eiNQbSNxYSNicG0jVXdhYiNQcW9wMSNCcG0jQWN4em11bSMrcXYjb3R3enwsMQ==
*/
#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back

const long long MOD = 1e9 + 7;
const long long MAXN = 1e6 + 1;
using namespace std;

typedef long long ll;

long long readInt() {
  bool minus1 = false;
  long long result = 0;
  char ch;
  ch = getchar();
  while (true) {
    if (ch == '-') break;
    if (ch >= '0' && ch <= '9') break;
    ch = getchar();
  }
  if (ch == '-')
    minus1 = true;
  else
    result = ch - '0';
  while (true) {
    ch = getchar();
    if (ch < '0' || ch > '9') break;
    result = result * 10 + (ch - '0');
  }
  if (minus1)
    return -result;
  else
    return result;
}

pair<int, int> a[MAXN];

int main() {
  int n = readInt();
  for (int i = 1; i <= n; i++) {
    a[i] = {readInt(), readInt()};
  }
  sort(a + 1, a + 1 + n);
  multiset<int> s1;
  multiset<int> s2;
  for (int i = 1; i <= n; i++) {
    s2.insert(a[i].S);
  }
  ll ans = 0;
  for (int i = 1; i < n; i++) {
    /// min(R) - max(L) + 1
    s2.erase(s2.find(a[i].S));
    s1.insert(a[i].S);
    int min_R_1 = *s1.begin();
    int min_R_2 = *s2.begin();
    ans = max(ans, max(1ll * min_R_1 - a[i].F + 1, 0ll) +
                       max(1ll * min_R_2 - a[n].F + 1, 0ll));
    /// checking for >
    s1.erase(s1.find(a[i].S));
    auto nxt = s1.end();
    if (nxt != s1.begin()) {
      nxt--;
      //cout << "NUM " << (*nxt) << " " << a[i].S << endl;
      //cout << *s1.begin() << endl;
      ans = max(ans,
                max(1ll * min(1ll * a[i].S, 1ll * (*nxt)) - a[i].F + 1, 0ll) +
                    max(1ll * (s1.size() > 1 ? min(1ll * (*s1.begin()), min_R_2) : min_R_2) - a[n].F + 1, 0ll));
    }
    s1.insert(a[i].S);

    if (min_R_1 >= min_R_2) {
      // correct
      s1.erase(s1.find(a[i].S));
      auto nxt = s1.end();
      if (nxt != s1.begin()) {
        nxt--;
        ans = max(ans,
                  max(1ll * min(1ll * a[i].S, 1ll * (*nxt)) - a[i].F + 1, 0ll) +
                      max(1ll * min_R_2 - a[n].F + 1, 0ll));
      }
      s1.insert(a[i].S);
    }
  }
  cout << ans;
  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:72:51: error: no matching function for call to 'min(long long int, int&)'
   72 |                     max(1ll * (s1.size() > 1 ? min(1ll * (*s1.begin()), min_R_2) : min_R_2) - a[n].F + 1, 0ll));
      |                                                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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:4:
/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:72:51: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   72 |                     max(1ll * (s1.size() > 1 ? min(1ll * (*s1.begin()), min_R_2) : min_R_2) - a[n].F + 1, 0ll));
      |                                                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/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
In file included from /usr/include/c++/14/algorithm:61:
/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:72:51: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   72 |                     max(1ll * (s1.size() > 1 ? min(1ll * (*s1.begin()), min_R_2) : min_R_2) - a[n].F + 1, 0ll));
      |                                                ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~