Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
AtCoder.Extra.DynSparseSegTree.Persistent
Description
A dynamic, sparse, persitent segment tree that covers a half-open interval [l0,r0). Nodes are
instantinated as needed, with the required capacity being 2qlog2L, where q is the number of
mutable operations. The traid-off compared to the non-sparse variant is that initial monoid
values are fixed at mempty
.
Example
>>>
import AtCoder.Extra.DynSparseSegTree.Persistent qualified as Seg
>>>
import Data.Semigroup (Sum (..))
>>>
import Data.Vector.Unboxed qualified as VU
Create a DynSegTree
over [0,4) with some initial capacity:
>>>
let len = 4; q = 2
>>>
seg <- Seg.new @_ @(Sum Int) (Seg.recommendedCapacity len q) 0 4
Different from the SegTree
module, it requires explicit root handle:
>>>
-- [0, 0, 0, 0]
>>>
root <- Seg.newRoot seg
>>>
root1 <- Seg.write seg root 1 $ Sum 10
>>>
root2 <- Seg.write seg root1 2 $ Sum 20
>>>
-- [0, 10, 20, 0]
>>>
Seg.prod seg root2 0 3
Sum {getSum = 30}
>>>
Seg.maxRight seg root2 (< (Sum 30))
2
Since: 1.2.1.0
Synopsis
- data DynSparseSegTree s a = DynSparseSegTree {}
- newtype Index = Index {}
- new :: (HasCallStack, PrimMonad m, Monoid a, Unbox a) => Int -> Int -> Int -> m (DynSparseSegTree (PrimState m) a)
- recommendedCapacity :: Int -> Int -> Int
- newRoot :: (HasCallStack, PrimMonad m, Monoid a, Unbox a) => DynSparseSegTree (PrimState m) a -> m Index
- write :: (HasCallStack, PrimMonad m, Monoid a, Unbox a) => DynSparseSegTree (PrimState m) a -> Index -> Int -> a -> m Index
- modify :: (HasCallStack, PrimMonad m, Monoid a, Unbox a) => DynSparseSegTree (PrimState m) a -> Index -> (a -> a) -> Int -> m Index
- modifyM :: (HasCallStack, PrimMonad m, Monoid a, Unbox a) => DynSparseSegTree (PrimState m) a -> Index -> (a -> m a) -> Int -> m Index
- prod :: (HasCallStack, PrimMonad m, Monoid a, Unbox a) => DynSparseSegTree (PrimState m) a -> Index -> Int -> Int -> m a
- allProd :: (HasCallStack, PrimMonad m, Monoid a, Unbox a) => DynSparseSegTree (PrimState m) a -> Index -> m a
- maxRight :: (HasCallStack, PrimMonad m, Monoid a, Unbox a) => DynSparseSegTree (PrimState m) a -> Index -> (a -> Bool) -> m Int
- maxRightM :: (HasCallStack, PrimMonad m, Monoid a, Unbox a) => DynSparseSegTree (PrimState m) a -> Index -> (a -> m Bool) -> m Int
- clear :: PrimMonad m => DynSparseSegTree (PrimState m) a -> m ()
Dynamic, sparse segment tree
data DynSparseSegTree s a Source #
A dynamic, sparse segment tree that covers a half-open interval [l0,r0). Is is dynamic in that the nodes are instantinated as needed.
Since: 1.2.1.0
Constructors
DynSparseSegTree | |
Fields
|
Re-exports
Strongly typed index of pool items. User has to explicitly corece
on raw index use.
Instances
Constructors
Arguments
:: (HasCallStack, PrimMonad m, Monoid a, Unbox a) | |
=> Int | Capacity n |
-> Int | Left index boundary l0 |
-> Int | Right index boundary r0 |
-> m (DynSparseSegTree (PrimState m) a) | Dynamic, sparse, persistent segment tree |
O(n) Creates a DynSparseSegTree
of capacity n for interval [l0,r0) with
mempty
as initial leaf values.
Since: 1.2.1.0
recommendedCapacity :: Int -> Int -> Int Source #
O(1) Returns recommended capacity for L and q: 2qlog2L.
Since: 1.2.1.0
newRoot :: (HasCallStack, PrimMonad m, Monoid a, Unbox a) => DynSparseSegTree (PrimState m) a -> m Index Source #
O(1) Creates a new root in [l0,r0).
Since: 1.2.1.0
Accessing elements
write :: (HasCallStack, PrimMonad m, Monoid a, Unbox a) => DynSparseSegTree (PrimState m) a -> Index -> Int -> a -> m Index Source #
O(logL) Writes to the monoid value of the node at i.
Constraints
- l0≤i<r0
Since: 1.2.1.0
modify :: (HasCallStack, PrimMonad m, Monoid a, Unbox a) => DynSparseSegTree (PrimState m) a -> Index -> (a -> a) -> Int -> m Index Source #
O(logL) Modifies the monoid value of the node at i.
Constraints
- l0≤i<r0
Since: 1.2.1.0
modifyM :: (HasCallStack, PrimMonad m, Monoid a, Unbox a) => DynSparseSegTree (PrimState m) a -> Index -> (a -> m a) -> Int -> m Index Source #
O(logL) Modifies the monoid value of the node at i.
Constraints
- l0≤i<r0
Since: 1.2.1.0
Products
prod :: (HasCallStack, PrimMonad m, Monoid a, Unbox a) => DynSparseSegTree (PrimState m) a -> Index -> Int -> Int -> m a Source #
O(logL) Returns the monoid product in [l,r).
Constraints
- l0≤l≤r≤r0
Since: 1.2.1.0
allProd :: (HasCallStack, PrimMonad m, Monoid a, Unbox a) => DynSparseSegTree (PrimState m) a -> Index -> m a Source #
O(logL) Returns the monoid product in [l0,r0).
Since: 1.2.1.0
Binary searches
maxRight :: (HasCallStack, PrimMonad m, Monoid a, Unbox a) => DynSparseSegTree (PrimState m) a -> Index -> (a -> Bool) -> m Int Source #
O(logL) Returns the maximum r∈[l0,r0) where f(al0al0+1…ar−1) holds.
Since: 1.2.1.0
maxRightM :: (HasCallStack, PrimMonad m, Monoid a, Unbox a) => DynSparseSegTree (PrimState m) a -> Index -> (a -> m Bool) -> m Int Source #
O(logL) Returns the maximum r∈[l0,r0) where f(al0al0+1…ar−1) holds.
Since: 1.2.1.0