{-# LANGUAGE TypeFamilies #-}
module AtCoder.Extra.Monoid.RangeAdd
(
RangeAdd (..),
new,
unRangeAdd,
act,
)
where
import AtCoder.LazySegTree (SegAct (..))
import Data.Semigroup (stimes, Sum (..), Max(..), Min(..))
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
newtype RangeAdd a = RangeAdd a
deriving newtype
(
RangeAdd a -> RangeAdd a -> Bool
(RangeAdd a -> RangeAdd a -> Bool)
-> (RangeAdd a -> RangeAdd a -> Bool) -> Eq (RangeAdd a)
forall a. Eq a => RangeAdd a -> RangeAdd a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => RangeAdd a -> RangeAdd a -> Bool
== :: RangeAdd a -> RangeAdd a -> Bool
$c/= :: forall a. Eq a => RangeAdd a -> RangeAdd a -> Bool
/= :: RangeAdd a -> RangeAdd a -> Bool
Eq,
Eq (RangeAdd a)
Eq (RangeAdd a) =>
(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)
-> (RangeAdd a -> RangeAdd a -> RangeAdd a)
-> (RangeAdd a -> RangeAdd a -> RangeAdd a)
-> Ord (RangeAdd a)
RangeAdd a -> RangeAdd a -> Bool
RangeAdd a -> RangeAdd a -> Ordering
RangeAdd a -> RangeAdd a -> RangeAdd a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (RangeAdd a)
forall a. Ord a => RangeAdd a -> RangeAdd a -> Bool
forall a. Ord a => RangeAdd a -> RangeAdd a -> Ordering
forall a. Ord a => RangeAdd a -> RangeAdd a -> RangeAdd a
$ccompare :: forall a. Ord a => RangeAdd a -> RangeAdd a -> Ordering
compare :: RangeAdd a -> RangeAdd a -> Ordering
$c< :: forall a. Ord a => RangeAdd a -> RangeAdd a -> Bool
< :: RangeAdd a -> RangeAdd a -> Bool
$c<= :: forall a. Ord a => RangeAdd a -> RangeAdd a -> Bool
<= :: RangeAdd a -> RangeAdd a -> Bool
$c> :: forall a. Ord a => RangeAdd a -> RangeAdd a -> Bool
> :: RangeAdd a -> RangeAdd a -> Bool
$c>= :: forall a. Ord a => RangeAdd a -> RangeAdd a -> Bool
>= :: RangeAdd a -> RangeAdd a -> Bool
$cmax :: forall a. Ord a => RangeAdd a -> RangeAdd a -> RangeAdd a
max :: RangeAdd a -> RangeAdd a -> RangeAdd a
$cmin :: forall a. Ord a => RangeAdd a -> RangeAdd a -> RangeAdd a
min :: RangeAdd a -> RangeAdd a -> RangeAdd a
Ord,
Int -> RangeAdd a -> ShowS
[RangeAdd a] -> ShowS
RangeAdd a -> String
(Int -> RangeAdd a -> ShowS)
-> (RangeAdd a -> String)
-> ([RangeAdd a] -> ShowS)
-> Show (RangeAdd a)
forall a. Show a => Int -> RangeAdd a -> ShowS
forall a. Show a => [RangeAdd a] -> ShowS
forall a. Show a => RangeAdd a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall a. Show a => Int -> RangeAdd a -> ShowS
showsPrec :: Int -> RangeAdd a -> ShowS
$cshow :: forall a. Show a => RangeAdd a -> String
show :: RangeAdd a -> String
$cshowList :: forall a. Show a => [RangeAdd a] -> ShowS
showList :: [RangeAdd a] -> ShowS
Show
)
{-# INLINE new #-}
new :: a -> RangeAdd a
new :: forall a. a -> RangeAdd a
new = a -> RangeAdd a
forall a. a -> RangeAdd a
RangeAdd
{-# INLINE unRangeAdd #-}
unRangeAdd :: RangeAdd a -> a
unRangeAdd :: forall a. RangeAdd a -> a
unRangeAdd (RangeAdd a
a) = a
a
{-# INLINE act #-}
act :: (Semigroup a) => RangeAdd a -> a -> a
act :: forall a. Semigroup a => RangeAdd a -> a -> a
act (RangeAdd a
dx) a
x = a
dx a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
x
{-# INLINE actWithLength #-}
actWithLength :: (Semigroup a) => Int -> RangeAdd a -> a -> a
actWithLength :: forall a. Semigroup a => Int -> RangeAdd a -> a -> a
actWithLength Int
len (RangeAdd a
f) a
x = Int -> a -> a
forall b. Integral b => b -> a -> a
forall a b. (Semigroup a, Integral b) => b -> a -> a
stimes Int
len a
f a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
x
instance (Semigroup a) => Semigroup (RangeAdd a) where
{-# INLINE (<>) #-}
(RangeAdd a
a) <> :: RangeAdd a -> RangeAdd a -> RangeAdd a
<> (RangeAdd a
b) = a -> RangeAdd a
forall a. a -> RangeAdd a
RangeAdd (a -> RangeAdd a) -> a -> RangeAdd a
forall a b. (a -> b) -> a -> b
$! a
a a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
b
instance (Monoid a) => Monoid (RangeAdd a) where
{-# INLINE mempty #-}
mempty :: RangeAdd a
mempty = a -> RangeAdd a
forall a. a -> RangeAdd a
RangeAdd a
forall a. Monoid a => a
mempty
instance (Monoid (Sum a)) => SegAct (RangeAdd (Sum a)) (Sum a) where
{-# INLINE segActWithLength #-}
segActWithLength :: Int -> RangeAdd (Sum a) -> Sum a -> Sum a
segActWithLength Int
len RangeAdd (Sum a)
f Sum a
x = Int -> RangeAdd (Sum a) -> Sum a -> Sum a
forall a. Semigroup a => Int -> RangeAdd a -> a -> a
actWithLength Int
len RangeAdd (Sum a)
f Sum a
x
instance (Monoid (Max a)) => SegAct (RangeAdd (Max a)) (Max a) where
{-# INLINE segActWithLength #-}
segActWithLength :: Int -> RangeAdd (Max a) -> Max a -> Max a
segActWithLength Int
len RangeAdd (Max a)
f Max a
x = Int -> RangeAdd (Max a) -> Max a -> Max a
forall a. Semigroup a => Int -> RangeAdd a -> a -> a
actWithLength Int
len RangeAdd (Max a)
f Max a
x
instance (Monoid (Min a)) => SegAct (RangeAdd (Min a)) (Min a) where
{-# INLINE segActWithLength #-}
segActWithLength :: Int -> RangeAdd (Min a) -> Min a -> Min a
segActWithLength Int
len RangeAdd (Min a)
f Min a
x = Int -> RangeAdd (Min a) -> Min a -> Min a
forall a. Semigroup a => Int -> RangeAdd a -> a -> a
actWithLength Int
len RangeAdd (Min a)
f Min a
x
newtype instance VU.MVector s (RangeAdd a) = MV_RangeAdd (VU.MVector s a)
newtype instance VU.Vector (RangeAdd a) = V_RangeAdd (VU.Vector a)
deriving instance (VU.Unbox a) => VGM.MVector VUM.MVector (RangeAdd a)
deriving instance (VU.Unbox a) => VG.Vector VU.Vector (RangeAdd a)
instance (VU.Unbox a) => VU.Unbox (RangeAdd a)