Language: C++

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <limits>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <functional>

using namespace std;

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif
using ll = long long;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vvvll = std::vector<vvll>;
#define reps(i, S, E) for (ll i = (S); i <= (E); i++)
#define rep(i, N) reps(i, 0, N-1)
#define deps(i, E, S) for (ll i = (E); i >= (S); i--)
#define dep(i, N) deps(i, N-1, 0)
const ll MOD = 1000000007;
const ll INF = 1LL << 60;


//AtCoder Grand Contest 040 - B
template< typename Monoid >
struct SegmentTree {
	using F = function< Monoid(Monoid, Monoid) >;

	int sz;
	vector< Monoid > seg;

	const F f;
	const Monoid M1;

	SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
		sz = 1;
		while (sz < n) sz <<= 1;
		seg.assign(2 * sz, M1);
	}

	void set(int k, const Monoid &x) {
		seg[k + sz] = x;
	}

	void build() {
		for (int k = sz - 1; k > 0; k--) {
			seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
		}
	}

	void update(int k, const Monoid &x) {
		k += sz;
		seg[k] = x;
		while (k >>= 1) {
			seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
		}
	}

	Monoid query(int a, int b) {
		Monoid L = M1, R = M1;
		for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
			if (a & 1) L = f(L, seg[a++]);
			if (b & 1) R = f(seg[--b], R);
		}
		return f(L, R);
	}

	Monoid operator[](const int &k) const {
		return seg[k + sz];
	}

	template< typename C >
	int find_subtree(int a, const C &check, Monoid &M, bool type) {
		while (a < sz) {
			Monoid nxt = type ? f(seg[2 * a + type], M) : f(M, seg[2 * a + type]);
			if (check(nxt)) a = 2 * a + type;
			else M = nxt, a = 2 * a + 1 - type;
		}
		return a - sz;
	}

	template< typename C >
	int find_first(int a, const C &check) {
		Monoid L = M1;
		if (a <= 0) {
			if (check(f(L, seg[1]))) return find_subtree(1, check, L, false);
			return -1;
		}
		int b = sz;
		for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
			if (a & 1) {
				Monoid nxt = f(L, seg[a]);
				if (check(nxt)) return find_subtree(a, check, L, false);
				L = nxt;
				++a;
			}
		}
		return -1;
	}

	template< typename C >
	int find_last(int b, const C &check) {
		Monoid R = M1;
		if (b >= sz) {
			if (check(f(seg[1], R))) return find_subtree(1, check, R, true);
			return -1;
		}
		int a = sz;
		for (b += sz; a < b; a >>= 1, b >>= 1) {
			if (b & 1) {
				Monoid nxt = f(seg[--b], R);
				if (check(nxt)) return find_subtree(b, check, R, true);
				R = nxt;
			}
		}
		return -1;
	}
};

//SegmentTree(n, f, M1):= �T�C�Y n �̏������B������ f ��2�̋�Ԃ̗v�f���}�[�W����񍀉��Z, M1 �̓��m�C�h�̒P�ʌ��ł���B
//set(k, x):= k �Ԗڂ̗v�f�� x ��������B
//build():= �Z�O�����g�؂��\�z����B
//query(a, b):= ��� [a,b) �ɑ΂��ē񍀉��Z�������ʂ�Ԃ��B
//update(k, x):= k �Ԗڂ̗v�f�� x �ɕύX����B
//operator[k] := k �Ԗڂ̗v�f��Ԃ��B
//find_first(a, check) := [a,x) �� check �𖞂����ŏ��̗v�f�ʒu x ��Ԃ��B
//find_last(b, check) := [x,b) �� check �𖞂����Ō�̗v�f�ʒu x ��Ԃ��B

//�g�p��
//int main() {
//	int N, Q;
//	scanf("%d %d", &N, &Q);
//	SegmentTree< int > seg(N, [](int a, int b) { return min(a, b); }, INT_MAX);
//	while(Q--) {
//		int T, X, Y;
//		scanf("%d %d %d", &T, &X, &Y);
//		if(T == 0) seg.update(X, Y);
//		else printf("%d\n", seg.query(X, Y + 1));
//	}
//}

struct LLPair {
	int a = 0;
	int b = 0;
	LLPair() {}
	LLPair(int a, int b) {
		this->a = a;
		this->b = b;
	}
};

int main() {
	int N;
	scanf("%d", &N);
	vector<LLPair> P(N);
	rep(i, N) {
		int l;
		scanf("%d", &l);
		int r;
		scanf("%d", &r);
		P[i].a = l;
		P[i].b = r;
	}

	std::sort(P.begin(), P.end(), [](const LLPair &a, const LLPair &b) {
		return a.a < b.a; //�~���ɂ���ɂ� > �Ƃ���
	});

	SegmentTree<int> segMin(N, [](int a, int b) { return min(a, b); }, INT_MAX);
	rep(i, N) {
		segMin.update(i, P[i].b);
	}

	int result = 0;

	rep(i, N - 1) {
		//i���O���[�v1�Ƃ���
		//i+1�`N-1�܂ł��O���[�v2�Ƃ���
		//0�`i-1���O���[�v1��2�Ɋ��蓖�Ă�

		int g1l = P[i].a;
		int g2l = P[N - 1].a;

		if (i == 0) {
			int g1r = P[i].b;
			int g2r = segMin.query(i + 1, N);
			result = max(result, max(0, g1r - g1l + 1) + max(0, g2r - g2l + 1));
		}
		else {
			//0�`i-1���O���[�v1�Ɋ��蓖�Ă�
			{
				int g1r = min(P[i].b, segMin.query(0, i));
				int g2r = segMin.query(i + 1, N);
				result = max(result, max(0, g1r - g1l + 1) + max(0, g2r - g2l + 1));
			}

			//0�`i-1���O���[�v2�Ɋ��蓖�Ă�
			{
				int g1r = P[i].b;
				int g2r = min(segMin.query(0, i), segMin.query(i + 1, N));
				result = max(result, max(0, g1r - g1l + 1) + max(0, g2r - g2l + 1));
			}
		}
	}

	printf("%d\n", result);
}


//AtCoder Beginner Contest 018 - D
/*
// nCk�̑g�ݍ��킹�ɑ΂��ď��������s����
void recursive_comb(int *indexes, int s, int rest, std::function<void(int *)> f) {
	if (rest == 0) {
		f(indexes);
	}
	else {
		if (s < 0) return;
		recursive_comb(indexes, s - 1, rest, f);
		indexes[rest - 1] = s;
		recursive_comb(indexes, s - 1, rest - 1, f);
	}
}

void foreach_comb(int n, int k, std::function<void(int *)> f) {
	int *indexes = new int[k];
	recursive_comb(indexes, n - 1, k, f);
	delete[] indexes;
}

struct LLPair {
	ll a = 0;
	ll b = 0;
	LLPair() {}
	LLPair(ll a, ll b) {
		this->a = a;
		this->b = b;
	}
};

ll N;
ll M;
ll P;
ll Q;
ll R;
vll x;
vll y;
vll z;
ll choco[20][20];
ll result = 0;

int main() {
	scanf("%lld", &N);
	scanf("%lld", &M);
	scanf("%lld", &P);
	scanf("%lld", &Q);
	scanf("%lld", &R);
	x.resize(R);
	y.resize(R);
	z.resize(R);
	rep(i, R) {
		ll a;
		scanf("%lld", &a);
		ll b;
		scanf("%lld", &b);
		ll c;
		scanf("%lld", &c);
		x[i] = a - 1;
		y[i] = b - 1;
		z[i] = c;
	}

	memset(choco, 0, sizeof(choco));
	rep(i, R) {
		choco[x[i]][y[i]] = z[i];
	}

	//���s��
	foreach_comb(N, P, [](int *indexes) {
		//std::cout << indexes[0] << ',' << indexes[1] << ',' << indexes[2] << ',' << indexes[3] << ',' << indexes[4] << std::endl;
		vector<LLPair> men(M); //�j�q[i]���Q�����邱�Ƃɂ��K���x
		rep(i, M) {
			men[i].a = i;
			men[i].b = 0;
		}

		rep(i, P) {
			rep(j, M) {
				men[j].b += choco[indexes[i]][j];
			}			
		}

		std::sort(men.begin(), men.end(), [](const LLPair &a, const LLPair &b) {
			return a.b > b.b; //�~���ɂ���ɂ� > �Ƃ���
		});

		ll happy = 0;
		rep(i, Q) {
			happy += men[i].b;
		}

		result = max(result, happy);
	});

	printf("%lld\n", result);
}
*/

//AtCoder Regular Contest 088
/*
int main() {
	char S[101010];
	scanf("%s", S);
	ll len = strlen(S);

	vll cont;
	char prev = ' ';
	ll count = 0;
	rep(i, len) {
		if (S[i] != prev && i > 0) {
			cont.push_back(count);
			count = 1;
		}
		else {
			count++;
		}

		prev = S[i];
	}
	cont.push_back(count);

	ll result = 0;
	if (cont.size() == 1) {
		result = cont[0];
	}
	else {
		//result = max(result, cont[0]);
		//result = max(result, cont[cont.size() - 1]);
		//reps(i, 1, cont.size() - 2) {
		//	ll temp = min(cont[i - 1] + cont[i], cont[i] + cont[i + 1]);
		//	result = max(result, temp);
		//}

		vll sumLeft(cont.size(), 0);
		sumLeft[0] = cont[0];
		reps(i, 1, cont.size() - 1) {
			sumLeft[i] = sumLeft[i - 1] + cont[i];
		}
		vll sumRight(cont.size(), 0);
		sumRight[cont.size() - 1] = cont[cont.size() - 1];
		deps(i, cont.size() - 2, 0) {
			sumRight[i] = sumRight[i + 1] + cont[i];
		}
		rep(i, cont.size()) {
			result = max(result, min(sumLeft[i], sumRight[i]));
		}
	}

	printf("%lld\n", result);
}
*/

//AtCoder Beginner Contest 038 - D
/*
struct LLPair {
	ll w = 0;
	ll h = 0;
	LLPair() {}
	LLPair(ll w, ll h) {
		this->w = w;
		this->h = h;
	}
};

int main() {
	ll N;
	scanf("%lld", &N);
	vector<LLPair> P(N);
	vll sameWidthCount(101010, 0);
	ll numOfWidth = 0;
	rep(i, N) {
		ll w;
		scanf("%lld", &w);
		ll h;
		scanf("%lld", &h);
		P[i].w = w;
		P[i].h = h;
		if (sameWidthCount[w] == 0) {
			numOfWidth++;
		}
		sameWidthCount[w]++;
	}

	std::sort(P.begin(), P.end(), [](const LLPair &a, const LLPair &b) {
		if (a.w == b.w) {
			return a.h > b.h; //�~���ɂ���ɂ� > �Ƃ���
		}
		else {
			return a.w < b.w; //�~���ɂ���ɂ� > �Ƃ���
		}
	});

	rep(j, N) {
		eprintf("%lld ", P[j].w);
	}
	eprintf("\n");
	rep(j, N) {
		eprintf("%lld ", P[j].h);
	}
	eprintf("\n\n");

	vll dp(N, INF); //dp[h / w][�Œ���]
	int iP = 0;

	rep(i, numOfWidth) {
		ll loopCount = sameWidthCount[P[iP].w];
		vll writeIndex;
		vll writeValue;

		rep(j, loopCount) {
			ll index = lower_bound(dp.begin(), dp.end(), P[iP].h) - dp.begin();
			writeIndex.push_back(index);
			writeValue.push_back(P[iP].h);
			iP++;
		}
		rep(j, loopCount) {
			dp[writeIndex[j]] = writeValue[j];
		}

		rep(j, N) {
			dp[j] == INF ? eprintf("- ") : eprintf("%lld ", dp[j]);
		}
		eprintf("\n");
	}

	ll result = lower_bound(dp.begin(), dp.end(), INF) - dp.begin();
	printf("%lld\n", result);
}
*/


//AtCoder Beginner Contest 126 - F
/*
ll LLpow(ll x, ll n) {
	ll ret = 1;
	while (n > 0) {
		if (n & 1) {
			ret *= x;
		}
		x *= x;
		n >>= 1;
	}
	return ret;
}

int main() {
	ll M;
	scanf("%lld", &M);
	ll K;
	scanf("%lld", &K);

	if (M == 0) {
		if (K > 0) {
			printf("-1\n");
			return 0;
		}

		printf("0 0\n");
		return 0;
	}
	else if (M == 1) {
		if (K > 0) {
			printf("-1\n");
			return 0;
		}

		printf("0 0 1 1\n");
		return 0;
	}
	else {
		ll N = LLpow(2, M);
		if (K >= N) {
			printf("-1\n");
			return 0;
		}

		vll result;

		result.push_back(K);
		rep(i, N) {
			if (i != K) {
				result.push_back(i);
			}
		}
		result.push_back(K);
		dep(i, N) {
			if (i != K) {
				result.push_back(i);
			}
		}
		for (auto val : result) {
			printf("%lld ", val);
		}
		printf("\n");
	}
}
*/

//AtCoder Beginner Contest 116 - D
/*
struct LLPair {
	ll t = 0;
	ll d = 0;
	LLPair() {}
	LLPair(ll t, ll d) {
		this->t = t;
		this->d = d;
	}
};

bool operator< (const LLPair &p1, const LLPair &p2) {
	return p1.d > p2.d;
};

int main() {
	ll N;
	scanf("%lld", &N);
	ll K;
	scanf("%lld", &K);
	vector<LLPair> pair(N);
	priority_queue<LLPair> pq; //�l�^�̔��������Ⴂ��
	vll count(N, 0);
	rep(i, N) {
		ll t;
		scanf("%lld", &t);
		ll d;
		scanf("%lld", &d);
		pair[i].t = t - 1;
		pair[i].d = d;
	}

	std::sort(pair.begin(), pair.end(), [](const LLPair &a, const LLPair &b) {
		return a.d > b.d; //�~���ɂ���ɂ� > �Ƃ���
	});

	ll sum_d = 0;
	ll kind = 0;

	rep(i, K) {
		sum_d += pair[i].d;
		if (count[pair[i].t] == 0) {
			kind++;
		}
	
		count[pair[i].t]++;
		pq.push(pair[i]);
	}

	ll result = sum_d + kind * kind;

	ll iPair = K;
	while (pq.empty() == false) {
		//��菜�����̂�I��
		LLPair item = pq.top();
		pq.pop();

		if (count[item.t] == 1) {
			//���X�P�̏ꍇ�́A��菜���Ǝ�ސ�������̂ŃX�L�b�v
			continue;
		}

		count[item.t]--;
		sum_d -= item.d;

		//pair[i]�����ɓ����
		while (iPair < N) {
			if (count[pair[iPair].t] > 0) {
				//���łɂ��̎�ނ̂��̂����݂���ꍇ�͎�ސ��������Ȃ��̂ŃX�L�b�v
				iPair++;
				continue;
			}

			count[pair[iPair].t]++;
			sum_d += pair[iPair].d;
			kind++;

			result = max(result, sum_d + kind * kind);

			iPair++;
			break;
		}

		if (iPair == N) {
			break;
		}
	}

	printf("%lld\n", result);
}
*/

//AtCoder Beginner Contest 065 - D
/*
struct LLPoint {
	ll x = 0;
	ll y = 0;
	ll no = 0;
	LLPoint() {}
	LLPoint(ll no, ll x, ll y) {
		this->no = no;
		this->x = x;
		this->y = y;
	}
};

// �O���t�e���v���[�g
template<typename T>
struct edge {
	int src, to;
	T cost;
	edge(int to, T cost) : src(-1), to(to), cost(cost) {}
	edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}
	edge &operator=(const int &x) {
		to = x;
		return *this;
	}
	operator int() const { return to; }
};
template<typename T>
using Edges = vector<edge<T>>;
template<typename T>
using WeightedGraph = vector<Edges<T>>;
using UnWeightedGraph = vector<vector<int>>;
template<typename T>
using Matrix = vector<vector<T >>;

//Union-Find : O(��(N))
struct UnionFind {
	vector<int> data;
	UnionFind(int sz) {
		data.assign(sz, -1);
	}
	bool unite(int x, int y) {
		x = find(x), y = find(y);
		if (x == y) return (false);
		if (data[x] > data[y]) swap(x, y);
		data[x] += data[y];
		data[y] = x;
		return (true);
	}
	int find(int k) {
		if (data[k] < 0) {
			return (k);
		}
		return (data[k] = find(data[k]));
	}
	int size(int k) {
		return (-data[find(k)]);
	}
};

//�ŏ��S���(Kruskal) : O(E * logV)
template<typename T>
T kruskal(Edges<T> &edges, int V) {
	sort(begin(edges), end(edges), [](const edge<T> &a, const edge<T> &b) {
		return (a.cost < b.cost);
	});
	UnionFind tree(V);
	T ret = 0;
	for (auto &e : edges) {
		if (tree.unite(e.src, e.to)) ret += e.cost;
	}
	return (ret);
}

int main() {
	ll N;
	scanf("%lld", &N);
	vector<LLPoint> xSort(N);
	vector<LLPoint> ySort(N);
	rep(i, N) {
		ll x;
		scanf("%lld", &x);
		ll y;
		scanf("%lld", &y);
		xSort[i].no = i;
		xSort[i].x = x;
		xSort[i].y = y;
		ySort[i].no = i;
		ySort[i].x = x;
		ySort[i].y = y;
	}

	std::sort(xSort.begin(), xSort.end(), [](const LLPoint &a, const LLPoint &b) {
		return a.x < b.x; //�~���ɂ���ɂ� > �Ƃ���
	});

	std::sort(ySort.begin(), ySort.end(), [](const LLPoint &a, const LLPoint &b) {
		return a.y < b.y; //�~���ɂ���ɂ� > �Ƃ���
	});

	Edges<ll> edges;

	rep(i, N - 1) {
		edges.emplace_back(xSort[i].no, xSort[i + 1].no, abs(xSort[i].x - xSort[i + 1].x));
		edges.emplace_back(ySort[i].no, ySort[i + 1].no, abs(ySort[i].y - ySort[i + 1].y));
	}

	ll result = kruskal(edges, (int)N);

	printf("%lld\n", result);
}
*/


Compiler output:
a.cc: In function 'int main()':
a.cc:178:76: error: 'INT_MAX' was not declared in this scope
  178 |         SegmentTree<int> segMin(N, [](int a, int b) { return min(a, b); }, INT_MAX);
      |                                                                            ^~~~~~~
a.cc:13:1: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
   12 | #include <functional>
  +++ |+#include <climits>
   13 |