Counting Lattice Rectangles in $O(n\log n)$ Operations
Abstract
Let be the number of rectangles, not necessarily axis-parallel, whose vertices belong to the square grid of lattice points. We give an exact algorithm that computes one prescribed value in arithmetic operations and arithmetic words of working memory. The algorithm decomposes the count into M\"obius divisor layers, partitions weighted floor-moment queries by a truncated Euclidean coefficient-cone recursion, and reuses uniform marker grids along common coefficient paths. Each marker requires only its uniform cell and constant-size corrections at nearby boundaries, which select an exact precompiled cell operator. All integer operands have bits. An exact 128-bit C++ implementation for the reported input range is compared experimentally with the previous algorithm.
Cite
@article{arxiv.2607.17961,
title = {Counting Lattice Rectangles in $O(n\log n)$ Operations},
author = {Dmitry Babichev and Tatiana Shpakova},
journal= {arXiv preprint arXiv:2607.17961},
year = {2026}
}