Problem 579
Closest Pair of Points
Hard · 30 pts · Geometry, Divide and Conquer, Sorting
Problem
Given N points in the plane, find the smallest squared Euclidean distance (x1 - x2)² + (y1 - y2)² between two different points of the list. Points may repeat, in which case the answer is 0. Checking every pair is O(N²). Divide and conquer: split the points by x, solve both halves, then only points within the current best distance of the dividing line need checking, and each of them only against a few neighbours in y order. The numbers in this problem can be larger than a 32-bit int can hold (about 2.1 × 10^9), so use a 64-bit integer type: long long in C++, long in Java (Python needs nothing special).
Input
The first line contains N. Each of the next N lines contains two integers x and y.
Output
Print the smallest squared distance between two points.
Constraints
2 <= N <= 50000; -10^7 <= x, y <= 10^7
Example input
4 0 0 5 5 1 2 6 7
Example output
5
Explanation
Among (0,0), (5,5), (1,2) and (6,7), the closest pairs are (0,0)-(1,2) and (5,5)-(6,7), both with squared distance 1² + 2² = 5. So the answer is 5.