Language: C++

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

struct str{
    int l, r;
};

str niz[100005];

multiset <pair <int, int>> prvil;
multiset <pair <int, int>> drugir;
multiset <pair <int, int>> prvir;

int res;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(10);
    cout << fixed;

    int n;
    cin >> n;
    int maxl = 0, minr = 1e9;
    int mxsz = 0;
    for(int i=1; i<=n; i++){
        cin >> niz[i].l >> niz[i].r;
        mxsz = max(mxsz, niz[i].r-niz[i].l+1);
        maxl = max(niz[i].l, maxl);
        minr = min(niz[i].r, minr);
    }
    for(int i=1; i<=n; i++){
        if(maxl == niz[i].l && minr == niz[i].r){
            cout << mxsz + max(0, minr-maxl+1);
            return 0;
        }
    }
    int a=0, b=0;
    for(int i=1; i<=n; i++){
        if(minr == niz[i].r && (b == 0 || niz[b].l < niz[i].l)){
            b = i;
        }
        if(maxl == niz[i].l && (a == 0 || niz[a].r > niz[i].l)){
            a = i;
        }
    }
    int mx1 = 0;
    for(int i=1; i<=n; i++){
        if(i != a && i != b) mx1 = max(mx1, niz[i].r - niz[i].l+1);
    }
    if(n > 2){
        res = mx1 + max(0LL, minr-maxl+1);
    }
    for(int i=1; i<=n; i++){
        if(minr == niz[i].r && (b == 0 || niz[b].l > niz[i].l)){
            b = i;
        }
    }
    drugir.insert({-niz[b].l, niz[b].r});
    for(int i=1; i<=n; i++){
        if(i != b){
            if(niz[i].l > niz[b].l){
                prvir.insert({niz[i].r, niz[i].l});
                prvil.insert({niz[i].l, niz[i].r});
            }
            else{
                drugir.insert({-niz[i].l, niz[i].r});
            }
        }
    }
    int l2 = -drugir.begin()->first;
    int r1 = prvir.begin()->first;
    int l1 = prvil.rbegin()->first;
    res = max(res, max(0, minr-l2+1)+max(0, r1-l1+1));
    while(prvil.size() > 1){
        pair <int, int> pii = *prvil.begin();
        prvil.erase(prvil.find(pii));
        prvir.erase(prvir.find({pii.second, pii.first}));
        drugir.insert({-pii.first, pii.second});
        l2 = -drugir.begin()->first;
        r1 = prvir.begin()->first;
        l1 = prvil.rbegin()->first;
        res = max(res, max(0, minr-l2+1)+max(0, r1-l1+1));
        //cout << l1 << " " << r1 << " " << l2 << " " << minr << endl;
    }
    cout << res;
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:55:24: error: no matching function for call to 'max(long long int, int)'
   55 |         res = mx1 + max(0LL, minr-maxl+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:55:24: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
   55 |         res = mx1 + max(0LL, minr-maxl+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:55:24: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
   55 |         res = mx1 + max(0LL, minr-maxl+1);
      |                     ~~~^~~~~~~~~~~~~~~~~~