Language: C++

//#include "pch.h"
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <limits.h>
#include <string>

using namespace std;

#define rip(i, n, s) for (int i = (s);i < (int)( n ); i++)
#define mp make_pair
#define all(a) (a).begin(), (a).end()
typedef long long ll;
typedef pair<ll, ll> P;

template<typename T>
void vdeb(vector<T> &da) {
	int size = da.size();
	for (int i = 0;i < size;i++) {
		if (i == size - 1) {
			cout << da[i] << endl;
		}
		else {
			cout << da[i] << ' ';
		}
	}
}
template<typename T>
void vvdeb(vector<T> &da) {
	int size = da.size();
	for (int i = 0;i < size;i++) {
		vdeb(da[i]);
	}
}
template<typename T>
struct matrix{
	int size;
	vector<vector<T> > table;
	matrix(int n){
		size=n;
		table=vector<vector<T> >(n,vector<T>(n));
	}
	matrix<T> operator*(const matrix<T> &othor){
		matrix<T> ret(size);
		for(int i=0;i<size;i++){
			for(int j=0;j<size;j++){
				for(int k=0;k<size;k++){
					ret.table[i][j]+=table[i][k]*othor.table[k][j];
				}
			}
		}
		return ret;
	}
	T all_sum(){
		T ret = table[0][0];
		for(int i=1;i<size;i++){
			ret+=table[0][i];
		}
		for(int i=1;i<size;i++){
			for(int j=0;j<size;j++){
				ret+=table[i][j];
			}
		}
		return ret;
	}
};
struct UnionFind{
    int size;
    vector<int> par;
    vector<int> rank;
    UnionFind(int x){
        size=x;
        par=vector<int> (x);
        rank=vector<int> (x,1);
        for (int i=0;i<x;i++){
            par[i]=i;
        }
    }
    int root(int x){
        if(par[x]==x){
            return x;
        }
        else{
            return par[x]=root(par[x]);
        }
    }
    void unite(int x,int y){
        x=root(x);y=root(y);
        if(x==y){
            return;
        }
        if(rank[x]<rank[y]){
            par[x]=y;
        }
        else{
            par[y]=x;
        }
        if(rank[x]==rank[y]){
            rank[x]++;
        }
    }
    bool check(int x,int y){
        return root(x)==root(y);
    }
    void initialize(){
        for (int i=0;i<size;i++){
            par[i]=i;
            rank[i]=1;
        }
    }
};
struct SegTree{ //0indexed
    vector<int> nod;
    int size;
    SegTree(int x){
        size=1;
        while(x>size){
            size*=2;
        }
        nod=vector<int>(size*2-1,0);
    }
    void update(int k,int a){
        k+=size-1;
        nod[k]+=a;//最下の処理
        while(k>0){
            k=(k-1)/2;
            nod[k]+=a; //下から上へ 
        }
    }
    int query(int a,int b,int k,int l,int r){
        //[a,b)について
        //kは節点の番号,[l,r)はその節点がカバーする範囲
        if(r<=a||b<=l){ //[a,b)と[l,r)が重なって無いとき
            return 0;
        }
        if(a<=l && r<=b){ //[a,b)が[l,r)を完全に含んでいるとき
            return nod[k];
        }
        else{
            return query(a,b,k*2+1,l,(l+r)/2)+query(a,b,k*2+2,(l+r)/2,r);
        }
    }
    void deb(){ //全ノードの出力
		for(int i=0;i<size*2-1;i++){
			if(i==size*2-2){
				cout << nod[i] << endl;
			}
			else{
				cout << nod[i] << ' ';
			}
		}
		return;
	}
};
struct BIT{
    vector<long long> li;
    int size;
    BIT (int n){
        li=vector<long long>(n+1,0);
        size=n;
    }
    long long sum (int i){
        long long s=0;
        while(i>0){
            s+=li[i];
            i-=i & -i;
        }
        return s;
    }
    void add(int i,long long x){
        while(i<=size){
            li[i]+=x;
            i+=i & -i;
        }
    }
};
struct complex{ //except for (0,0)
    double real;
    double imag;
    complex (double x,double y){
        real=x;
        imag=y;
    }
    double dist(){
        return sqrt(real*real+imag*imag);
    }
};
complex operator+(const complex &x,const complex &y){
    return complex(x.real+y.real,x.imag+y.imag);
}
complex operator-(const complex &x,const complex &y){
    return complex(x.real-y.real,x.imag-y.imag);
}
complex operator*(const complex &x,const complex &y){
    return complex(x.real*y.real-x.imag*y.imag,x.real*y.imag+x.imag*y.imag);
}
complex operator*(const complex &x,const double y){
    return complex(x.real*y,x.imag*y);
}
complex operator/(const complex &x,const double &y){
    return x*(1/y);
}
complex operator/(const complex &x,const complex &y){
    complex ret(y.real,-y.imag);
    double div=ret.dist();
    ret=x*ret;
    return ret/div;
}
bool operator>(const complex &x,const complex &y){//偏角、絶対値の順で
    if (x.imag==0 && y.imag==0){
        return x.real<y.real;
    }
    else if (x.imag==0 && y.imag>0){
        return x.real<0;
    }
    else if (x.imag==0 && y.imag<0){
        return false;
    }
    else if (x.imag>0 && y.imag==0){
        return y.real > 0;
    }
    else if (x.imag<0 && y.imag==0){
        return true;
    }
    else if(x.imag*y.imag>0){
        return y.real/y.imag > x.real/x.imag;
    }
    else if(x.imag*y.imag<0){
        return x.imag<y.imag;
    }
}
bool operator<(const complex &x,const complex &y){
    return (y>x);
}
bool operator>=(const complex &x,const complex &y){
    return !(y>x);
}
bool operator<=(const complex &x,const complex &y){
    return !(x>y);
}
long long pow(long long n, long long p, long long k) {//n^k(mod p)
	if (!k) return 1;
	long long a = pow(n,p, k>>1);
	a = a * a%p;
	if (k & 1) a = a * n%p;
	return a;
}
void euclid(long long &a, long long &b, long long p) { // a>=b A*b+B*(a-a/b*b)=1
	if (a == 1) {
		a = 1;
	}
	else {
		long long A = b, B = a % b;
		euclid(A, B, p);
		b = (A - (p + a / b) % p * B % p + p) % p;
		a = B;
	}
}
long long rev(long long n, long long p) {//nの逆元 n*x-p*y=1
	//long long q = p;
	//euclid(p, n, p);
	//return n % q;
    return pow(n,p,p-2);
}
long long bino(long long n, long long m, long long p) {//nCm(mod p)
	long long me = 1;
	long long ans = 1;
	for (int i = 0;i < n + m;i++) {
		me = (long long)(i + 1) * ans % p;
		if (i == m - 1) {
			ans = (long long)rev(me, p) * ans%p;
		}
		if (i == m - n - 1) {
			ans = (long long)(rev(me, p))*ans%p;
		}
	}
	ans = (long long)(ans*me%p);
	return ans;
}
struct modint {
	long long num;
	long long p;
	modint() {
		num = 0;
		p = 1000000007;
	}
	modint(int x) {
		p = 1000000007;
		num = (long long)x%p;
	}
	modint(long long x) {
		p = 1000000007;
		num = x % p;
	}
	modint operator+(const modint &other) {
		modint ret;
		ret.p = p;
		ret.num = (num + other.num) % p;
		return ret;
	}
	modint operator+(const long long &other) {
		modint ret;
		ret.p = p;
		ret.num = (num + (other%p)) % p;
		return ret;
	}
	modint operator-(const modint &other) {
		modint ret;
		ret.p = p;
		ret.num = (num - other.num + p) % p;
		return ret;
	}
	modint operator-(const long long &other) {
		modint ret;
		ret.p = p;
		ret.num = (num - (other%p) + p) % p;
		return ret;
	}
	modint operator*(const modint &other) {
		modint ret;
		ret.p = p;
		ret.num = (num*other.num) % p;
		return ret;
	}
	modint operator*(const long long &other) {
		modint ret;
		ret.p = p;
		ret.num = (num*(other%p)) % p;
		return ret;
	}
	modint operator/(const modint &other) {
		modint ret;
		ret.p = p;
		ret.num = (num*rev(other.num, p)) % p;
		return ret;
	}
	modint operator/(const long long &other) {
		modint ret;
		ret.p = p;
		ret.num = (num*rev(other%p, p)) % p;
		return ret;
	}
	void operator+=(const long long &other) {
		num = (num + other) % p;
	}
	void operator+=(const modint &other) {
		num = (num + other.num) % p;
	}
	void operator-=(const long long &other) {
		num = (num - other + p) % p;
	}
	void operator-=(const modint &other) {
		num = (num - other.num + p) % p;
	}
	void operator*=(const long long &other) {
		num = (num*other) % p;
	}
	void operator*=(const modint &other) {
		num = (num*other.num) % p;
	}
};
template<typename T, typename S>
struct Mypair{
    T first;
    S second;
    Mypair(T _first, S _second){
        first = _first;
        second = _second;
    }
};
template<typename T, typename S>
bool operator<(const Mypair<T, S> &left, const Mypair<T, S> &right){
    if(left.first == right.first){
        return left.second > right.second;
    }
    else{
        return left.first < right.first;
    }
}
template<typename T, typename S> bool operator>(const Mypair<T,S> &left, const Mypair<T,S> &right){return right < left;}
template<typename T, typename S> bool operator>=(const Mypair<T,S> &left, const Mypair<T,S> &right){return !(left < right);}
template<typename T, typename S> bool operator<=(const Mypair<T,S> &left, const Mypair<T,S> &right){return !(right < left);}

int main() {
	int n;
	cin >> n;
	ll L = 0, R = INT_MAX, l = INT_MAX, r = 0;
	vector<P> da(n);
	rip(i, n, 0) {
		cin >> da[i].first >> da[i].second;
		da[i].second++;
		L = max(da[i].first, L);
		R = min(da[i].second, R);
		r = max(da[i].second, r);
		l = max(da[i].first, l);
	}
	ll ans = 0,c = -1;
	rip(i, n, 0) {
        if(ans < da[i].second - da[i].first){
            ans = da[i].second -da[i].first;
            c = i;
        }
	}
    rip(i,n,0){
        if(i!=c){
            r = min(da[i].second,r);
            l = max(da[i].first, l);
        }
    }
    ans += max(r-l,(ll)0);
	vector<P> rl(n * 2);
	rip(i, n, 0) {
		rl[i * 2].first = max(da[i].second - L, (ll)0);
		rl[i * 2 + 1].first = max(R - da[i].first, (ll)0);
		rl[i * 2].second = i + 1;
		rl[i * 2 + 1].second = -i - 1;
	}
	sort(all(rl));
	if (rl[n * 2 - 1].second + rl[n * 2 - 2].second == 0) {
		cout << R - L + da[rl[n * 2 - 1].second - 1].second - da[rl[n * 2 - 1].second - 1].first << endl;
		//cout << 0 << endl;
		return 0;
	}
	priority_queue<pair<P,int>, vector<pair<P, int>>, greater<pair<P, int>>> pq;
	rip(i, n * 2, 0) {
		if (rl[i].second > 0) {
			pq.push(mp(mp(rl[i].first,R - da[rl[i].second-1].first),rl[i].second));
		}
	}
	ll nn = R - da[pq.top().second - 1].first;
	pq.pop();
	while (!pq.empty()) {
		ans = max(ans, nn + pq.top().first.first);
		nn = min(nn, R - da[pq.top().second - 1].first);
		pq.pop();
		//cout << ans << endl;
	}
	/*rip(i,n*2,0){
		printf("%d %d\n",rl[i].first,rl[i].second);
	}*/
	cout << ans << endl;
}


Compiler output:
a.cc: In member function 'double complex::dist()':
a.cc:188:16: error: 'sqrt' was not declared in this scope
  188 |         return sqrt(real*real+imag*imag);
      |                ^~~~
a.cc: In function 'bool operator>(const complex&, const complex&)':
a.cc:234:1: warning: control reaches end of non-void function [-Wreturn-type]
  234 | }
      | ^