Processing math: 100%
ac-library-hs-1.2.3.0: Data structures and algorithms
Safe HaskellSafe-Inferred
LanguageGHC2021

AtCoder.Extra.SegTree2d

Description

Two-dimensional segment tree for commutative monoids at fixed points.

SegTree2d vs WaveletMatrix2d

They basically the same functionalities and performance, however, in ac-library-hs, SegTree2d has better API and even outperforms WaveletMatrix2d.

Examples

Expand

Create a two-dimensional segment tree for points (0,0) with weight 10 and (1,1) with weight 20:

>>> import AtCoder.Extra.SegTree2d qualified as Seg
>>> import Data.Semigroup (Sum (..))
>>> import Data.Vector.Unboxed qualified as VU
>>> seg <- Seg.build3 @_ @(Sum Int) $ VU.fromList [(0, 0, 10), (1, 1, 20)]

Get monoid product in [x1,x2)×[y1,y2) with prod:

>>> Seg.prod seg {- x -} 0 2 {- y -} 0 2
Sum {getSum = 30}

Monoid values can be altered:

>>> Seg.write seg 1 50
>>> Seg.prod seg {- x -} 1 2 {- y -} 1 2
Sum {getSum = 50}
>>> Seg.allProd seg
Sum {getSum = 60}
>>> Seg.count seg {- x -} 0 2 {- y -} 0 2
2

Since: 1.2.3.0

Synopsis

SegTree2d

data SegTree2d s a Source #

Two-dimensional segment tree.

Since: 1.2.3.0

Constructors

SegTree2d 

Fields

Constructors

new Source #

Arguments

:: (PrimMonad m, Monoid a, Unbox a) 
=> Vector (Int, Int)

(x,y) vector

-> m (SegTree2d (PrimState m) a)

Two-dimensional segment tree

O(nlogn) Creates a SegTree2d from a vector of (x,y) coordinates.

Since: 1.2.3.0

build Source #

Arguments

:: (HasCallStack, PrimMonad m, Monoid a, Unbox a) 
=> Vector Int

x vector

-> Vector Int

y vector

-> Vector a

w vector

-> m (SegTree2d (PrimState m) a)

Two-dimensional segment tree

O(nlogn) Creates a SegTree2d from vectors of x, y and w.

Since: 1.2.3.0

build2 Source #

Arguments

:: (HasCallStack, PrimMonad m, Monoid a, Unbox a) 
=> Vector (Int, Int)

(x,y) vector

-> Vector a

w vector

-> m (SegTree2d (PrimState m) a)

Two-dimensional segment tree

O(nlogn) Creates a SegTree2d from vectors of (x,y) and w.

Since: 1.2.3.0

build3 :: (HasCallStack, PrimMonad m, Monoid a, Unbox a) => Vector (Int, Int, a) -> m (SegTree2d (PrimState m) a) Source #

O(nlogn) Creates a SegTree2d from a vector of (x,y,w).

Since: 1.2.3.0

Write

write Source #

Arguments

:: (HasCallStack, PrimMonad m, Monoid a, Unbox a) 
=> SegTree2d (PrimState m) a

Two-dimensional segment tree

-> Int

Original point index

-> a

New monoid value

-> m () 

O(logn) Writes to the k-th original point's monoid value.

Since: 1.2.3.0

modify Source #

Arguments

:: (HasCallStack, PrimMonad m, Monoid a, Unbox a) 
=> SegTree2d (PrimState m) a

Two-dimensional segment tree

-> (a -> a)

Function that alters the monoid value

-> Int

Original point index

-> m () 

O(logn) Given f, modofies the k-th original point's monoid value.

Since: 1.2.3.0

modifyM Source #

Arguments

:: (HasCallStack, PrimMonad m, Monoid a, Unbox a) 
=> SegTree2d (PrimState m) a

Two-dimensional segment tree

-> (a -> m a)

Function that alters the monoid value

-> Int

Original point index

-> m () 

O(logn) Given f, modofies the k-th original point's monoid value.

Since: 1.2.3.0

Monoid product

prod Source #

Arguments

:: (HasCallStack, PrimMonad m, Monoid a, Unbox a) 
=> SegTree2d (PrimState m) a

Two-dimensional segment tree

-> Int

x1

-> Int

x2

-> Int

y1

-> Int

y2

-> m a

Πp[x1,x2)×[y1,y2)ap

O(log2n) Returns monoid product Πp[x1,x2)×[y1,y2)ap.

Since: 1.2.3.0

allProd :: (HasCallStack, PrimMonad m, Monoid a, Unbox a) => SegTree2d (PrimState m) a -> m a Source #

O(1) Returns monoid product Πp[,)×[,)ap.

Since: 1.2.3.0

Count

count Source #

Arguments

:: (HasCallStack, PrimMonad m, Monoid a, Unbox a) 
=> SegTree2d (PrimState m) a

Two-dimensional segment tree

-> Int

x1

-> Int

x2

-> Int

y1

-> Int

y2

-> m Int

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

O(logn) Returns the number of points in [x1,x2)×[y1,y2).

Since: 1.2.3.0