In case of failure
Time Limit: 60000/30000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1527 Accepted Submission(s): 595
Problem Description
To help their clients deal with faulty Cash Machines, the board of The Planar Bank has decided to stick a label expressing sincere regret and sorrow of the bank about the failure on every ATM. The very same label would gently ask the customer to calmly head to the nearest Machine (that should hopefully
work fine).
In order to do so, a list of two-dimensional locations of all n ATMs has been prepared, and your task is to find for each of them the one closest with respect to the Euclidean distance.
work fine).
In order to do so, a list of two-dimensional locations of all n ATMs has been prepared, and your task is to find for each of them the one closest with respect to the Euclidean distance.
Input
The input contains several test cases. The very first line contains the number of cases t (t <= 15) that follow. Each test cases begin with the number of Cash Machines n (2 <= n <= 10^5). Each of the next n lines contain the coordinates of one Cash Machine x,y (0 <= x,y <=10^9) separated by a space. No two
points in one test case will coincide.
points in one test case will coincide.
Output
For each test case output n lines. i-th of them should contain the squared distance between the i-th ATM from the input and its nearest neighbour.
Sample Input
2 10 17 41 0 34 24 19 8 28 14 12 45 5 27 31 41 11 42 45 36 27 15 0 0 1 2 2 3 3 2 4 0 8 4 7 4 6 3 6 1 8 0 11 0 12 2 13 1 14 2 15 0
Sample Output
200 100 149 100 149 52 97 52 360 97 5 2 2 2 5 1 1 2 4 5 5 2 2 2 5
Source
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <iostream>
#define MAX 100005
#define INF (1LL<<62)
using namespace std;
typedef long long LL;
struct Point
{
int x , y;
}p[MAX], p2[MAX];
bool dv[MAX];
bool cmpx ( const Point& p1 , const Point& p2 )
{
return p1.x < p2.x;
}
bool cmpy ( const Point& p1 , const Point& p2 )
{
return p1.y < p2.y;
}
LL getDis ( Point p1 , Point p2 )
{
return (LL)(p1.x-p2.x)*(p1.x-p2.x) + (LL)(p1.y-p2.y)*(p1.y-p2.y);
}
void build ( int l , int r )
{
if ( l == r ) return;
int mid = l + r >> 1;
int minx = min_element ( p+l,p+r,cmpx )->x;
int miny = min_element ( p+l,p+r,cmpy )->y;
int maxx = max_element ( p+l,p+r,cmpx )->x;
int maxy = max_element ( p+l,p+r,cmpy )->y;
dv[mid] = maxx - minx >= maxy-miny;
nth_element ( p+l , p+mid , p+r , dv[mid]?cmpx:cmpy );
build ( l , mid );
build ( mid+1 , r );
}
LL res;
void find ( int l , int r , Point a )
{
if ( l == r ) return;
int mid = l + r >> 1;
LL dis = getDis ( a , p[mid] );
if ( dis > 0 ) res = min ( res , dis );
LL d = dv[mid]?(a.x - p[mid].x):(a.y - p[mid].y);
int l1 = l , l2 = mid+1 , r1 = mid , r2 = r;
if ( d > 0 ) swap ( l1 , l2 ) , swap ( r1 , r2 );
find ( l1 , r1 , a );
if ( d*d<res ) find ( l2 , r2 , a );
}
int t,n;
int main ( )
{
scanf ( "%d" , &t );
while ( t-- )
{
scanf ( "%d" , &n );
for ( int i = 0 ; i < n ; i++ )
{
scanf ( "%d%d" , &p[i].x , &p[i].y);
p2[i] = p[i];
}
build ( 0 , n );
for ( int i = 0 ; i < n ; i++ )
{
res = INF;
find ( 0 , n , p2[i] );
printf ( "%lld\n" , res );
}
}
}