Can a minimum-coordinate query help us find the Euclidean closest pair? Put a $$$45^\circ$$$ sector at each point and select the point with the smallest $$$x$$$ in that sector. For an individual query point, this selection can miss its nearest neighbor. For a global closest pair, however, if the selection skips one endpoint, the two candidates are closer to each other than the supposed closest pair. That observation is enough to solve the problem with four Fenwick-tree sweeps.
Given $$$n\ge 2$$$ planar lattice points, we want to find the minimum Euclidean distance $$$d^*$$$ between two distinct input entries. We can compare squared distances throughout. If two input points coincide, the answer is zero, which we can check by sorting. From now on, assume the points are pairwise distinct and let $$$P$$$ be their set.
Consider $$$p=(0,0)$$$, $$$q=(10,1)$$$, and $$$r=(9,8)$$$. Both $$$q$$$ and $$$r$$$ lie in the sector between the positive $$$x$$$-axis and the ray $$$y=x$$$. The minimum-$$$x$$$ point is $$$r$$$, although $$$|pq|^2=101\lt 145=|pr|^2$$$. The shorter edge is $$$qr$$$, with $$$|qr|^2=50$$$. Thus the local choice skips $$$q$$$, and $$$pq$$$ was never a global minimum in the first place.

Figure 1. The upper point $$$r$$$ has smaller $$$x$$$ than $$$q$$$. The thick segment $$$qr$$$ is the global closest pair.
For $$$p=(x_p,y_p)$$$, define its closed sector by
Within $$$S(p)$$$, compare points lexicographically by $$$(x,y)$$$. Write $$$r\prec q$$$ when $$$x_r\lt x_q$$$, or when $$$x_r=x_q$$$ and $$$y_r\lt y_q$$$.

Figure 2. The square marks the query point. The filled dots show that points on either boundary ray qualify.
Lemma 1. If $$$(p,q)$$$ is a global closest pair and $$$q\in S(p)$$$, then $$$q$$$ is the lexicographic minimum of $$$S(p)$$$.
Translate $$$p$$$ to the origin and write $$$q=(t,s)$$$, with $$$0\le s\le t$$$ and $$$t\gt 0$$$. Suppose $$$r=(t',s')\in S(p)$$$ precedes $$$q$$$. Since $$$0\lt t'\le t$$$ and $$$0\le s'\le t'$$$, the point $$$r$$$ lies in the triangle with vertices $$$p$$$, $$$A=(t,0)$$$, and $$$B=(t,t)$$$.

Figure 3. The dashed circle is centered at $$$q$$$ and passes through $$$p$$$. The thick segment joins $$$q$$$ to a point $$$r$$$ that precedes it.
The disk centered at $$$q$$$ with radius $$$|pq|$$$ contains all three vertices. Their squared distances from $$$q$$$ are $$$t^2+s^2$$$, $$$s^2$$$, and $$$(t-s)^2$$$, respectively. Strict convexity puts every point of the triangle other than its vertices strictly inside the disk. The vertex $$$A$$$ is also strictly inside. The vertex $$$p$$$ is excluded from the sector.

Figure 4. On the right edge of the triangle, the lower point comes first in lexicographic order.
The final vertex $$$B$$$ can lie on the circle only when $$$s=0$$$. In that case $$$q=(t,0)$$$ comes before $$$B=(t,t)$$$ by the tie-break on $$$y$$$, so $$$r$$$ cannot be $$$B$$$.

Figure 5. When $$$s=0$$$, the upper vertex and $$$p$$$ are both on the circle. The upper vertex comes after $$$q$$$.
Therefore $$$r$$$ lies strictly inside the disk and $$$|qr|\lt |pq|$$$, contradicting the global minimality of $$$(p,q)$$$. $$$\square$$$
The opening angle $$$45^\circ$$$ is tight for this selection rule if we keep the positive $$$x$$$-axis as one boundary and widen the sector upward. Take $$$p=(0,0)$$$, $$$q=(t,0)$$$, and $$$r=(t-1,t+1)$$$ for an integer $$$t\ge 2$$$. Then
Thus $$$pq$$$ is the unique closest pair. The slope of $$$pr$$$ is $$$(t+1)/(t-1)$$$, which approaches $$$1$$$ from above. For any fixed opening $$$\theta\in(45^\circ,90^\circ)$$$, a large enough $$$t$$$ puts $$$r$$$ inside the widened sector, where its smaller $$$x$$$ makes it win the query at $$$p$$$.

Figure 6. For $$$t=8$$$, the open point $$$r=(7,9)$$$ enters the hatched region when the opening reaches $$$55^\circ$$$. The horizontal segment $$$pq$$$ is still the unique closest pair.
To implement one pass, set $$$k=x-y$$$. Then $$$q\in S(p)$$$ is equivalent to $$$q\ne p$$$ together with
The change of coordinates turns the sector into an upper-right quadrant in the $$$(k,y)$$$ plane.

Figure 7. The left panel uses $$$(x,y)$$$, and the right panel uses $$$(k,y)$$$. The ring marks the candidate with the smallest original $$$x$$$.
Process the points in decreasing order of $$$y$$$, breaking ties by decreasing $$$x$$$. Query before inserting the current point. At $$$p$$$, all points strictly above it have already been inserted. Among points on the same horizontal line, exactly those to its right have been inserted. This is the correct tie order because when $$$y_q=y_p$$$, the condition $$$k_q\ge k_p$$$ reduces to $$$x_q\ge x_p$$$, and distinctness makes the inequality strict.

Figure 8. The ring marks the current query point. Filled points have already been inserted. The sweep visits the current horizontal line from right to left.
Compress the values $$$k_i=x_i-y_i$$$ into increasing ranks $$$\mathrm{rk}_i\in[1,m]$$$ and reverse them by setting $$$\mathrm{pos}_i=m+1-\mathrm{rk}_i$$$. The condition $$$k_q\ge k_p$$$ now becomes a Fenwick prefix query on $$$[1,\mathrm{pos}_p]$$$. Each tree entry stores the lexicographically smallest inserted point in its block. The query combines the block minima, and each insertion updates its blocks by taking a minimum. The coordinate used for comparison can stay fixed because subtracting $$$x_p$$$ from every candidate's $$$x$$$ preserves their order.

Figure 9. A prefix of length seven is queried from right to left using blocks of lengths one, two, and four.
For each nonempty query, evaluate the squared distance to the returned point and update the answer. Run this same pass on the four coordinate systems below.
| Coordinates used in the pass | Directions covered in the original plane |
|---|---|
| $$$(x,y)$$$ | $$$[0,\pi/4]$$$ |
| $$$(y,x)$$$ | $$$[\pi/4,\pi/2]$$$ |
| $$$(y,-x)$$$ | $$$[\pi/2,3\pi/4]$$$ |
| $$$(x,-y)$$$ | $$$[-\pi/4,0]$$$ |

Figure 10. The hatched sectors cover one closed half-circle. The arrows show the two opposite orientations of the same segment.
The union is $$$[-\pi/4,3\pi/4]$$$. For any unordered pair, at least one of the two orientations of its segment lies in this half-circle. Each coordinate transformation preserves distances, so Lemma 1 applies in the pass that covers that orientation. In particular, every global closest pair is returned by at least one query. There are at most $$$4n$$$ candidate edges before deduplication. Sorting, compression, and the Fenwick operations take $$$O(n\log n)$$$ time in total and use $$$O(n)$$$ space.
The standard divide-and-conquer solution splits the points by $$$x$$$ and checks a narrow strip around the dividing line at each merge. The standard plane sweep keeps an active window and uses the current best distance to restrict both that window and the range of $$$y$$$-coordinates it examines. Both take $$$O(n\log n)$$$ time. This Codeforces post gives a short description of the standard sweep.
The four-pass sweep has the same asymptotic running time. Its candidate at each point is fixed by the sector and the coordinate order, independently of the best distance found so far. The interesting part is that a query can miss a point's nearest neighbor inside the sector, yet the candidate set still contains every global closest pair. The distance is computed only after the Fenwick query returns a candidate.
The proof uses the fact that a shorter edge between two candidates would contradict global minimality. This matters in the bichromatic problem. Color $$$p$$$ red and $$$q,r$$$ blue in the opening example. The query at $$$p$$$ picks $$$r$$$, with squared distance $$$145$$$. The true bichromatic minimum is $$$pq$$$, with squared distance $$$101$$$. The shorter edge $$$qr$$$ joins points of the same color.

Figure 11. The square and the filled dots represent the two colors. The thick edge is the true bichromatic minimum. The dotted edge joins points of the same color.
The sector-based candidate rule is related to $$$\Theta$$$-graphs and Yao graphs. A standard $$$\Theta$$$-graph selects a point by its projection onto the cone bisector. This sweep compares projections onto a boundary ray of the canonical sector. Yao graphs select a Euclidean nearest neighbor in each cone. A familiar competitive-programming example of this general approach is the Manhattan MST sweep, which uses $$$x+y$$$ as the selection key in a suitably oriented octant.
References
A. C. Yao, “On constructing minimum spanning trees in $$$k$$$-dimensional spaces and related problems,” SIAM Journal on Computing, 11(4), 721–736, 1982.
K. L. Clarkson, “Approximation algorithms for shortest path motion planning,” STOC, 56–65, 1987.
J. M. Keil, “Approximating the complete Euclidean graph,” SWAT, 208–213, 1988.
P. Bose, A. van Renssen, and S. Verdonschot, “On the spanning ratio of $$$\Theta$$$-graphs,” WADS, 182–194, 2013.



