B - In case of failure
Time Limit:30000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
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#include <iostream> #include <cmath> #include <algorithm> #include <cstdio> using namespace std; const int MAXN = 100000 + 10; const long long INF = 99999999999999999LL; struct Node { long long val[3]; }node[MAXN], temp[MAXN]; int split[MAXN]; int cur, n; bool cmp(Node a, Node b) { return a.val[split[cur]] < b.val[split[cur]]; } void Maketree(int l, int r) { if (l >= r) { return ; } int mid = (l + r) >> 1; cur = mid; long long maxx, maxy, minx, miny; maxx = maxy = -1; minx = miny = INF; for (int i = l; i <= r; i++) { maxx = max(maxx, temp[i].val[0]); maxy = max(maxy, temp[i].val[1]); minx = min(minx, temp[i].val[0]); miny = min(miny, temp[i].val[1]); } split[mid] = (maxx - minx) < (maxy - miny); nth_element(temp + l, temp + mid, temp + r + 1, cmp); Maketree(l, mid - 1); Maketree(mid + 1, r); } long long mul(long long x) { return x * x; } long long dist(Node a, Node b) { long long res = 0; for (int i = 0; i < 2; i++) { res += mul(a.val[i] - b.val[i]); } return res ? res : INF; } long long Query(int l, int r, Node x) { if (l >= r) { if (l == r) { return dist(x, temp[l]); } return INF; } int mid = (l + r) >> 1; int cur = split[mid]; long long res = dist(x, temp[mid]), t; if (x.val[cur] < temp[mid].val[cur]) { t = Query(l, mid - 1, x); if (t > mul(x.val[cur] - temp[mid].val[cur])) { t = min(t, Query(mid + 1, r, x)); } } else { t = Query(mid + 1, r, x); if (t > mul(x.val[cur] - temp[mid].val[cur])) { t = min(t, Query(l, mid - 1, x)); } } return min(res, t); } void input() { int t; scanf("%d", &t); while (t--) { scanf("%d", &n); for (int i = 0; i < n; i++) { for (int j = 0; j < 2; j++) { scanf("%lld", &node[i].val[j]); } temp[i] = node[i]; } Maketree(0, n - 1); for (int i = 0; i < n; i++) { printf("%I64d\n", Query(0, n - 1, node[i])); } } } int main() { input(); return 0; }