1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
#include <bits/stdc++.h>
#define For(i, l, r) for (long long i = (l), i##end = (r); i <= i##end; ++i)
#define Fordown(i, r, l) for (long long i = (r), i##end = (l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
using namespace std;
const long long maxn = 100010;
template<typename type>
type read() {
type s = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9') {
if (ch == '-') f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0';
ch = getchar();
}
return s * f;
}
struct PNT {
int x, y, id;
} p[maxn];
bool cmpx(PNT a, PNT b) {
return a.x < b.x;
}
bool cmpy(PNT a, PNT b) {
return a.y < b.y;
}
int getDis(PNT a, PNT b) {
return min(abs(a.x - b.x), abs(a.y - b.y));
}
class DJU {
public:
int fa[maxn], sz[maxn];
void init(int n) {
For(i, 1, n) {
fa[i] = i;
sz[i] = 1;
}
}
int findFa(int a) {
if (fa[a] != a)
return fa[a] = findFa(fa[a]);
return a;
}
void merge(int a, int b) {
int faa = findFa(a);
int fab = findFa(b);
if (faa != fab) {
fa[fab] = faa;
sz[faa] += sz[fab];
}
}
} dj;
int bktx[400001], bkty[400001];
vector<pair<int, int> > e;
int n;
int main() {
n = read<int>();
dj.init(n);
bool flg = 1;
int lst = 0;
For(i, 1, n) {
p[i].x = read<int>();
p[i].y = read<int>();
p[i].id = i;
int x = p[i].x, y = p[i].y;
p[i].x = x - y;
p[i].y = y + x;
}
For(i, 1, n) {
if ((bktx[p[i].x + 200000] != 0) && (bkty[p[i].y + 200000] != 0)) {
dj.merge(bktx[p[i].x + 200000], p[i].id);
dj.merge(bktx[p[i].x + 200000], bkty[p[i].y + 200000]);
} else {
if (bktx[p[i].x + 200000] == 0) {
bktx[p[i].x + 200000] = p[i].id;
} else {
dj.merge(bktx[p[i].x + 200000], p[i].id);
}
if (bkty[p[i].y + 200000] == 0) {
bkty[p[i].y + 200000] = p[i].id;
} else {
dj.merge(bkty[p[i].y + 200000], p[i].id);
}
}
}
int ans = 0x3f3f3f3f;
int cnt = 0;
sort(p + 1, p + 1 + n, cmpx);
For(i, 1, n - 1) {
For(j, i + 1, n) {
if (dj.findFa(p[i].id) != dj.findFa(p[j].id) && getDis(p[i], p[j]) != 0) {
if (ans > getDis(p[i], p[j])) {
ans = getDis(p[i], p[j]);
e.clear();
int pa, pb;
pa = dj.findFa(p[i].id);
pb = dj.findFa(p[j].id);
if (pa > pb) swap(pa, pb);
e.push_back(make_pair(pa, pb));
} else {
if (ans == getDis(p[i], p[j])) {
int pa, pb;
pa = dj.findFa(p[i].id);
pb = dj.findFa(p[j].id);
if (pa > pb) swap(pa, pb);
e.push_back(make_pair(pa, pb));
}
}
break;
}
}
}
sort(p + 1, p + 1 + n, cmpy);
For(i, 1, n - 1) {
For(j, i + 1, n) {
if (dj.findFa(p[i].id) != dj.findFa(p[j].id) && getDis(p[i], p[j]) != 0) {
if (ans > getDis(p[i], p[j])) {
ans = getDis(p[i], p[j]);
e.clear();
int pa, pb;
pa = dj.findFa(p[i].id);
pb = dj.findFa(p[j].id);
if (pa > pb) swap(pa, pb);
e.push_back(make_pair(pa, pb));
} else {
if (ans == getDis(p[i], p[j])) {
int pa, pb;
pa = dj.findFa(p[i].id);
pb = dj.findFa(p[j].id);
if (pa > pb) swap(pa, pb);
e.push_back(make_pair(pa, pb));
}
}
break;
}
}
}
sort(e.begin(), e.end());
vector<pair<int, int>>::iterator it = unique(e.begin(), e.end());
e.erase(it, e.end());
int ansbig = 0;
for (auto i : e) {
ansbig += dj.sz[i.first] * dj.sz[i.second];
}
cout << ans << endl;
cout << ansbig << endl;
return 0;
}
|