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

AtCoder.Extra.Monoid

Description

Extra module of pre-defined SegAct instances and helpful monoids.

Since: 1.0.0.0

Synopsis

Re-exports

It's mainly a list. It is recommended to use specific submodules.

SegAct

class Monoid f => SegAct f a where Source #

Typeclass reprentation of the LazySegTree properties. User can implement either segAct or segActWithLength.

Instances should satisfy the follwing properties:

Left monoid action
segAct (f2 <> f1) x = segAct f2 (segAct f1 x)
Identity map
segAct mempty x = x
Endomorphism
segAct f (x1 <> x2) = (segAct f x1) <> (segAct f x2)

If you implement SegAct via segActWithLength, satisfy one more propety:

Linear left monoid action
segActWithLength len f a = stimes len (segAct f a) a.

Invariant

In SegAct instances, new semigroup values are always given from the left: new <> old. The order is important for non-commutative monoid implementations.

Example instance

Expand

Take Affine1 as an example of type \(F\).

{-# LANGUAGE TypeFamilies #-}

import AtCoder.LazySegTree qualified as LST
import AtCoder.LazySegTree (SegAct (..))
import Data.Monoid
import Data.Vector.Generic qualified as VG
import Data.Vector.Generic.Mutable qualified as VGM
import Data.Vector.Unboxed qualified as VU
import Data.Vector.Unboxed.Mutable qualified as VUM

-- | f x = a * x + b. It's implemented as a newtype of `(a, a)` for easy Unbox deriving.
newtype Affine1 a = Affine1 (Affine1 a)
  deriving newtype (Eq, Ord, Show)

-- | This type alias makes the Unbox deriving easier, described velow.
type Affine1Repr a = (a, a)

instance (Num a) => Semigroup (Affine1 a) where
  {-# INLINE (<>) #-}
  (Affine1 (!a1, !b1)) <> (Affine1 (!a2, !b2)) = Affine1 (a1 * a2, a1 * b2 + b1)

instance (Num a) => Monoid (Affine1 a) where
  {-# INLINE mempty #-}
  mempty = Affine1 (1, 0)

instance (Num a) => SegAct (Affine1 a) (Sum a) where
  {-# INLINE segActWithLength #-}
  segActWithLength len (Affine1 (!a, !b)) !x = a * x + b * fromIntegral len

Deriving Unbox is very easy for such a newtype (though the efficiency is not the maximum):

newtype instance VU.MVector s (Affine1 a) = MV_Affine1 (VU.MVector s (Affine1 a))
newtype instance VU.Vector (Affine1 a) = V_Affine1 (VU.Vector (Affine1 a))
deriving instance (VU.Unbox a) => VGM.MVector VUM.MVector (Affine1 a)
deriving instance (VU.Unbox a) => VG.Vector VU.Vector (Affine1 a)
instance (VU.Unbox a) => VU.Unbox (Affine1 a)

Example contest template

Expand

Define your monoid action F and your acted monoid X:

{-# LANGUAGE TypeFamilies #-}

import AtCoder.LazySegTree qualified as LST
import AtCoder.LazySegTree (SegAct (..))
import Data.Vector.Generic qualified as VG
import Data.Vector.Generic.Mutable qualified as VGM
import Data.Vector.Unboxed qualified as VU
import Data.Vector.Unboxed.Mutable qualified as VUM

{- ORMOLU_DISABLE -}
-- | F is a custom monoid action, defined as a newtype of FRepr.
newtype F = F FRepr deriving newtype (Eq, Ord, Show) ; unF :: F -> FRepr ; unF (F x) = x ; newtype instance VU.MVector s F = MV_F (VU.MVector s FRepr) ; newtype instance VU.Vector F = V_F (VU.Vector FRepr) ; deriving instance VGM.MVector VUM.MVector F ; deriving instance VG.Vector VU.Vector F ; instance VU.Unbox F ;
{- ORMOLU_ENABLE -}

-- | Affine: f x = a * x + b
type FRepr = (Int, Int)

instance Semigroup F where
  -- new <> old
  {-# INLINE (<>) #-}
  (F (!a1, !b1)) <> (F (!a2, !b2)) = F (a1 * a2, a1 * b2 + b1)

instance Monoid F where
  {-# INLINE mempty #-}
  mempty = F (1, 0)

{- ORMOLU_DISABLE -}
-- | X is a custom acted monoid, defined as a newtype of XRepr.
newtype X = X XRepr deriving newtype (Eq, Ord, Show) ; unX :: X -> XRepr ; unX (X x) = x; newtype instance VU.MVector s X = MV_X (VU.MVector s XRepr) ; newtype instance VU.Vector X = V_X (VU.Vector XRepr) ; deriving instance VGM.MVector VUM.MVector X ; deriving instance VG.Vector VU.Vector X ; instance VU.Unbox X ;
{- ORMOLU_ENABLE -}

-- | Acted Int (same as `Sum Int`).
type XRepr = Int

deriving instance Num X; -- in our case X is a Num.

instance Semigroup X where
  {-# INLINE (<>) #-}
  (X x1) <> (X x2) = X $! x1 + x2

instance Monoid X where
  {-# INLINE mempty #-}
  mempty = X 0

instance SegAct F X where
  -- {-# INLINE segAct #-}
  -- segAct len (F (!a, !b)) (X x) = X $! a * x + b
  {-# INLINE segActWithLength #-}
  segActWithLength len (F (!a, !b)) (X x) = X $! a * x + len * b

It's tested as below:

expect :: (Eq a, Show a) => String -> a -> a -> ()
expect msg a b
  | a == b = ()
  | otherwise = error $ msg ++ ": expected " ++ show a ++ ", found " ++ show b

main :: IO ()
main = do
  seg <- LST.build _ F @X $ VU.map X $ VU.fromList [1, 2, 3, 4]
  LST.applyIn seg 1 3 $ F (2, 1) -- [1, 5, 7, 4]
  LST.write seg 3 $ X 10 -- [1, 5, 7, 10]
  LST.modify seg (+ (X 1)) 0   -- [2, 5, 7, 10]
  !_ <- (expect "test 1" (X 5)) <$> LST.read seg 1
  !_ <- (expect "test 2" (X 14)) <$> LST.prod seg 0 3 -- reads an interval [0, 3)
  !_ <- (expect "test 3" (X 24)) <$> LST.allProd seg
  !_ <- (expect "test 4" 2) <$> LST.maxRight seg 0 (<= (X 10)) -- sum [0, 2) = 7 <= 10
  !_ <- (expect "test 5" 3) <$> LST.minLeft seg 4 (<= (X 10)) -- sum [3, 4) = 10 <= 10
  putStrLn "=> test passed!"

Since: 1.0.0.0

Minimal complete definition

Nothing

Methods

segAct :: f -> a -> a Source #

Lazy segment tree action \(f(x)\).

Since: 1.0.0.0

segActWithLength :: Int -> f -> a -> a Source #

Lazy segment tree action \(f(x)\) with the target monoid's length.

If you implement SegAct with this function, you don't have to store the monoid's length, since it's given externally.

Since: 1.0.0.0

Instances

Instances details
Monoid a => SegAct (RangeSet a) a Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeSet

Methods

segAct :: RangeSet a -> a -> a Source #

segActWithLength :: Int -> RangeSet a -> a -> a Source #

Num a => SegAct (Affine1 (Sum a)) (Sum a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Affine1

Methods

segAct :: Affine1 (Sum a) -> Sum a -> Sum a Source #

segActWithLength :: Int -> Affine1 (Sum a) -> Sum a -> Sum a Source #

Num a => SegAct (Affine1 a) (Sum a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Affine1

Methods

segAct :: Affine1 a -> Sum a -> Sum a Source #

segActWithLength :: Int -> Affine1 a -> Sum a -> Sum a Source #

Num a => SegAct (Mat2x2 a) (V2 a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Mat2x2

Methods

segAct :: Mat2x2 a -> V2 a -> V2 a Source #

segActWithLength :: Int -> Mat2x2 a -> V2 a -> V2 a Source #

Monoid (Max a) => SegAct (RangeAdd (Max a)) (Max a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeAdd

Methods

segAct :: RangeAdd (Max a) -> Max a -> Max a Source #

segActWithLength :: Int -> RangeAdd (Max a) -> Max a -> Max a Source #

Monoid (Min a) => SegAct (RangeAdd (Min a)) (Min a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeAdd

Methods

segAct :: RangeAdd (Min a) -> Min a -> Min a Source #

segActWithLength :: Int -> RangeAdd (Min a) -> Min a -> Min a Source #

Monoid (Sum a) => SegAct (RangeAdd (Sum a)) (Sum a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeAdd

Methods

segAct :: RangeAdd (Sum a) -> Sum a -> Sum a Source #

segActWithLength :: Int -> RangeAdd (Sum a) -> Sum a -> Sum a Source #

Num a => SegAct (Dual (Affine1 (Sum a))) (Sum a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Affine1

Methods

segAct :: Dual (Affine1 (Sum a)) -> Sum a -> Sum a Source #

segActWithLength :: Int -> Dual (Affine1 (Sum a)) -> Sum a -> Sum a Source #

Num a => SegAct (Dual (Affine1 a)) (Sum a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Affine1

Methods

segAct :: Dual (Affine1 a) -> Sum a -> Sum a Source #

segActWithLength :: Int -> Dual (Affine1 a) -> Sum a -> Sum a Source #

Num a => SegAct (Dual (Mat2x2 a)) (V2 a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Mat2x2

Methods

segAct :: Dual (Mat2x2 a) -> V2 a -> V2 a Source #

segActWithLength :: Int -> Dual (Mat2x2 a) -> V2 a -> V2 a Source #

Affine1

newtype Affine1 a Source #

Monoid action \(f: x \rightarrow ax + b\).

  • Use Mat2x2 if inverse operations are required, or if it's necessary to store the monoid length in the acted monoid (V2).

Composition and dual

The affine transformation acts as a left monoid action: \(f_2 (f_1 v) = (f_2 \circ f_1) v\). To apply the leftmost transformation first in a segment tree, wrap Affine1 in Data.Monoid.Dual.

Example

Expand
>>> import AtCoder.Extra.Monoid (SegAct(..), Affine1(..))
>>> import AtCoder.LazySegTree qualified as LST
>>> seg <- LST.build @_ @(Affine1 Int) @(Sum Int) $ VU.generate 3 Sum -- [0, 1, 2]
>>> LST.applyIn seg 0 3 $ Affine1 (2, 1) -- [1, 3, 5]
>>> getSum <$> LST.allProd seg
9

Since: 1.0.0.0

Constructors

Affine1 (Affine1Repr a) 

Instances

Instances details
Unbox a => Vector Vector (Affine1 a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Affine1

Methods

basicUnsafeFreeze :: Mutable Vector s (Affine1 a) -> ST s (Vector (Affine1 a))

basicUnsafeThaw :: Vector (Affine1 a) -> ST s (Mutable Vector s (Affine1 a))

basicLength :: Vector (Affine1 a) -> Int

basicUnsafeSlice :: Int -> Int -> Vector (Affine1 a) -> Vector (Affine1 a)

basicUnsafeIndexM :: Vector (Affine1 a) -> Int -> Box (Affine1 a)

basicUnsafeCopy :: Mutable Vector s (Affine1 a) -> Vector (Affine1 a) -> ST s ()

elemseq :: Vector (Affine1 a) -> Affine1 a -> b -> b

Unbox a => MVector MVector (Affine1 a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Affine1

Num a => Monoid (Affine1 a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Affine1

Methods

mempty :: Affine1 a #

mappend :: Affine1 a -> Affine1 a -> Affine1 a #

mconcat :: [Affine1 a] -> Affine1 a #

Num a => Semigroup (Affine1 a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Affine1

Methods

(<>) :: Affine1 a -> Affine1 a -> Affine1 a #

sconcat :: NonEmpty (Affine1 a) -> Affine1 a #

stimes :: Integral b => b -> Affine1 a -> Affine1 a #

Show a => Show (Affine1 a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Affine1

Methods

showsPrec :: Int -> Affine1 a -> ShowS #

show :: Affine1 a -> String #

showList :: [Affine1 a] -> ShowS #

Eq a => Eq (Affine1 a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Affine1

Methods

(==) :: Affine1 a -> Affine1 a -> Bool #

(/=) :: Affine1 a -> Affine1 a -> Bool #

Ord a => Ord (Affine1 a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Affine1

Methods

compare :: Affine1 a -> Affine1 a -> Ordering #

(<) :: Affine1 a -> Affine1 a -> Bool #

(<=) :: Affine1 a -> Affine1 a -> Bool #

(>) :: Affine1 a -> Affine1 a -> Bool #

(>=) :: Affine1 a -> Affine1 a -> Bool #

max :: Affine1 a -> Affine1 a -> Affine1 a #

min :: Affine1 a -> Affine1 a -> Affine1 a #

Unbox a => Unbox (Affine1 a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Affine1

Num a => SegAct (Affine1 (Sum a)) (Sum a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Affine1

Methods

segAct :: Affine1 (Sum a) -> Sum a -> Sum a Source #

segActWithLength :: Int -> Affine1 (Sum a) -> Sum a -> Sum a Source #

Num a => SegAct (Affine1 a) (Sum a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Affine1

Methods

segAct :: Affine1 a -> Sum a -> Sum a Source #

segActWithLength :: Int -> Affine1 a -> Sum a -> Sum a Source #

Num a => SegAct (Dual (Affine1 (Sum a))) (Sum a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Affine1

Methods

segAct :: Dual (Affine1 (Sum a)) -> Sum a -> Sum a Source #

segActWithLength :: Int -> Dual (Affine1 (Sum a)) -> Sum a -> Sum a Source #

Num a => SegAct (Dual (Affine1 a)) (Sum a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Affine1

Methods

segAct :: Dual (Affine1 a) -> Sum a -> Sum a Source #

segActWithLength :: Int -> Dual (Affine1 a) -> Sum a -> Sum a Source #

newtype MVector s (Affine1 a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Affine1

newtype MVector s (Affine1 a) = MV_Affine1 (MVector s (Affine1Repr a))
newtype Vector (Affine1 a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Affine1

type Affine1Repr a = (a, a) Source #

Affine1 internal representation. Tuples are not the fastest representation, but it's easier to implement Unbox.

Since: 1.0.0.0

Mat2x2

newtype Mat2x2 a Source #

Monoid action \(f: x \rightarrow ax + b\). Less efficient than Affine1, but compatible with inverse opereations.

Composition and dual

The affine transformation acts as a left monoid action: \(f_2 (f_1 v) = (f_2 \circ f_1) v\). To apply the leftmost transformation first in a segment tree, wrap Mat2x2 in Data.Monoid.Dual.

Example

Expand
>>> import AtCoder.Extra.Monoid.Mat2x2 qualified as Mat2x2
>>> import AtCoder.Extra.Monoid.V2 qualified as V2
>>> import AtCoder.Extra.Monoid (SegAct(..), Mat2x2(..), V2(..))
>>> import AtCoder.LazySegTree qualified as LST
>>> seg <- LST.build @_ @(Mat2x2 Int) @(V2 Int) $ VU.generate 3 V2.new -- [0, 1, 2]
>>> LST.applyIn seg 0 3 $ Mat2x2.new 2 1 -- [1, 3, 5]
>>> V2.unV2 <$> LST.allProd seg
9

Since: 1.1.0.0

Constructors

Mat2x2 (Mat2x2Repr a) 

Instances

Instances details
Unbox a => Vector Vector (Mat2x2 a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Mat2x2

Methods

basicUnsafeFreeze :: Mutable Vector s (Mat2x2 a) -> ST s (Vector (Mat2x2 a))

basicUnsafeThaw :: Vector (Mat2x2 a) -> ST s (Mutable Vector s (Mat2x2 a))

basicLength :: Vector (Mat2x2 a) -> Int

basicUnsafeSlice :: Int -> Int -> Vector (Mat2x2 a) -> Vector (Mat2x2 a)

basicUnsafeIndexM :: Vector (Mat2x2 a) -> Int -> Box (Mat2x2 a)

basicUnsafeCopy :: Mutable Vector s (Mat2x2 a) -> Vector (Mat2x2 a) -> ST s ()

elemseq :: Vector (Mat2x2 a) -> Mat2x2 a -> b -> b

Unbox a => MVector MVector (Mat2x2 a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Mat2x2

Methods

basicLength :: MVector s (Mat2x2 a) -> Int

basicUnsafeSlice :: Int -> Int -> MVector s (Mat2x2 a) -> MVector s (Mat2x2 a)

basicOverlaps :: MVector s (Mat2x2 a) -> MVector s (Mat2x2 a) -> Bool

basicUnsafeNew :: Int -> ST s (MVector s (Mat2x2 a))

basicInitialize :: MVector s (Mat2x2 a) -> ST s ()

basicUnsafeReplicate :: Int -> Mat2x2 a -> ST s (MVector s (Mat2x2 a))

basicUnsafeRead :: MVector s (Mat2x2 a) -> Int -> ST s (Mat2x2 a)

basicUnsafeWrite :: MVector s (Mat2x2 a) -> Int -> Mat2x2 a -> ST s ()

basicClear :: MVector s (Mat2x2 a) -> ST s ()

basicSet :: MVector s (Mat2x2 a) -> Mat2x2 a -> ST s ()

basicUnsafeCopy :: MVector s (Mat2x2 a) -> MVector s (Mat2x2 a) -> ST s ()

basicUnsafeMove :: MVector s (Mat2x2 a) -> MVector s (Mat2x2 a) -> ST s ()

basicUnsafeGrow :: MVector s (Mat2x2 a) -> Int -> ST s (MVector s (Mat2x2 a))

Num a => Monoid (Mat2x2 a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Mat2x2

Methods

mempty :: Mat2x2 a #

mappend :: Mat2x2 a -> Mat2x2 a -> Mat2x2 a #

mconcat :: [Mat2x2 a] -> Mat2x2 a #

Num a => Semigroup (Mat2x2 a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Mat2x2

Methods

(<>) :: Mat2x2 a -> Mat2x2 a -> Mat2x2 a #

sconcat :: NonEmpty (Mat2x2 a) -> Mat2x2 a #

stimes :: Integral b => b -> Mat2x2 a -> Mat2x2 a #

Show a => Show (Mat2x2 a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Mat2x2

Methods

showsPrec :: Int -> Mat2x2 a -> ShowS #

show :: Mat2x2 a -> String #

showList :: [Mat2x2 a] -> ShowS #

Eq a => Eq (Mat2x2 a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Mat2x2

Methods

(==) :: Mat2x2 a -> Mat2x2 a -> Bool #

(/=) :: Mat2x2 a -> Mat2x2 a -> Bool #

Ord a => Ord (Mat2x2 a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Mat2x2

Methods

compare :: Mat2x2 a -> Mat2x2 a -> Ordering #

(<) :: Mat2x2 a -> Mat2x2 a -> Bool #

(<=) :: Mat2x2 a -> Mat2x2 a -> Bool #

(>) :: Mat2x2 a -> Mat2x2 a -> Bool #

(>=) :: Mat2x2 a -> Mat2x2 a -> Bool #

max :: Mat2x2 a -> Mat2x2 a -> Mat2x2 a #

min :: Mat2x2 a -> Mat2x2 a -> Mat2x2 a #

Unbox a => Unbox (Mat2x2 a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Mat2x2

Num a => SegAct (Mat2x2 a) (V2 a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Mat2x2

Methods

segAct :: Mat2x2 a -> V2 a -> V2 a Source #

segActWithLength :: Int -> Mat2x2 a -> V2 a -> V2 a Source #

Num a => SegAct (Dual (Mat2x2 a)) (V2 a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Mat2x2

Methods

segAct :: Dual (Mat2x2 a) -> V2 a -> V2 a Source #

segActWithLength :: Int -> Dual (Mat2x2 a) -> V2 a -> V2 a Source #

newtype MVector s (Mat2x2 a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Mat2x2

newtype MVector s (Mat2x2 a) = MV_Mat2x2 (MVector s (Mat2x2Repr a))
newtype Vector (Mat2x2 a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Mat2x2

newtype Vector (Mat2x2 a) = V_Mat2x2 (Vector (Mat2x2Repr a))

type Mat2x2Repr a = (a, a, a, a) Source #

Mat2x2 internal representation. Tuples are not the fastest representation, but it's easier to implement Unbox.

Since: 1.1.0.0

newtype V2 a Source #

A monoid acted on by Mat2x2, an affine transformation target.

Since: 1.1.0.0

Constructors

V2 (V2Repr a) 

Instances

Instances details
Unbox a => Vector Vector (V2 a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.V2

Methods

basicUnsafeFreeze :: Mutable Vector s (V2 a) -> ST s (Vector (V2 a))

basicUnsafeThaw :: Vector (V2 a) -> ST s (Mutable Vector s (V2 a))

basicLength :: Vector (V2 a) -> Int

basicUnsafeSlice :: Int -> Int -> Vector (V2 a) -> Vector (V2 a)

basicUnsafeIndexM :: Vector (V2 a) -> Int -> Box (V2 a)

basicUnsafeCopy :: Mutable Vector s (V2 a) -> Vector (V2 a) -> ST s ()

elemseq :: Vector (V2 a) -> V2 a -> b -> b

Unbox a => MVector MVector (V2 a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.V2

Methods

basicLength :: MVector s (V2 a) -> Int

basicUnsafeSlice :: Int -> Int -> MVector s (V2 a) -> MVector s (V2 a)

basicOverlaps :: MVector s (V2 a) -> MVector s (V2 a) -> Bool

basicUnsafeNew :: Int -> ST s (MVector s (V2 a))

basicInitialize :: MVector s (V2 a) -> ST s ()

basicUnsafeReplicate :: Int -> V2 a -> ST s (MVector s (V2 a))

basicUnsafeRead :: MVector s (V2 a) -> Int -> ST s (V2 a)

basicUnsafeWrite :: MVector s (V2 a) -> Int -> V2 a -> ST s ()

basicClear :: MVector s (V2 a) -> ST s ()

basicSet :: MVector s (V2 a) -> V2 a -> ST s ()

basicUnsafeCopy :: MVector s (V2 a) -> MVector s (V2 a) -> ST s ()

basicUnsafeMove :: MVector s (V2 a) -> MVector s (V2 a) -> ST s ()

basicUnsafeGrow :: MVector s (V2 a) -> Int -> ST s (MVector s (V2 a))

Num a => Monoid (V2 a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.V2

Methods

mempty :: V2 a #

mappend :: V2 a -> V2 a -> V2 a #

mconcat :: [V2 a] -> V2 a #

Num a => Semigroup (V2 a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.V2

Methods

(<>) :: V2 a -> V2 a -> V2 a #

sconcat :: NonEmpty (V2 a) -> V2 a #

stimes :: Integral b => b -> V2 a -> V2 a #

Show a => Show (V2 a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.V2

Methods

showsPrec :: Int -> V2 a -> ShowS #

show :: V2 a -> String #

showList :: [V2 a] -> ShowS #

Eq a => Eq (V2 a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.V2

Methods

(==) :: V2 a -> V2 a -> Bool #

(/=) :: V2 a -> V2 a -> Bool #

Ord a => Ord (V2 a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.V2

Methods

compare :: V2 a -> V2 a -> Ordering #

(<) :: V2 a -> V2 a -> Bool #

(<=) :: V2 a -> V2 a -> Bool #

(>) :: V2 a -> V2 a -> Bool #

(>=) :: V2 a -> V2 a -> Bool #

max :: V2 a -> V2 a -> V2 a #

min :: V2 a -> V2 a -> V2 a #

Unbox a => Unbox (V2 a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.V2

Num a => SegAct (Mat2x2 a) (V2 a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Mat2x2

Methods

segAct :: Mat2x2 a -> V2 a -> V2 a Source #

segActWithLength :: Int -> Mat2x2 a -> V2 a -> V2 a Source #

Num a => SegAct (Dual (Mat2x2 a)) (V2 a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.Mat2x2

Methods

segAct :: Dual (Mat2x2 a) -> V2 a -> V2 a Source #

segActWithLength :: Int -> Dual (Mat2x2 a) -> V2 a -> V2 a Source #

newtype MVector s (V2 a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.V2

newtype MVector s (V2 a) = MV_V2 (MVector s (V2Repr a))
newtype Vector (V2 a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.V2

newtype Vector (V2 a) = V_V2 (Vector (V2Repr a))

type V2Repr a = (a, a) Source #

V2 internal representation. Tuples are not the fastest representation, but it's easier to implement Unbox.

Since: 1.1.0.0

Range add

newtype RangeAdd a Source #

Monoid action \(f: x \rightarrow x + d\).

Example

Expand
>>> import AtCoder.Extra.Monoid (SegAct(..), RangeAdd(..))
>>> import AtCoder.LazySegTree qualified as LST
>>> import Data.Semigroup (Max(..))
>>> seg <- LST.build @_ @(RangeAdd (Sum Int)) @(Sum Int) $ VU.generate 3 Sum -- [0, 1, 2]
>>> LST.applyIn seg 0 3 $ RangeAdd (Sum 5) -- [5, 6, 7]
>>> getSum <$> LST.prod seg 0 3
18

Since: 1.0.0.0

Constructors

RangeAdd a 

Instances

Instances details
Unbox a => Vector Vector (RangeAdd a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeAdd

Methods

basicUnsafeFreeze :: Mutable Vector s (RangeAdd a) -> ST s (Vector (RangeAdd a))

basicUnsafeThaw :: Vector (RangeAdd a) -> ST s (Mutable Vector s (RangeAdd a))

basicLength :: Vector (RangeAdd a) -> Int

basicUnsafeSlice :: Int -> Int -> Vector (RangeAdd a) -> Vector (RangeAdd a)

basicUnsafeIndexM :: Vector (RangeAdd a) -> Int -> Box (RangeAdd a)

basicUnsafeCopy :: Mutable Vector s (RangeAdd a) -> Vector (RangeAdd a) -> ST s ()

elemseq :: Vector (RangeAdd a) -> RangeAdd a -> b -> b

Unbox a => MVector MVector (RangeAdd a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeAdd

Monoid a => Monoid (RangeAdd a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeAdd

Methods

mempty :: RangeAdd a #

mappend :: RangeAdd a -> RangeAdd a -> RangeAdd a #

mconcat :: [RangeAdd a] -> RangeAdd a #

Semigroup a => Semigroup (RangeAdd a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeAdd

Methods

(<>) :: RangeAdd a -> RangeAdd a -> RangeAdd a #

sconcat :: NonEmpty (RangeAdd a) -> RangeAdd a #

stimes :: Integral b => b -> RangeAdd a -> RangeAdd a #

Show a => Show (RangeAdd a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeAdd

Methods

showsPrec :: Int -> RangeAdd a -> ShowS #

show :: RangeAdd a -> String #

showList :: [RangeAdd a] -> ShowS #

Eq a => Eq (RangeAdd a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeAdd

Methods

(==) :: RangeAdd a -> RangeAdd a -> Bool #

(/=) :: RangeAdd a -> RangeAdd a -> Bool #

Ord a => Ord (RangeAdd a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeAdd

Methods

compare :: RangeAdd a -> RangeAdd a -> Ordering #

(<) :: RangeAdd a -> RangeAdd a -> Bool #

(<=) :: RangeAdd a -> RangeAdd a -> Bool #

(>) :: RangeAdd a -> RangeAdd a -> Bool #

(>=) :: RangeAdd a -> RangeAdd a -> Bool #

max :: RangeAdd a -> RangeAdd a -> RangeAdd a #

min :: RangeAdd a -> RangeAdd a -> RangeAdd a #

Unbox a => Unbox (RangeAdd a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeAdd

Monoid (Max a) => SegAct (RangeAdd (Max a)) (Max a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeAdd

Methods

segAct :: RangeAdd (Max a) -> Max a -> Max a Source #

segActWithLength :: Int -> RangeAdd (Max a) -> Max a -> Max a Source #

Monoid (Min a) => SegAct (RangeAdd (Min a)) (Min a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeAdd

Methods

segAct :: RangeAdd (Min a) -> Min a -> Min a Source #

segActWithLength :: Int -> RangeAdd (Min a) -> Min a -> Min a Source #

Monoid (Sum a) => SegAct (RangeAdd (Sum a)) (Sum a) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeAdd

Methods

segAct :: RangeAdd (Sum a) -> Sum a -> Sum a Source #

segActWithLength :: Int -> RangeAdd (Sum a) -> Sum a -> Sum a Source #

newtype MVector s (RangeAdd a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeAdd

newtype MVector s (RangeAdd a) = MV_RangeAdd (MVector s a)
newtype Vector (RangeAdd a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeAdd

newtype Vector (RangeAdd a) = V_RangeAdd (Vector a)

Range set

newtype RangeSet a Source #

Monoid action \(f: x \rightarrow a\).

Example

Expand
>>> import AtCoder.Extra.Monoid (SegAct(..), RangeSet(..))
>>> import AtCoder.LazySegTree qualified as LST
>>> import Data.Bit (Bit (..))
>>> import Data.Semigroup (Product(..))
>>> seg <- LST.build @_ @(RangeSet (Product Int)) @(Product Int) $ VU.generate 4 Product -- [0, 1, 2, 3]
>>> LST.applyIn seg 0 3 $ RangeSet (Bit True, Product 5) -- [5, 5, 5, 3]
>>> getProduct <$> LST.prod seg 0 4
375

Since: 1.0.0.0

Constructors

RangeSet (RangeSetRepr a) 

Instances

Instances details
Unbox a => Vector Vector (RangeSet a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeSet

Methods

basicUnsafeFreeze :: Mutable Vector s (RangeSet a) -> ST s (Vector (RangeSet a))

basicUnsafeThaw :: Vector (RangeSet a) -> ST s (Mutable Vector s (RangeSet a))

basicLength :: Vector (RangeSet a) -> Int

basicUnsafeSlice :: Int -> Int -> Vector (RangeSet a) -> Vector (RangeSet a)

basicUnsafeIndexM :: Vector (RangeSet a) -> Int -> Box (RangeSet a)

basicUnsafeCopy :: Mutable Vector s (RangeSet a) -> Vector (RangeSet a) -> ST s ()

elemseq :: Vector (RangeSet a) -> RangeSet a -> b -> b

Unbox a => MVector MVector (RangeSet a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeSet

Monoid a => Monoid (RangeSet a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeSet

Methods

mempty :: RangeSet a #

mappend :: RangeSet a -> RangeSet a -> RangeSet a #

mconcat :: [RangeSet a] -> RangeSet a #

Semigroup (RangeSet a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeSet

Methods

(<>) :: RangeSet a -> RangeSet a -> RangeSet a #

sconcat :: NonEmpty (RangeSet a) -> RangeSet a #

stimes :: Integral b => b -> RangeSet a -> RangeSet a #

Show a => Show (RangeSet a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeSet

Methods

showsPrec :: Int -> RangeSet a -> ShowS #

show :: RangeSet a -> String #

showList :: [RangeSet a] -> ShowS #

Eq a => Eq (RangeSet a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeSet

Methods

(==) :: RangeSet a -> RangeSet a -> Bool #

(/=) :: RangeSet a -> RangeSet a -> Bool #

Ord a => Ord (RangeSet a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeSet

Methods

compare :: RangeSet a -> RangeSet a -> Ordering #

(<) :: RangeSet a -> RangeSet a -> Bool #

(<=) :: RangeSet a -> RangeSet a -> Bool #

(>) :: RangeSet a -> RangeSet a -> Bool #

(>=) :: RangeSet a -> RangeSet a -> Bool #

max :: RangeSet a -> RangeSet a -> RangeSet a #

min :: RangeSet a -> RangeSet a -> RangeSet a #

Unbox a => Unbox (RangeSet a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeSet

Monoid a => SegAct (RangeSet a) a Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeSet

Methods

segAct :: RangeSet a -> a -> a Source #

segActWithLength :: Int -> RangeSet a -> a -> a Source #

newtype MVector s (RangeSet a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeSet

newtype Vector (RangeSet a) Source #

Since: 1.0.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RangeSet

type RangeSetRepr a = (Bit, a) Source #

RangeSet internal representation. The first value represents if it is an identity action. Tuples are not the fastest representation, but it's easier to implement Unbox.

Since: 1.1.0.0

Rolling hash

data RollingHash b p Source #

Rolling hash algorithm implemented as a monoid, typically stored in a segment tree. The type parameters \(b\) and \(p\) represent the B-adic base and the modulus, respectively.

Combining RollingHash with SegTree enables \(O(\log |s|)\) string slice creation and \(O(1)\) slice comparison.

Example

Expand

It's convenient to define a type alias of RollingHash:

>>> import AtCoder.Extra.Monoid.RollingHash qualified as RH
>>> import AtCoder.SegTree qualified as ST
>>> import Data.Char (ord)
>>> import Data.Semigroup (Dual (..))
>>> type RH = RH.RollingHash 100 998244353

Let's test whether "abcba" is a palindrome:

>>> seg <- ST.build @_ @RH . VU.map (RH.unsafeNew . ord) $ VU.fromList "abcba"
>>> seg' <- ST.build @_ @(Dual RH) . VU.map (Dual . RH.unsafeNew . ord) $ VU.fromList "abcba"
>>> hash1 <- ST.prod seg 2 5       --   cba  (left to right)
>>> Dual hash2 <- ST.prod seg' 0 3 -- abc    (right to lett)
>>> hash1 == hash2
True

Since: 1.1.0.0

Instances

Instances details
Vector Vector (RollingHash b p) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RollingHash

Methods

basicUnsafeFreeze :: Mutable Vector s (RollingHash b p) -> ST s (Vector (RollingHash b p))

basicUnsafeThaw :: Vector (RollingHash b p) -> ST s (Mutable Vector s (RollingHash b p))

basicLength :: Vector (RollingHash b p) -> Int

basicUnsafeSlice :: Int -> Int -> Vector (RollingHash b p) -> Vector (RollingHash b p)

basicUnsafeIndexM :: Vector (RollingHash b p) -> Int -> Box (RollingHash b p)

basicUnsafeCopy :: Mutable Vector s (RollingHash b p) -> Vector (RollingHash b p) -> ST s ()

elemseq :: Vector (RollingHash b p) -> RollingHash b p -> b0 -> b0

MVector MVector (RollingHash b p) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RollingHash

(KnownNat b, KnownNat p) => Monoid (RollingHash b p) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RollingHash

Methods

mempty :: RollingHash b p #

mappend :: RollingHash b p -> RollingHash b p -> RollingHash b p #

mconcat :: [RollingHash b p] -> RollingHash b p #

(KnownNat b, KnownNat p) => Semigroup (RollingHash b p) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RollingHash

Methods

(<>) :: RollingHash b p -> RollingHash b p -> RollingHash b p #

sconcat :: NonEmpty (RollingHash b p) -> RollingHash b p #

stimes :: Integral b0 => b0 -> RollingHash b p -> RollingHash b p #

Show (RollingHash b p) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RollingHash

Methods

showsPrec :: Int -> RollingHash b p -> ShowS #

show :: RollingHash b p -> String #

showList :: [RollingHash b p] -> ShowS #

Eq (RollingHash b p) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RollingHash

Methods

(==) :: RollingHash b p -> RollingHash b p -> Bool #

(/=) :: RollingHash b p -> RollingHash b p -> Bool #

Unbox (RollingHash b p) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RollingHash

newtype MVector s (RollingHash b p) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RollingHash

newtype MVector s (RollingHash b p) = MV_RH (MVector s RHRepr)
newtype Vector (RollingHash b p) Source #

Since: 1.1.0.0

Instance details

Defined in AtCoder.Extra.Monoid.RollingHash

newtype Vector (RollingHash b p) = V_RH (Vector RHRepr)