Problem:
Score : 1200 points

Problem Statement

Snuke loves flags.

Snuke is placing N flags on a line.

The i-th flag can be placed at either coordinate x_i or coordinate y_i.

Snuke thinks that the flags look nicer when the smallest distance between two of them, d, is larger. Find the maximum possible value of d.

Constraints

2 ≤ N ≤ 10^{4}

1 ≤ x_i, y_i ≤ 10^{9}

x_i and y_i are integers.

Input

The input is given from Standard Input in the following format:

N
x_1 y_1
:
x_N y_N

Output

Print the answer.

Sample Input 1

3
1 3
2 5
1 9

Sample Output 1

4

The optimal solution is to place the first flag at coordinate 1, the second flag at coordinate 5 and the third flag at coordinate 9. The smallest distance between two of the flags is 4 in this case.

Sample Input 2

5
2 2
2 2
2 2
2 2
2 2

Sample Output 2

0

There can be more than one flag at the same position.

Sample Input 3

22
93 6440
78 6647
862 11
8306 9689
798 99
801 521
188 206
6079 971
4559 209
50 94
92 6270
5403 560
803 83
1855 99
42 504
75 484
629 11
92 122
3359 37
28 16
648 14
11 269

Sample Output 3

17

Solution in C++ (GCC 9.2.1) (judge verdict: Time Limit Exceeded)

#include <bits/stdc++.h>
using namespace std;
#define mid ((s + e) / 2)
#define makefast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define comp(x) (lower_bound(vec.begin(), vec.end(), x) - vec.begin())
#define lc (v * 2)
#define rc (v * 2 + 1)
long long M = 1e9 + 7;
//>>>>>>>>>>>>>>>>>>>>>>>
 
const int N = 2e4 + 7;
 
int n, x[N], plc[N], d;
vector < pair <int, int> > vec;
 
pair <int, int> close(int y) {
	int l = lower_bound(vec.begin(), vec.end(), make_pair(y - d, 0)) - vec.begin();
	int r = upper_bound(vec.begin(), vec.end(), make_pair(y + d, N)) - vec.begin();
	return {l, r};
}
bool mrk[N];
set <int> seg[N * 6];
 
void put(int p, int u, int tp) {
//	cout << "put :: " << p << ' ' << u << ' ' << tp << endl;
	 p += (1 << 15);
	 while (p) {
	 	if (tp == 1)
			seg[p].insert(u);
		else
			seg[p].erase(u);
		p /= 2;
	 }

}
 
int get(int l, int r) {
	if (r <= l)
		return 0;
	l += (1 << 15);
	r += (1 << 15) - 1;
	if (seg[l].size())
		return *seg[l].begin();
	if (seg[r].size())
		return *seg[r].begin();
	while (l / 2 != r / 2) {
		if (l % 2 == 0 && seg[l + 1].size())
			return *seg[l + 1].begin();
		if (r % 2 == 1 && seg[r - 1].size())
			return *seg[r - 1].begin();
		l /= 2;
		r /= 2;
	}
	return 0;
}
vector <int> st;
 
void dfs(int v) {
	mrk[v] = 1;
	bool ok = 1;
	while (ok) {
		ok = 0;
		int u = get(close(x[v]).first, plc[v]);
		if (u) {
			put(plc[u ^ 1], u, -1);
			dfs(u);
			ok = 1;
			continue;
		}
		u = get(plc[v] + 1, close(x[v]).second);
		if (u) {
			put(plc[u ^ 1], u, -1);
			dfs(u);
			ok = 1;
		}
	}
	st.push_back(v);
}
int com[N];
int cnt;
 
void rev(int v) {
	com[v] = cnt;
	bool ok = 1;
	while (ok) {
		ok = 0;
		int u = get(close(x[v ^ 1]).first, plc[v ^ 1]);
		if (u) {
			put(plc[u], u, -1);
			rev(u);
			ok = 1;
			continue;
		}
		u = get(plc[v ^ 1] + 1, close(x[v ^ 1]).second);
		if (u) {
			put (plc[u], u, -1);
			rev(u);
			ok = 1;
			continue;
		}
	}
}
 
bool can() {
	cnt = 0;
	memset(mrk, 0, sizeof mrk);
	for (int i = 1; i < 4 * N; i++) {
		if (seg[i].size()) {
			cout << " sz " << endl;
			exit(0);
		}
	}
	for (int i = 2; i <= 2 * n + 1; i++)
		put(plc[i ^ 1], i, 1);	
	for (int i = 2; i <= 2 * n + 1; i++) 
		if (!mrk[i]) {
			put(plc[i ^ 1], i, -1);
			dfs(i);
		}
	memset(com, 0, sizeof com);
	for (int i = 1; i < 4 * N; i++) {
		if (seg[i].size()) {
			cout << " sz " << endl;
			exit(0);
		}
	}
	for (int i = 2; i <= 2 * n + 1; i++)
		put(plc[i], i, 1);
	while (st.size()) {	
		int v = st.back();
		st.pop_back();
		if (com[v])
			continue;
		cnt++;
		put(plc[v], v, -1);
		rev(v);
	}
	for (int i = 2; i <= 2 * n + 1; i++) 
		if (com[i] == com[i ^ 1]) {
			return 0;
		}
	return 1;
}
 
int main() {
	makefast;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> x[2 * i] >> x[2 * i + 1];
		vec.push_back({x[2 * i], 2 * i});
		vec.push_back({x[2 * i + 1], 2 * i + 1});
	}
	sort (vec.begin(), vec.end());
	for (int i = 0; i < vec.size(); i++) {
		plc[vec[i].second] = i;
	}
	int l = -1, r = 1e9 + 1;
	while (r - l > 1) {
		d = (l + r) / 2;
		if (can())
			l = d;
		else
			r = d;
	}
	cout << l + 1;
}