Processing math: 100%
ac-library-hs-1.2.3.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 [x1,x2)×[y1,y2).

    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(nlogn) Creates a KdTree from x and y vectors.

Constraints

  • |xs|=|ys|.

Since: 1.2.2.0

build2 Source #

Arguments

:: HasCallStack 
=> Vector (Int, Int)

x,y coordnates

-> KdTree

KdTree

O(nlogn) Creates KdTree from a (x,y) vector.

Constraints

  • |xs|=|ys|.

Since: 1.2.2.0

findPointsIn Source #

Arguments

:: HasCallStack 
=> KdTree

KdTree

-> Int

x1

-> Int

x2

-> Int

y1

-> Int

y2

-> Int

Maximum number of points in [x1,x2)×[y1,y2).

-> Vector Int

Point indices in [x1,x2)×[y1,y2).

O(n) Collects points in [x1,x2)×[y1,y2).

Since: 1.2.2.0

findNearestPoint Source #

Arguments

:: HasCallStack 
=> KdTree

KdTree

-> Int

x

-> Int

y

-> Maybe Int

The nearest point index

O(logn), 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