Submission details
Task:Closest points
Sender:aalto26jw_003
Submission time:2026-11-11 16:27:23 +0200
Language:Python3 (PyPy3)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.06 sdetails
#2ACCEPTED0.80 sdetails
#3--details
#4ACCEPTED0.79 sdetails
#5ACCEPTED0.05 sdetails
#6ACCEPTED0.85 sdetails
#7ACCEPTED0.05 sdetails
#8ACCEPTED0.05 sdetails
#9ACCEPTED0.05 sdetails
#10ACCEPTED0.86 sdetails
#11ACCEPTED0.05 sdetails
#12ACCEPTED0.05 sdetails
#13ACCEPTED0.86 sdetails
#14ACCEPTED0.05 sdetails
#15ACCEPTED0.06 sdetails
#16ACCEPTED0.85 sdetails
#17ACCEPTED0.85 sdetails
#18ACCEPTED0.05 sdetails

Code

# FULL TEMPLATE: delete every block you don't use. Blocks don't depend on each other.
# Nodes are 1..n (slot 0 unused). Arrays are 0-indexed.
import sys
from collections import deque, defaultdict, Counter
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
from itertools import accumulate, permutations, combinations, product
from functools import lru_cache, cmp_to_key
from math import gcd, isqrt, comb, inf
input = sys.stdin.readline
MOD = 10**9 + 7
import random

def I():  return int(input())                    # "5"          -> 5
def MI(): return map(int, input().split())       # "8 4"        -> n, q = MI()
def LI(): return list(map(int, input().split())) # "3 2 4 5"    -> [3, 2, 4, 5]
def S():  return input().strip()                 # "BABAZA", ".*.#" (kept as text)
def LS(): return input().split()                 # "? 2 3"      -> ['?', '2', '3']

def dist2(a, b):  # squared distance: stays an integer, compare these instead of sqrt
    dx, dy = a[0] - b[0], a[1] - b[1]
    return dx * dx + dy * dy

def closest_pair(points):  # min squared distance, randomized grid: expected O(n), no list shifting.
    pts = points[:]               # Grid of cells of side s >= current best distance: a closer
    random.shuffle(pts)           # point is in the 3x3 cells around p. Each cell holds O(1) points.
    best = dist2(pts[0], pts[1])  # When best drops, rebuild the grid: point i is in the closest
    grid, s = {}, 0               # pair of the first i with probability 2/i, so the expected
    i = 0                         # total rebuild work is O(n)
    while i < len(pts):
        if best == 0:
            return 0
        if s == 0:                # (re)build with the first i points
            s = isqrt(best - 1) + 1          # ceil(sqrt(best))
            grid = {}
            for q in pts[:i]:
                grid.setdefault((q[0] // s, q[1] // s), []).append(q)
        x, y = pts[i]
        cx, cy = x // s, y // s
        found = best
        for gx in (cx - 1, cx, cx + 1):
            for gy in (cy - 1, cy, cy + 1):
                for qx, qy in grid.get((gx, gy), ()):
                    dd = (x - qx) ** 2 + (y - qy) ** 2
                    if dd < found:
                        found = dd
        if found < best:
            best, s = found, 0    # rebuild before the next point (this one is included then)
        else:
            grid.setdefault((cx, cy), []).append(pts[i])
        i += 1
    return best

def main():
    n = I()
    pts = [tuple(MI()) for _ in range(n)]
    print(str(closest_pair(pts)))


main()

Test details

Test 1

Verdict: ACCEPTED

input
100
58 36
81 -7
46 49
87 -58
...

correct output
1

user output
1

Test 2

Verdict: ACCEPTED

input
200000
-222 -705
277 680
-436 561
528 -516
...

correct output
1

user output
1

Test 3

Verdict:

input
200000
-464738043 865360844
465231470 129093134
-276549869 -21946314
111055008 -48821736
...

correct output
25413170

user output
(empty)

Test 4

Verdict: ACCEPTED

input
200000
1 513001000
2 689002000
3 785003000
4 799004000
...

correct output
1000000

user output
1000000

Test 5

Verdict: ACCEPTED

input
4
0 0
0 3
3 0
1 1

correct output
2

user output
2

Test 6

Verdict: ACCEPTED

input
200000
1 0
1 1
1 2
1 3
...

correct output
1

user output
1

Test 7

Verdict: ACCEPTED

input
4
1 2
10 3
3 5
8 5

correct output
8

user output
8

Test 8

Verdict: ACCEPTED

input
4
10 6
4 10
8 3
2 3

correct output
13

user output
13

Test 9

Verdict: ACCEPTED

input
2
-999999999 -999999999
999999999 999999999

correct output
7999999984000000008

user output
7999999984000000008

Test 10

Verdict: ACCEPTED

input
200000
0 1
1 1
2 1
3 1
...

correct output
1

user output
1

Test 11

Verdict: ACCEPTED

input
8
1 10000
-1 -10000
2 0
-2 0
...

correct output
16

user output
16

Test 12

Verdict: ACCEPTED

input
3
-1000000000 -1000000000
1000000000 1000000000
0 0

correct output
2000000000000000000

user output
2000000000000000000

Test 13

Verdict: ACCEPTED

input
199999
1 1
2 1
3 1
4 1
...

correct output
1

user output
1

Test 14

Verdict: ACCEPTED

input
4
0 0
5 8
6 1
10000 0

correct output
37

user output
37

Test 15

Verdict: ACCEPTED

input
435
-842 -199
-480 798
-176 -406
792 608
...

correct output
2

user output
2

Test 16

Verdict: ACCEPTED

input
200000
1 0
1 2
1 4
1 6
...

correct output
4

user output
4

Test 17

Verdict: ACCEPTED

input
200000
0 1
2 1
4 1
6 1
...

correct output
4

user output
4

Test 18

Verdict: ACCEPTED

input
3
-1000000000 -1000000000
1000000000 1000000000
1000000000 -1000000000

correct output
4000000000000000000

user output
4000000000000000000