Palace

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 404    Accepted Submission(s): 104

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   palaces in the underworld, which can be located on a 2-Dimension plane with  (x,y coordinates (where  x, 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   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   between palace  (x1,y1 and  (x2,y2 as  d=(x1x2)2+(y1y2).

Input
The first line of the input contains an integer    (1T5, which denotes the number of testcases.

For each testcase, the first line contains an integers    (3n105, which denotes the number of temples in this testcase.

The following   lines contains   pairs of integers, the  -th pair  (x,y  (105x,y105 denotes the position of the  -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
12
Hint
If 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 \$。

Source

```#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fwrite(ch) freopen(ch,"w",stdout)

using namespace std;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int msz = 100100;
const int mod = 1e9+7;
const double eps = 1e-8;

struct Point
{
LL x,y;
int id;
};

LL dist(Point a,Point b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}

Point p[msz];
Point tmpt[msz];

bool cmpxy(Point a,Point b)
{
if(a.x != b.x) return a.x < b.x;
return a.y < b.y;
}

bool cmpy(Point a,Point b)
{
return a.y < b.y;
}

LL d;
int pt[2];

void Closest_Pair(int left,int right,int opt)
{
if(left == right) return;
LL tmp;
if(left + 1 == right)
{
if(p[left].id != opt && p[right].id != opt)
{
tmp = dist(p[left],p[right]);
if(d > tmp)
{
d = tmp;
if(opt == -1)
{
pt[0] = p[left].id;
pt[1] = p[right].id;
}
}
}
return;
}

int mid = (left+right)/2;
Closest_Pair(left,mid,opt);
Closest_Pair(mid+1,right,opt);

int k = 0;

for(int i = left; i <= right; ++i)
{
if(p[i].id != opt && abs(p[mid].x - p[i].x) <= d)
tmpt[k++] = p[i];
}

sort(tmpt,tmpt+k,cmpy);

for(int i = 0; i < k; ++i)
{
for(int j = i+1; j < k && tmpt[j].y - tmpt[i].y < d; ++j)
{
tmp = dist(tmpt[i],tmpt[j]);
if(d > tmp)
{
if(opt == -1)
{
pt[0] = tmpt[i].id;
pt[1] = tmpt[j].id;
}
d = tmp;
}
}
}
}

int main()
{
//fwrite("");

int t,n;

scanf("%d",&t);

while(t--)
{
scanf("%d",&n);
for(int i = 0; i < n; ++i)
{
scanf("%lld%lld",&p[i].x,&p[i].y);
p[i].id = i;
}

sort(p,p+n,cmpxy);

d = INF;
Closest_Pair(0,n-1,-1);
LL ans = 1LL*d*(n-2);

d = INF;
Closest_Pair(0,n-1,pt[0]);
ans += d;

d = INF;
Closest_Pair(0,n-1,pt[1]);
ans += d;
printf("%lld\n",ans);
}

return 0;
}```