Problem Description
The last trial Venus imposes on Psyche is a quest to the underworld. She is to take a box and obtain in it a dose of the beauty of Prosperina, queen of the underworld.
There are n palaces in the underworld, which can be located on a 2-Dimension plane with (x,y) coordinates (where x,y are integers). Psyche would like to find the distance of the closest pair of two palaces. It is the password to enter the main palace.
However, the underworld is mysterious and changes all the time. At different times, exactly one of the n palaces disappears.
Psyche wonders what the distance of the closest pair of two palaces is after some palace has disappeared.
Print the sum of the distance after every single palace has disappeared.
To avoid floating point error, define the distance d between palace (x1,y1) and (x2,y2) as d=(x1−x2)2+(y1−y2)2 .
There are n palaces in the underworld, which can be located on a 2-Dimension plane with (x,y) coordinates (where x,y are integers). Psyche would like to find the distance of the closest pair of two palaces. It is the password to enter the main palace.
However, the underworld is mysterious and changes all the time. At different times, exactly one of the n palaces disappears.
Psyche wonders what the distance of the closest pair of two palaces is after some palace has disappeared.
Print the sum of the distance after every single palace has disappeared.
To avoid floating point error, define the distance d between palace (x1,y1) and (x2,y2) as d=(x1−x2)2+(y1−y2)2 .
Input
The first line of the input contains an integer
T
(1≤T≤5)
, which denotes the number of testcases.
For each testcase, the first line contains an integers n (3≤n≤105) , which denotes the number of temples in this testcase.
The following n lines contains n pairs of integers, the i -th pair (x,y) (−105≤x,y≤105) denotes the position of the i -th palace.
For each testcase, the first line contains an integers n (3≤n≤105) , which denotes the number of temples in this testcase.
The following n lines contains n pairs of integers, the i -th pair (x,y) (−105≤x,y≤105) denotes the position of the i -th palace.
Output
For each testcase, print an integer which denotes the sum of the distance after every single palace has disappeared.
Sample Input
1 3 0 0 1 1 2 2
Sample Output
12HintIf palace $ (0,0) $ disappears,$ d = (1-2) ^ 2 + (1 - 2) ^ 2 = 2 $; If palace $ (1,1) $ disappears,$ d = (0-2) ^ 2 + (0 - 2) ^ 2 = 8 $; If palace $ (2,2) $ disappears,$ d = (0-1) ^ 2 + (0-1) ^ 2 = 2 $; Thus the answer is $ 2 + 8 + 2 = 12 $。先找出平面最近点对,然后去掉其他点不会影响结果,去掉这两个点的再算一遍#include<set> #include<map> #include<cmath> #include<stack> #include<queue> #include<bitset> #include<cstdio> #include<string> #include<cstring> #include<iostream> #include<algorithm> #include<functional> #define rep(i,j,k) for (int i = j; i <= k; i++) #define per(i,j,k) for (int i = j; i >= k; i--) using namespace std; typedef __int64 LL; const int low(int x) { return x&-x; } const int N = 1e5 + 10; const int mod = 998244353; const int INF = 0x7FFFFFFF; int T, n, x, y; struct point { int x, y, id; void read() { scanf("%d%d", &x, &y); } bool operator<(const point&a)const { return x < a.x; } }a[N], b[N]; bool cmp(const point&a, const point&b) { return a.y < b.y; } LL dis(point&a, point&b) { return 1LL * (a.x - b.x)*(a.x - b.x) + 1LL * (a.y - b.y)*(a.y - b.y); } LL solve(int l, int r, int &x, int &y) { if (l + 1 == r) { sort(a + l, a + r + 1, cmp); x = a[l].id; y = a[r].id; return dis(a[l], a[r]); } if (l + 2 == r) { sort(a + l, a + r + 1, cmp); if (dis(a[l], a[l + 1]) <= dis(a[l], a[r]) && dis(a[l], a[l + 1]) <= dis(a[r - 1], a[r])) { x = a[l].id; y = a[l + 1].id; return dis(a[l], a[l + 1]); } if (dis(a[l], a[r]) <= dis(a[l + 1], a[l]) && dis(a[l], a[r]) <= dis(a[r - 1], a[r])) { x = a[l].id; y = a[r].id; return dis(a[l], a[r]); } if (dis(a[r], a[r - 1]) <= dis(a[l], a[r]) && dis(a[r], a[r - 1]) <= dis(a[r - 1], a[l])) { x = a[r].id; y = a[r - 1].id; return dis(a[r], a[r - 1]); } } int mid = l + r >> 1, tot = 0; int q, w, e, rr; LL d1 = solve(l, mid, q, w), d2 = solve(mid + 1, r, e, rr); LL d = min(d1, d2); if (d1 < d2) x = q, y = w; else x = e, y = rr; for (int i = mid + 1; i <= r; i++) if (a[i].x - a[mid].x <= sqrt(1.0*d)) b[tot++] = a[i]; for (int i = l, j = 0; i <= mid; i++) { for (; j < tot&&a[i].y - b[j].y > d; j++); for (int k = j; k < tot&&b[k].y - a[i].y < d; k++) { if (d > dis(a[i], b[k])) { d = dis(a[i], b[k]); x = a[i].id; y = b[k].id; } } } for (int i = l, j = l, k = mid + 1; i <= r; i++) { if (j <= mid && (k>r || a[j].y < a[k].y)) b[i] = a[j++]; else b[i] = a[k++]; } for (int i = l; i <= r; i++) a[i] = b[i]; return d; } LL solve(int l, int r) { if (l + 1 == r) { sort(a + l, a + r + 1, cmp); return dis(a[l], a[r]); } if (l + 2 == r) { sort(a + l, a + r + 1, cmp); return min(dis(a[l], a[r]), min(dis(a[l], a[l + 1]), dis(a[r], a[r - 1]))); } int mid = l + r >> 1, tot = 0; LL d = min(solve(l, mid), solve(mid + 1, r)); for (int i = mid + 1; i <= r; i++) if (a[i].x - a[mid].x <= sqrt(1.0 * d)) b[tot++] = a[i]; for (int i = l, j = 0; i <= mid; i++) { for (; j < tot&&a[i].y - b[j].y > d; j++); for (int k = j; k < tot&&b[k].y - a[i].y < d; k++) { if (d > dis(a[i], b[k])) d = dis(a[i], b[k]); } } for (int i = l, j = l, k = mid + 1; i <= r; i++) { if (j <= mid && (k>r || a[j].y < a[k].y)) b[i] = a[j++]; else b[i] = a[k++]; } for (int i = l; i <= r; i++) a[i] = b[i]; return d; } int main() { scanf("%d", &T); while (T--) { scanf("%d", &n); for (int i = 0; i < n; i++) a[i].read(), a[i].id = i; sort(a, a + n); LL ans = 1LL * (n - 2)*solve(0, n - 1, x, y); for (int i = 0; i < n - 1; i++) if (a[i].id == x) swap(a[i], a[n - 1]); sort(a, a + n - 1); ans += solve(0, n - 2); for (int i = 0; i < n - 1; i++) if (a[i].id == y) swap(a[i], a[n - 1]); sort(a, a + n - 1); ans += solve(0, n - 2); printf("%I64d\n", ans); } }