Submission #1831278
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
const int inf = 1e9;
int n, tot, MAX;
pair<int,int> z[N];
vector<int> G[N * 2];
int pos_seg[N];
int lst[N];
void add(int u, int v) {
G[-v + N].push_back(u + N);
G[-u + N].push_back(v + N);
}
// SEGMENT TREE
#define mid ((l + r) >> 1)
void build(int v, int l, int r) {
if (l > r) return;
if (l == r) {
int pos = z[l].second;
pos_seg[l] = v;
if (lst[pos]) add(-v, -lst[pos]), add(v, lst[pos]); else lst[pos] = v;
MAX = max(MAX, v);
return;
}
build(v << 1, l, mid); build(v << 1 | 1, mid + 1, r);
add(v, -(v << 1));
add(v, -(v << 1 | 1));
}
void join(int pos, int v, int l, int r, int L, int R) {
if (l > r || R < l || L > r) return;
if (L <= l && r <= R) { add(-pos, -v); return; }
join(pos, v << 1, l, mid, L, R); join(pos, v << 1 | 1, mid + 1, r, L, R);
}
#undef mid
// ------------------
// TWO-SAT
int color[N * 2];
int low[N * 2], num[N * 2], step;
bool invalid;
void setColor(int u, int c) { // FALSE = 1, TRUE = 2
u -= N;
if (color[u + N] == 3-c) invalid = true; else color[u + N] = c;
if (color[-u + N] == c) invalid = true; else color[-u + N] = 3-c;
}
stack<int> st;
void dfs(int u) {
low[u] = num[u] = ++step; st.push(u);
for (int i = 0; i < G[u].size(); ++i) {
int v = G[u][i];
if (num[v]) low[u] = min(low[u], num[v]);
else dfs(v), low[u] = min(low[u], low[v]);
if (color[v] == 1) setColor(u, 1); // FALSE
}
if (low[u] == num[u]) {
int v = 0;
if (!color[u]) setColor(u, 2); // TRUE
do {
v = st.top(); st.pop();
setColor(v, color[u]);
low[v] = num[v] = inf;
} while(v != u);
}
}
bool check(int d) {
// reset
for (int i = 1; i <= n; ++i) lst[i] = 0;
for (int i = 1; i <= MAX; ++i) G[i + N].clear(), color[i + N] = 0, low[i + N] = num[i + N] = 0;
for (int i = 1; i <= MAX; ++i) G[-i + N].clear(), color[-i + N] = 0, low[-i + N] = num[-i + N] = 0;
step = 0;
invalid = false;
build(1, 1, tot);
// solve
int ptr = 1;
for (int i = 1; i <= tot; ++i) {
while(ptr <= i && z[i].first - z[ptr].first >= d) ++ptr;
join(pos_seg[i], 1, 1, tot, ptr, i - 1);
//cerr << i << " [" << ptr << "," << i-1 << "]" << endl;
}
ptr = tot;
for (int i = tot; i >= 1; --i) {
while(ptr >= i && z[ptr].first - z[i].first >= d) --ptr;
join(pos_seg[i], 1, 1, tot, i + 1, ptr);
//cerr << i << " [" << i+1 << "," << ptr << "]" << endl;
}
for (int i = 1; i <= MAX; ++i) if (!num[i + N]) dfs(i + N);
for (int i = 1; i <= MAX; ++i) if (!num[-i + N]) dfs(-i + N);
return !invalid;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i) {
int x, y; cin >> x >> y;
z[++tot] = make_pair(x, i);
z[++tot] = make_pair(y, i);
}
sort(z + 1, z + tot + 1);
build(1, 1, tot);
int l = 0, r = 1e9;
while(l < r) {
int mid = ((l + r + 1) >> 1);
if (check(mid)) l = mid; else r = mid - 1;
}
cout << l << endl;
}
Submission Info
Submission Time |
|
Task |
F - Flags |
User |
cheater2k |
Language |
C++14 (GCC 5.4.1) |
Score |
1200 |
Code Size |
3017 Byte |
Status |
AC |
Exec Time |
810 ms |
Memory |
33068 KB |
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
1200 / 1200 |
Status |
|
|
Set Name |
Test Cases |
Sample |
00_example_01.txt, 00_example_02.txt, 00_example_03.txt |
All |
00_example_01.txt, 00_example_02.txt, 00_example_03.txt, 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt |
Case Name |
Status |
Exec Time |
Memory |
00_example_01.txt |
AC |
5 ms |
14592 KB |
00_example_02.txt |
AC |
4 ms |
14592 KB |
00_example_03.txt |
AC |
4 ms |
14592 KB |
01.txt |
AC |
18 ms |
15104 KB |
02.txt |
AC |
9 ms |
14848 KB |
03.txt |
AC |
5 ms |
14720 KB |
04.txt |
AC |
6 ms |
14720 KB |
05.txt |
AC |
6 ms |
14720 KB |
06.txt |
AC |
543 ms |
29200 KB |
07.txt |
AC |
543 ms |
29276 KB |
08.txt |
AC |
547 ms |
29348 KB |
09.txt |
AC |
17 ms |
15104 KB |
10.txt |
AC |
10 ms |
14848 KB |
11.txt |
AC |
6 ms |
14720 KB |
12.txt |
AC |
6 ms |
14720 KB |
13.txt |
AC |
6 ms |
14720 KB |
14.txt |
AC |
523 ms |
27388 KB |
15.txt |
AC |
527 ms |
27328 KB |
16.txt |
AC |
523 ms |
27328 KB |
17.txt |
AC |
18 ms |
15104 KB |
18.txt |
AC |
9 ms |
14848 KB |
19.txt |
AC |
575 ms |
29040 KB |
20.txt |
AC |
594 ms |
28988 KB |
21.txt |
AC |
711 ms |
29704 KB |
22.txt |
AC |
713 ms |
29756 KB |
23.txt |
AC |
810 ms |
33068 KB |
24.txt |
AC |
609 ms |
31680 KB |
25.txt |
AC |
640 ms |
31260 KB |
26.txt |
AC |
559 ms |
30432 KB |
27.txt |
AC |
5 ms |
14592 KB |