ac-library-hs-1.2.2.0: Data structures and algorithms
Safe HaskellSafe-Inferred
LanguageGHC2021

AtCoder.Extra.KdTree

Description

Static, \(k\)-dimensional tree \((k = 2)\).

  • Points are fixed on build.
  • Multiple points can exist at the same coordinate.

Examples

Expand
>>> import AtCoder.Extra.KdTree qualified as KT
>>> import Data.Vector.Unboxed qualified as VU
>>> let xys = VU.fromList [(0, 0), (1, 1), (4, 2)]
>>> let kt = KT.build2 xys
>>> -- Find point indices in [0, 2) x [0, 2) with maximum capacity 3
>>> KT.findPointsIn kt 0 2 0 2 3
[0,1]
>>> KT.findNearestPoint kt 3 3
Just 2

Since: 1.2.2.0

Synopsis

K-dimensional tree

data KdTree Source #

Static, \(k\)-dimensional tree \((k = 2)\).

Since: 1.2.2.0

Constructors

KdTree 

Fields

  • nKt :: !Int

    The number of points in the \(k\)-d tree.

    Since: 1.2.2.0

  • incRectsKt :: !(Vector (Int, Int, Int, Int))

    Rectangle information: inclusive (closed) ranges \([x_1, x_2) \times [y_1, y_2)\).

    Since: 1.2.2.0

  • dataKt :: !(Vector Int)

    Maps rectangle index to original point index.

    Since: 1.2.2.0

Constructors

build Source #

Arguments

:: HasCallStack 
=> Vector Int

\(x\) coordnates

-> Vector Int

\(y\) coordnates

-> KdTree

KdTree

\(O(n \log n)\) Creates a KdTree from \(x\) and \(y\) vectors.

Constraints

  • \(|\mathrm{xs}| = |\mathrm{ys}|\).

Since: 1.2.2.0

build2 Source #

Arguments

:: HasCallStack 
=> Vector (Int, Int)

\(x, y\) coordnates

-> KdTree

KdTree

\(O(n \log n)\) Creates KdTree from a \((x, y)\) vector.

Constraints

  • \(|\mathrm{xs}| = |\mathrm{ys}|\).

Since: 1.2.2.0

findPointsIn Source #

Arguments

:: HasCallStack 
=> KdTree

KdTree

-> Int

\(x_l\)

-> Int

\(x_r\)

-> Int

\(y_l\)

-> Int

\(y_r\)

-> Int

Maximum number of points in \([x_l, x_r) \times [y_l, y_r)\).

-> Vector Int

Point indices in \([x_l, x_r) \times [y_l, y_r)\).

\(O(n \log n)\) Collects points in \([x_l, x_r) \times [y_l, y_r)\).

Since: 1.2.2.0

findNearestPoint Source #

Arguments

:: HasCallStack 
=> KdTree

KdTree

-> Int

\(x\)

-> Int

\(y\)

-> Maybe Int

The nearest point index

\(O(\log n)\), only if the points are randomly distributed. Returns the index of the nearest point, or Nothing if the KdTree has no point.

Since: 1.2.2.0