{-# LANGUAGE RecordWildCards #-}
module AtCoder.LazySegTree
(
SegAct (..),
LazySegTree (nLst, sizeLst, logLst),
new,
build,
write,
modify,
modifyM,
exchange,
read,
prod,
prodMaybe,
allProd,
applyAt,
applyIn,
minLeft,
minLeftM,
maxRight,
maxRightM,
freeze,
unsafeFreeze,
)
where
import AtCoder.Internal.Assert qualified as ACIA
import AtCoder.Internal.Bit qualified as ACIBIT
import Control.Monad (unless, when)
import Control.Monad.Primitive (PrimMonad, PrimState)
import Data.Bits (bit, countLeadingZeros, countTrailingZeros, testBit, (.&.), (.<<.), (.>>.))
import Data.Foldable (for_)
import Data.Vector.Generic.Mutable qualified as VGM
import Data.Vector.Unboxed qualified as VU
import Data.Vector.Unboxed.Mutable qualified as VUM
import GHC.Stack (HasCallStack)
import Prelude hiding (read)
class (Monoid f) => SegAct f a where
{-# INLINE segAct #-}
segAct :: f -> a -> a
segAct = Int -> f -> a -> a
forall f a. SegAct f a => Int -> f -> a -> a
segActWithLength Int
1
{-# INLINE segActWithLength #-}
segActWithLength :: Int -> f -> a -> a
segActWithLength Int
_ = f -> a -> a
forall f a. SegAct f a => f -> a -> a
segAct
data LazySegTree s f a = LazySegTree
{
forall s f a. LazySegTree s f a -> Int
nLst :: {-# UNPACK #-} !Int,
forall s f a. LazySegTree s f a -> Int
sizeLst :: {-# UNPACK #-} !Int,
forall s f a. LazySegTree s f a -> Int
logLst :: {-# UNPACK #-} !Int,
forall s f a. LazySegTree s f a -> MVector s a
dLst :: !(VUM.MVector s a),
forall s f a. LazySegTree s f a -> MVector s f
lzLst :: !(VUM.MVector s f)
}
{-# INLINE new #-}
new :: (HasCallStack, PrimMonad m, Monoid f, VU.Unbox f, Monoid a, VU.Unbox a) => Int -> m (LazySegTree (PrimState m) f a)
new :: forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, Monoid f, Unbox f, Monoid a,
Unbox a) =>
Int -> m (LazySegTree (PrimState m) f a)
new Int
nLst
| Int
nLst Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 = Vector a -> m (LazySegTree (PrimState m) f a)
forall (m :: * -> *) f a.
(PrimMonad m, Monoid f, Unbox f, Monoid a, Unbox a) =>
Vector a -> m (LazySegTree (PrimState m) f a)
build (Vector a -> m (LazySegTree (PrimState m) f a))
-> Vector a -> m (LazySegTree (PrimState m) f a)
forall a b. (a -> b) -> a -> b
$ Int -> a -> Vector a
forall a. Unbox a => Int -> a -> Vector a
VU.replicate Int
nLst a
forall a. Monoid a => a
mempty
| Bool
otherwise = [Char] -> m (LazySegTree (PrimState m) f a)
forall a. HasCallStack => [Char] -> a
error ([Char] -> m (LazySegTree (PrimState m) f a))
-> [Char] -> m (LazySegTree (PrimState m) f a)
forall a b. (a -> b) -> a -> b
$ [Char]
"new: given negative size `" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
nLst [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"`"
{-# INLINE build #-}
build :: (PrimMonad m, Monoid f, VU.Unbox f, Monoid a, VU.Unbox a) => VU.Vector a -> m (LazySegTree (PrimState m) f a)
build :: forall (m :: * -> *) f a.
(PrimMonad m, Monoid f, Unbox f, Monoid a, Unbox a) =>
Vector a -> m (LazySegTree (PrimState m) f a)
build Vector a
vs = do
let nLst :: Int
nLst = Vector a -> Int
forall a. Unbox a => Vector a -> Int
VU.length Vector a
vs
let sizeLst :: Int
sizeLst = Int -> Int
ACIBIT.bitCeil Int
nLst
let logLst :: Int
logLst = Int -> Int
forall b. FiniteBits b => b -> Int
countTrailingZeros Int
sizeLst
MVector (PrimState m) a
dLst <- Int -> a -> m (MVector (PrimState m) a)
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
Int -> a -> m (MVector (PrimState m) a)
VUM.replicate (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
sizeLst) a
forall a. Monoid a => a
mempty
MVector (PrimState m) f
lzLst <- Int -> f -> m (MVector (PrimState m) f)
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
Int -> a -> m (MVector (PrimState m) a)
VUM.replicate Int
sizeLst f
forall a. Monoid a => a
mempty
Vector a -> (Int -> a -> m ()) -> m ()
forall (m :: * -> *) a b.
(Monad m, Unbox a) =>
Vector a -> (Int -> a -> m b) -> m ()
VU.iforM_ Vector a
vs ((Int -> a -> m ()) -> m ()) -> (Int -> a -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Int
i a
v -> do
MVector (PrimState m) a -> Int -> a -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
VGM.write MVector (PrimState m) a
dLst (Int
sizeLst Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
i) a
v
let segtree :: LazySegTree (PrimState m) f a
segtree = LazySegTree {Int
MVector (PrimState m) f
MVector (PrimState m) a
nLst :: Int
sizeLst :: Int
logLst :: Int
dLst :: MVector (PrimState m) a
lzLst :: MVector (PrimState m) f
nLst :: Int
sizeLst :: Int
logLst :: Int
dLst :: MVector (PrimState m) a
lzLst :: MVector (PrimState m) f
..}
[Int] -> (Int -> m ()) -> m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Int
sizeLst Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1, Int
sizeLst Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2 .. Int
1] ((Int -> m ()) -> m ()) -> (Int -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Int
i -> do
LazySegTree (PrimState m) f a -> Int -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, Monoid f, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m ()
update LazySegTree (PrimState m) f a
segtree Int
i
LazySegTree (PrimState m) f a -> m (LazySegTree (PrimState m) f a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure LazySegTree (PrimState m) f a
segtree
{-# INLINE write #-}
write :: (HasCallStack, PrimMonad m, SegAct f a, VU.Unbox f, Monoid a, VU.Unbox a) => LazySegTree (PrimState m) f a -> Int -> a -> m ()
write :: forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> a -> m ()
write self :: LazySegTree (PrimState m) f a
self@LazySegTree {Int
MVector (PrimState m) f
MVector (PrimState m) a
nLst :: forall s f a. LazySegTree s f a -> Int
sizeLst :: forall s f a. LazySegTree s f a -> Int
logLst :: forall s f a. LazySegTree s f a -> Int
dLst :: forall s f a. LazySegTree s f a -> MVector s a
lzLst :: forall s f a. LazySegTree s f a -> MVector s f
nLst :: Int
sizeLst :: Int
logLst :: Int
dLst :: MVector (PrimState m) a
lzLst :: MVector (PrimState m) f
..} Int
p a
x = do
let !()
_ = HasCallStack => [Char] -> Int -> Int -> ()
[Char] -> Int -> Int -> ()
ACIA.checkIndex [Char]
"AtCoder.LazySegTree.write" Int
p Int
nLst
let p' :: Int
p' = Int
p Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
sizeLst
[Int] -> (Int -> m ()) -> m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Int
logLst, Int
logLst Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 .. Int
1] ((Int -> m ()) -> m ()) -> (Int -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Int
i -> do
LazySegTree (PrimState m) f a -> Int -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m ()
push LazySegTree (PrimState m) f a
self (Int -> m ()) -> Int -> m ()
forall a b. (a -> b) -> a -> b
$ Int
p' Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
i
MVector (PrimState m) a -> Int -> a -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
VGM.write MVector (PrimState m) a
dLst Int
p' a
x
[Int] -> (Int -> m ()) -> m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Int
1 .. Int
logLst] ((Int -> m ()) -> m ()) -> (Int -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Int
i -> do
LazySegTree (PrimState m) f a -> Int -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, Monoid f, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m ()
update LazySegTree (PrimState m) f a
self (Int -> m ()) -> Int -> m ()
forall a b. (a -> b) -> a -> b
$ Int
p' Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
i
{-# INLINE modify #-}
modify :: (HasCallStack, PrimMonad m, SegAct f a, VU.Unbox f, Monoid a, VU.Unbox a) => LazySegTree (PrimState m) f a -> (a -> a) -> Int -> m ()
modify :: forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> (a -> a) -> Int -> m ()
modify self :: LazySegTree (PrimState m) f a
self@LazySegTree {Int
MVector (PrimState m) f
MVector (PrimState m) a
nLst :: forall s f a. LazySegTree s f a -> Int
sizeLst :: forall s f a. LazySegTree s f a -> Int
logLst :: forall s f a. LazySegTree s f a -> Int
dLst :: forall s f a. LazySegTree s f a -> MVector s a
lzLst :: forall s f a. LazySegTree s f a -> MVector s f
nLst :: Int
sizeLst :: Int
logLst :: Int
dLst :: MVector (PrimState m) a
lzLst :: MVector (PrimState m) f
..} a -> a
f Int
p = do
let !()
_ = HasCallStack => [Char] -> Int -> Int -> ()
[Char] -> Int -> Int -> ()
ACIA.checkIndex [Char]
"AtCoder.LazySegTree.modify" Int
p Int
nLst
let p' :: Int
p' = Int
p Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
sizeLst
[Int] -> (Int -> m ()) -> m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Int
logLst, Int
logLst Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 .. Int
1] ((Int -> m ()) -> m ()) -> (Int -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Int
i -> do
LazySegTree (PrimState m) f a -> Int -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m ()
push LazySegTree (PrimState m) f a
self (Int -> m ()) -> Int -> m ()
forall a b. (a -> b) -> a -> b
$ Int
p' Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
i
MVector (PrimState m) a -> (a -> a) -> Int -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> (a -> a) -> Int -> m ()
VGM.modify MVector (PrimState m) a
dLst a -> a
f Int
p'
[Int] -> (Int -> m ()) -> m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Int
1 .. Int
logLst] ((Int -> m ()) -> m ()) -> (Int -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Int
i -> do
LazySegTree (PrimState m) f a -> Int -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, Monoid f, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m ()
update LazySegTree (PrimState m) f a
self (Int -> m ()) -> Int -> m ()
forall a b. (a -> b) -> a -> b
$ Int
p' Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
i
{-# INLINE modifyM #-}
modifyM :: (HasCallStack, PrimMonad m, SegAct f a, VU.Unbox f, Monoid a, VU.Unbox a) => LazySegTree (PrimState m) f a -> (a -> m a) -> Int -> m ()
modifyM :: forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> (a -> m a) -> Int -> m ()
modifyM self :: LazySegTree (PrimState m) f a
self@LazySegTree {Int
MVector (PrimState m) f
MVector (PrimState m) a
nLst :: forall s f a. LazySegTree s f a -> Int
sizeLst :: forall s f a. LazySegTree s f a -> Int
logLst :: forall s f a. LazySegTree s f a -> Int
dLst :: forall s f a. LazySegTree s f a -> MVector s a
lzLst :: forall s f a. LazySegTree s f a -> MVector s f
nLst :: Int
sizeLst :: Int
logLst :: Int
dLst :: MVector (PrimState m) a
lzLst :: MVector (PrimState m) f
..} a -> m a
f Int
p = do
let !()
_ = HasCallStack => [Char] -> Int -> Int -> ()
[Char] -> Int -> Int -> ()
ACIA.checkIndex [Char]
"AtCoder.LazySegTree.modify" Int
p Int
nLst
let p' :: Int
p' = Int
p Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
sizeLst
[Int] -> (Int -> m ()) -> m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Int
logLst, Int
logLst Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 .. Int
1] ((Int -> m ()) -> m ()) -> (Int -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Int
i -> do
LazySegTree (PrimState m) f a -> Int -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m ()
push LazySegTree (PrimState m) f a
self (Int -> m ()) -> Int -> m ()
forall a b. (a -> b) -> a -> b
$ Int
p' Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
i
MVector (PrimState m) a -> (a -> m a) -> Int -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> (a -> m a) -> Int -> m ()
VGM.modifyM MVector (PrimState m) a
dLst a -> m a
f Int
p'
[Int] -> (Int -> m ()) -> m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Int
1 .. Int
logLst] ((Int -> m ()) -> m ()) -> (Int -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Int
i -> do
LazySegTree (PrimState m) f a -> Int -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, Monoid f, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m ()
update LazySegTree (PrimState m) f a
self (Int -> m ()) -> Int -> m ()
forall a b. (a -> b) -> a -> b
$ Int
p' Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
i
{-# INLINE exchange #-}
exchange :: (HasCallStack, PrimMonad m, SegAct f a, VU.Unbox f, Monoid a, VU.Unbox a) => LazySegTree (PrimState m) f a -> Int -> a -> m a
exchange :: forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> a -> m a
exchange self :: LazySegTree (PrimState m) f a
self@LazySegTree {Int
MVector (PrimState m) f
MVector (PrimState m) a
nLst :: forall s f a. LazySegTree s f a -> Int
sizeLst :: forall s f a. LazySegTree s f a -> Int
logLst :: forall s f a. LazySegTree s f a -> Int
dLst :: forall s f a. LazySegTree s f a -> MVector s a
lzLst :: forall s f a. LazySegTree s f a -> MVector s f
nLst :: Int
sizeLst :: Int
logLst :: Int
dLst :: MVector (PrimState m) a
lzLst :: MVector (PrimState m) f
..} Int
p a
x = do
let !()
_ = HasCallStack => [Char] -> Int -> Int -> ()
[Char] -> Int -> Int -> ()
ACIA.checkIndex [Char]
"AtCoder.LazySegTree.exchange" Int
p Int
nLst
let p' :: Int
p' = Int
p Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
sizeLst
[Int] -> (Int -> m ()) -> m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Int
logLst, Int
logLst Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 .. Int
1] ((Int -> m ()) -> m ()) -> (Int -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Int
i -> do
LazySegTree (PrimState m) f a -> Int -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m ()
push LazySegTree (PrimState m) f a
self (Int -> m ()) -> Int -> m ()
forall a b. (a -> b) -> a -> b
$ Int
p' Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
i
a
res <- MVector (PrimState m) a -> Int -> a -> m a
forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m a
VGM.exchange MVector (PrimState m) a
dLst Int
p' a
x
[Int] -> (Int -> m ()) -> m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Int
1 .. Int
logLst] ((Int -> m ()) -> m ()) -> (Int -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Int
i -> do
LazySegTree (PrimState m) f a -> Int -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, Monoid f, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m ()
update LazySegTree (PrimState m) f a
self (Int -> m ()) -> Int -> m ()
forall a b. (a -> b) -> a -> b
$ Int
p' Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
i
a -> m a
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
res
{-# INLINE read #-}
read :: (HasCallStack, PrimMonad m, SegAct f a, VU.Unbox f, Monoid a, VU.Unbox a) => LazySegTree (PrimState m) f a -> Int -> m a
read :: forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m a
read self :: LazySegTree (PrimState m) f a
self@LazySegTree {Int
MVector (PrimState m) f
MVector (PrimState m) a
nLst :: forall s f a. LazySegTree s f a -> Int
sizeLst :: forall s f a. LazySegTree s f a -> Int
logLst :: forall s f a. LazySegTree s f a -> Int
dLst :: forall s f a. LazySegTree s f a -> MVector s a
lzLst :: forall s f a. LazySegTree s f a -> MVector s f
nLst :: Int
sizeLst :: Int
logLst :: Int
dLst :: MVector (PrimState m) a
lzLst :: MVector (PrimState m) f
..} Int
p = do
let !()
_ = HasCallStack => [Char] -> Int -> Int -> ()
[Char] -> Int -> Int -> ()
ACIA.checkIndex [Char]
"AtCoder.LazySegTree.read" Int
p Int
nLst
let p' :: Int
p' = Int
p Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
sizeLst
[Int] -> (Int -> m ()) -> m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Int
logLst, Int
logLst Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 .. Int
1] ((Int -> m ()) -> m ()) -> (Int -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Int
i -> do
LazySegTree (PrimState m) f a -> Int -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m ()
push LazySegTree (PrimState m) f a
self (Int -> m ()) -> Int -> m ()
forall a b. (a -> b) -> a -> b
$ Int
p' Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
i
MVector (PrimState m) a -> Int -> m a
forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
VGM.read MVector (PrimState m) a
dLst Int
p'
{-# INLINE prod #-}
prod :: (HasCallStack, PrimMonad m, SegAct f a, VU.Unbox f, Monoid a, VU.Unbox a) => LazySegTree (PrimState m) f a -> Int -> Int -> m a
prod :: forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> Int -> m a
prod self :: LazySegTree (PrimState m) f a
self@LazySegTree {Int
nLst :: forall s f a. LazySegTree s f a -> Int
nLst :: Int
nLst} Int
l0 Int
r0
| Bool -> Bool
not (Int -> Int -> Int -> Bool
ACIA.testInterval Int
l0 Int
r0 Int
nLst) = [Char] -> Int -> Int -> Int -> m a
forall a. HasCallStack => [Char] -> Int -> Int -> Int -> a
ACIA.errorInterval [Char]
"AtCoder.LazySegTree.prod" Int
l0 Int
r0 Int
nLst
| Int
l0 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
r0 = a -> m a
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
forall a. Monoid a => a
mempty
| Bool
otherwise = LazySegTree (PrimState m) f a -> Int -> Int -> m a
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> Int -> m a
unsafeProd LazySegTree (PrimState m) f a
self Int
l0 Int
r0
{-# INLINE prodMaybe #-}
prodMaybe :: (HasCallStack, PrimMonad m, SegAct f a, VU.Unbox f, Monoid a, VU.Unbox a) => LazySegTree (PrimState m) f a -> Int -> Int -> m (Maybe a)
prodMaybe :: forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> Int -> m (Maybe a)
prodMaybe self :: LazySegTree (PrimState m) f a
self@LazySegTree {Int
nLst :: forall s f a. LazySegTree s f a -> Int
nLst :: Int
nLst} Int
l0 Int
r0
| Bool -> Bool
not (Int -> Int -> Int -> Bool
ACIA.testInterval Int
l0 Int
r0 Int
nLst) = Maybe a -> m (Maybe a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
forall a. Maybe a
Nothing
| Int
l0 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
r0 = Maybe a -> m (Maybe a)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> Maybe a
forall a. a -> Maybe a
Just a
forall a. Monoid a => a
mempty)
| Bool
otherwise = a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> m a -> m (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LazySegTree (PrimState m) f a -> Int -> Int -> m a
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> Int -> m a
unsafeProd LazySegTree (PrimState m) f a
self Int
l0 Int
r0
{-# INLINE unsafeProd #-}
unsafeProd :: (HasCallStack, PrimMonad m, SegAct f a, VU.Unbox f, Monoid a, VU.Unbox a) => LazySegTree (PrimState m) f a -> Int -> Int -> m a
unsafeProd :: forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> Int -> m a
unsafeProd self :: LazySegTree (PrimState m) f a
self@LazySegTree {Int
MVector (PrimState m) f
MVector (PrimState m) a
nLst :: forall s f a. LazySegTree s f a -> Int
sizeLst :: forall s f a. LazySegTree s f a -> Int
logLst :: forall s f a. LazySegTree s f a -> Int
dLst :: forall s f a. LazySegTree s f a -> MVector s a
lzLst :: forall s f a. LazySegTree s f a -> MVector s f
nLst :: Int
sizeLst :: Int
logLst :: Int
dLst :: MVector (PrimState m) a
lzLst :: MVector (PrimState m) f
..} Int
l0 Int
r0 = do
let l :: Int
l = Int
l0 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
sizeLst
let r :: Int
r = Int
r0 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
sizeLst
[Int] -> (Int -> m ()) -> m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Int
logLst, Int
logLst Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 .. Int
1] ((Int -> m ()) -> m ()) -> (Int -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Int
i -> do
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (((Int
l Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
i) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.<<. Int
i) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
l) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ LazySegTree (PrimState m) f a -> Int -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m ()
push LazySegTree (PrimState m) f a
self (Int -> m ()) -> Int -> m ()
forall a b. (a -> b) -> a -> b
$ Int
l Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
i
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (((Int
r Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
i) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.<<. Int
i) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
r) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ LazySegTree (PrimState m) f a -> Int -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m ()
push LazySegTree (PrimState m) f a
self (Int -> m ()) -> Int -> m ()
forall a b. (a -> b) -> a -> b
$ (Int
r Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
i
Int -> Int -> a -> a -> m a
inner Int
l (Int
r Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) a
forall a. Monoid a => a
mempty a
forall a. Monoid a => a
mempty
where
inner :: Int -> Int -> a -> a -> m a
inner Int
l Int
r !a
smL !a
smR
| Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
r = a -> m a
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (a -> m a) -> a -> m a
forall a b. (a -> b) -> a -> b
$! a
smL a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
smR
| Bool
otherwise = do
!a
smL' <-
if Int -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Int
l Int
0
then (a
smL <>) (a -> a) -> m a -> m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MVector (PrimState m) a -> Int -> m a
forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
VGM.read MVector (PrimState m) a
dLst Int
l
else a -> m a
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
smL
!a
smR' <-
if Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Int
r Int
0
then (a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
smR) (a -> a) -> m a -> m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MVector (PrimState m) a -> Int -> m a
forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
VGM.read MVector (PrimState m) a
dLst Int
r
else a -> m a
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure a
smR
Int -> Int -> a -> a -> m a
inner ((Int
l Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
1) ((Int
r Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
1) a
smL' a
smR'
{-# INLINE allProd #-}
allProd :: (PrimMonad m, Monoid a, VU.Unbox a) => LazySegTree (PrimState m) f a -> m a
allProd :: forall (m :: * -> *) a f.
(PrimMonad m, Monoid a, Unbox a) =>
LazySegTree (PrimState m) f a -> m a
allProd LazySegTree {Int
MVector (PrimState m) a
MVector (PrimState m) f
nLst :: forall s f a. LazySegTree s f a -> Int
sizeLst :: forall s f a. LazySegTree s f a -> Int
logLst :: forall s f a. LazySegTree s f a -> Int
dLst :: forall s f a. LazySegTree s f a -> MVector s a
lzLst :: forall s f a. LazySegTree s f a -> MVector s f
nLst :: Int
sizeLst :: Int
logLst :: Int
dLst :: MVector (PrimState m) a
lzLst :: MVector (PrimState m) f
..} = MVector (PrimState m) a -> Int -> m a
forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
VGM.read MVector (PrimState m) a
dLst Int
1
{-# INLINE applyAt #-}
applyAt :: (HasCallStack, PrimMonad m, SegAct f a, VU.Unbox f, Monoid a, VU.Unbox a) => LazySegTree (PrimState m) f a -> Int -> f -> m ()
applyAt :: forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> f -> m ()
applyAt self :: LazySegTree (PrimState m) f a
self@LazySegTree {Int
MVector (PrimState m) f
MVector (PrimState m) a
nLst :: forall s f a. LazySegTree s f a -> Int
sizeLst :: forall s f a. LazySegTree s f a -> Int
logLst :: forall s f a. LazySegTree s f a -> Int
dLst :: forall s f a. LazySegTree s f a -> MVector s a
lzLst :: forall s f a. LazySegTree s f a -> MVector s f
nLst :: Int
sizeLst :: Int
logLst :: Int
dLst :: MVector (PrimState m) a
lzLst :: MVector (PrimState m) f
..} Int
p f
f = do
let !()
_ = HasCallStack => [Char] -> Int -> Int -> ()
[Char] -> Int -> Int -> ()
ACIA.checkIndex [Char]
"AtCoder.LazySegTree.applyAt" Int
p Int
nLst
let p' :: Int
p' = Int
p Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
sizeLst
[Int] -> (Int -> m ()) -> m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Int
logLst, Int
logLst Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 .. Int
1] ((Int -> m ()) -> m ()) -> (Int -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Int
i -> do
LazySegTree (PrimState m) f a -> Int -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m ()
push LazySegTree (PrimState m) f a
self (Int -> m ()) -> Int -> m ()
forall a b. (a -> b) -> a -> b
$ Int
p' Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
i
let !len :: Int
len = Int -> Int
forall a. Bits a => Int -> a
bit (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$! Int
logLst Int -> Int -> Int
forall a. Num a => a -> a -> a
- (Int
63 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int -> Int
forall b. FiniteBits b => b -> Int
countLeadingZeros Int
p')
MVector (PrimState m) a -> (a -> a) -> Int -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> (a -> a) -> Int -> m ()
VGM.modify MVector (PrimState m) a
dLst (Int -> f -> a -> a
forall f a. SegAct f a => Int -> f -> a -> a
segActWithLength Int
len f
f) Int
p'
[Int] -> (Int -> m ()) -> m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Int
1 .. Int
logLst] ((Int -> m ()) -> m ()) -> (Int -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Int
i -> do
LazySegTree (PrimState m) f a -> Int -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, Monoid f, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m ()
update LazySegTree (PrimState m) f a
self (Int -> m ()) -> Int -> m ()
forall a b. (a -> b) -> a -> b
$ Int
p' Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
i
{-# INLINE applyIn #-}
applyIn :: (HasCallStack, PrimMonad m, SegAct f a, VU.Unbox f, Monoid a, VU.Unbox a) => LazySegTree (PrimState m) f a -> Int -> Int -> f -> m ()
applyIn :: forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> Int -> f -> m ()
applyIn self :: LazySegTree (PrimState m) f a
self@LazySegTree {Int
MVector (PrimState m) f
MVector (PrimState m) a
nLst :: forall s f a. LazySegTree s f a -> Int
sizeLst :: forall s f a. LazySegTree s f a -> Int
logLst :: forall s f a. LazySegTree s f a -> Int
dLst :: forall s f a. LazySegTree s f a -> MVector s a
lzLst :: forall s f a. LazySegTree s f a -> MVector s f
nLst :: Int
sizeLst :: Int
logLst :: Int
dLst :: MVector (PrimState m) a
lzLst :: MVector (PrimState m) f
..} Int
l0 Int
r0 f
f
| Int
l0 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
r0 = () -> m ()
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
| Bool
otherwise = do
let l :: Int
l = Int
l0 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
sizeLst
let r :: Int
r = Int
r0 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
sizeLst
[Int] -> (Int -> m ()) -> m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Int
logLst, Int
logLst Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 .. Int
1] ((Int -> m ()) -> m ()) -> (Int -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Int
i -> do
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (((Int
l Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
i) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.<<. Int
i) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
l) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ LazySegTree (PrimState m) f a -> Int -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m ()
push LazySegTree (PrimState m) f a
self (Int
l Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
i)
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (((Int
r Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
i) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.<<. Int
i) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
r) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ LazySegTree (PrimState m) f a -> Int -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m ()
push LazySegTree (PrimState m) f a
self ((Int
r Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
i)
Int -> Int -> m ()
inner Int
l (Int
r Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)
[Int] -> (Int -> m ()) -> m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Int
1 .. Int
logLst] ((Int -> m ()) -> m ()) -> (Int -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Int
i -> do
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (((Int
l Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
i) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.<<. Int
i) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
l) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ LazySegTree (PrimState m) f a -> Int -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, Monoid f, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m ()
update LazySegTree (PrimState m) f a
self (Int
l Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
i)
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (((Int
r Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
i) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.<<. Int
i) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
r) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ LazySegTree (PrimState m) f a -> Int -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, Monoid f, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m ()
update LazySegTree (PrimState m) f a
self ((Int
r Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
i)
where
!()
_ = HasCallStack => [Char] -> Int -> Int -> Int -> ()
[Char] -> Int -> Int -> Int -> ()
ACIA.checkInterval [Char]
"AtCoder.LazySegTree.applyIn" Int
l0 Int
r0 Int
nLst
inner :: Int -> Int -> m ()
inner Int
l Int
r
| Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
r = () -> m ()
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
| Bool
otherwise = do
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Int
l Int
0) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ do
LazySegTree (PrimState m) f a -> Int -> f -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> f -> m ()
allApply LazySegTree (PrimState m) f a
self Int
l f
f
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Int -> Int -> Bool
forall a. Bits a => a -> Int -> Bool
testBit Int
r Int
0) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ do
LazySegTree (PrimState m) f a -> Int -> f -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> f -> m ()
allApply LazySegTree (PrimState m) f a
self Int
r f
f
Int -> Int -> m ()
inner ((Int
l Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
1) ((Int
r Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
1)
{-# INLINE minLeft #-}
minLeft :: (HasCallStack, PrimMonad m, SegAct f a, VU.Unbox f, Monoid a, VU.Unbox a) => LazySegTree (PrimState m) f a -> Int -> (a -> Bool) -> m Int
minLeft :: forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> (a -> Bool) -> m Int
minLeft LazySegTree (PrimState m) f a
seg Int
r0 a -> Bool
g = LazySegTree (PrimState m) f a -> Int -> (a -> m Bool) -> m Int
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> (a -> m Bool) -> m Int
minLeftM LazySegTree (PrimState m) f a
seg Int
r0 (Bool -> m Bool
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> m Bool) -> (a -> Bool) -> a -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
g)
{-# INLINE minLeftM #-}
minLeftM :: (HasCallStack, PrimMonad m, SegAct f a, VU.Unbox f, Monoid a, VU.Unbox a) => LazySegTree (PrimState m) f a -> Int -> (a -> m Bool) -> m Int
minLeftM :: forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> (a -> m Bool) -> m Int
minLeftM self :: LazySegTree (PrimState m) f a
self@LazySegTree {Int
MVector (PrimState m) f
MVector (PrimState m) a
nLst :: forall s f a. LazySegTree s f a -> Int
sizeLst :: forall s f a. LazySegTree s f a -> Int
logLst :: forall s f a. LazySegTree s f a -> Int
dLst :: forall s f a. LazySegTree s f a -> MVector s a
lzLst :: forall s f a. LazySegTree s f a -> MVector s f
nLst :: Int
sizeLst :: Int
logLst :: Int
dLst :: MVector (PrimState m) a
lzLst :: MVector (PrimState m) f
..} Int
r0 a -> m Bool
g = do
Bool
b <- a -> m Bool
g a
forall a. Monoid a => a
mempty
let !()
_ = HasCallStack => Bool -> [Char] -> ()
Bool -> [Char] -> ()
ACIA.runtimeAssert Bool
b [Char]
"AtCoder.LazySegTree.minLeftM: `g empty` returned `False`"
if Int
r0 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0
then Int -> m Int
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
0
else do
let r :: Int
r = Int
r0 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
sizeLst
[Int] -> (Int -> m ()) -> m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Int
logLst, Int
logLst Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 .. Int
1] ((Int -> m ()) -> m ()) -> (Int -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Int
i -> do
LazySegTree (PrimState m) f a -> Int -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m ()
push LazySegTree (PrimState m) f a
self (Int -> m ()) -> Int -> m ()
forall a b. (a -> b) -> a -> b
$ (Int
r Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
i
Int -> a -> m Int
inner Int
r a
forall a. Monoid a => a
mempty
where
!()
_ = HasCallStack => Bool -> [Char] -> ()
Bool -> [Char] -> ()
ACIA.runtimeAssert (Int
0 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
r0 Bool -> Bool -> Bool
&& Int
r0 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
nLst) ([Char] -> ()) -> [Char] -> ()
forall a b. (a -> b) -> a -> b
$ [Char]
"AtCoder.LazySegTree.minLeftM: given invalid `right` index `" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
r0 [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"` over length `" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
nLst [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"`"
inner :: Int -> a -> m Int
inner Int
r !a
sm = do
let r' :: Int
r' = Int -> Int
forall {b}. (Integral b, Bits b) => b -> b
chooseBit (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Int
r Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
!a
sm' <- (a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
sm) (a -> a) -> m a -> m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MVector (PrimState m) a -> Int -> m a
forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
VGM.read MVector (PrimState m) a
dLst Int
r'
Bool
b <- a -> m Bool
g a
sm'
if Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Bool
b
then do
Int -> a -> m Int
inner2 Int
r' a
sm
else do
if (Int
r' Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. (-Int
r')) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
r'
then Int -> a -> m Int
inner Int
r' a
sm'
else Int -> m Int
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
0
chooseBit :: b -> b
chooseBit b
r
| b
r b -> b -> Bool
forall a. Ord a => a -> a -> Bool
> b
1 Bool -> Bool -> Bool
&& b -> Bool
forall a. Integral a => a -> Bool
odd b
r = b -> b
chooseBit (b -> b) -> b -> b
forall a b. (a -> b) -> a -> b
$ b
r b -> Int -> b
forall a. Bits a => a -> Int -> a
.>>. Int
1
| Bool
otherwise = b
r
inner2 :: Int -> a -> m Int
inner2 Int
r a
sm
| Int
r Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sizeLst = do
LazySegTree (PrimState m) f a -> Int -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m ()
push LazySegTree (PrimState m) f a
self Int
r
let r' :: Int
r' = Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
r Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
!a
sm' <- (a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
sm) (a -> a) -> m a -> m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MVector (PrimState m) a -> Int -> m a
forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
VGM.read MVector (PrimState m) a
dLst Int
r'
Bool
b <- a -> m Bool
g a
sm'
if Bool
b
then Int -> a -> m Int
inner2 (Int
r' Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) a
sm'
else Int -> a -> m Int
inner2 Int
r' a
sm
| Bool
otherwise = Int -> m Int
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> m Int) -> Int -> m Int
forall a b. (a -> b) -> a -> b
$ Int
r Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
sizeLst
{-# INLINE maxRight #-}
maxRight :: (HasCallStack, PrimMonad m, SegAct f a, VU.Unbox f, Monoid a, VU.Unbox a) => LazySegTree (PrimState m) f a -> Int -> (a -> Bool) -> m Int
maxRight :: forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> (a -> Bool) -> m Int
maxRight LazySegTree (PrimState m) f a
seg Int
l0 a -> Bool
g = LazySegTree (PrimState m) f a -> Int -> (a -> m Bool) -> m Int
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> (a -> m Bool) -> m Int
maxRightM LazySegTree (PrimState m) f a
seg Int
l0 (Bool -> m Bool
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Bool -> m Bool) -> (a -> Bool) -> a -> m Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
g)
{-# INLINE maxRightM #-}
maxRightM :: (HasCallStack, PrimMonad m, SegAct f a, VU.Unbox f, Monoid a, VU.Unbox a) => LazySegTree (PrimState m) f a -> Int -> (a -> m Bool) -> m Int
maxRightM :: forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> (a -> m Bool) -> m Int
maxRightM self :: LazySegTree (PrimState m) f a
self@LazySegTree {Int
MVector (PrimState m) f
MVector (PrimState m) a
nLst :: forall s f a. LazySegTree s f a -> Int
sizeLst :: forall s f a. LazySegTree s f a -> Int
logLst :: forall s f a. LazySegTree s f a -> Int
dLst :: forall s f a. LazySegTree s f a -> MVector s a
lzLst :: forall s f a. LazySegTree s f a -> MVector s f
nLst :: Int
sizeLst :: Int
logLst :: Int
dLst :: MVector (PrimState m) a
lzLst :: MVector (PrimState m) f
..} Int
l0 a -> m Bool
g = do
Bool
b <- a -> m Bool
g a
forall a. Monoid a => a
mempty
let !()
_ = HasCallStack => Bool -> [Char] -> ()
Bool -> [Char] -> ()
ACIA.runtimeAssert Bool
b [Char]
"AtCoder.LazySegTree.maxRightM: `g mempty` returned `False`"
if Int
l0 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
nLst
then Int -> m Int
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
nLst
else do
let l :: Int
l = Int
l0 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
sizeLst
[Int] -> (Int -> m ()) -> m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Int
logLst, Int
logLst Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 .. Int
1] ((Int -> m ()) -> m ()) -> (Int -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Int
i -> do
LazySegTree (PrimState m) f a -> Int -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m ()
push LazySegTree (PrimState m) f a
self (Int
l Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
i)
Int -> a -> m Int
inner Int
l a
forall a. Monoid a => a
mempty
where
!()
_ = HasCallStack => Bool -> [Char] -> ()
Bool -> [Char] -> ()
ACIA.runtimeAssert (Int
0 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
l0 Bool -> Bool -> Bool
&& Int
l0 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
nLst) ([Char] -> ()) -> [Char] -> ()
forall a b. (a -> b) -> a -> b
$ [Char]
"AtCoder.LazySegTree.maxRightM: given invalid `left` index `" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
l0 [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"` over length `" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Int -> [Char]
forall a. Show a => a -> [Char]
show Int
nLst [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"`"
inner :: Int -> a -> m Int
inner Int
l !a
sm = do
let l' :: Int
l' = Int -> Int
chooseBit Int
l
!a
sm' <- (a
sm <>) (a -> a) -> m a -> m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MVector (PrimState m) a -> Int -> m a
forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
VGM.read MVector (PrimState m) a
dLst Int
l'
Bool
b <- a -> m Bool
g a
sm'
if Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ Bool
b
then do
Int -> a -> m Int
inner2 Int
l' a
sm
else do
let l'' :: Int
l'' = Int
l' Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
if (Int
l'' Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. (-Int
l'')) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
l''
then Int -> a -> m Int
inner Int
l'' a
sm'
else Int -> m Int
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
nLst
chooseBit :: Int -> Int
chooseBit :: Int -> Int
chooseBit Int
l
| Int -> Bool
forall a. Integral a => a -> Bool
even Int
l = Int -> Int
chooseBit (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$ Int
l Int -> Int -> Int
forall a. Bits a => a -> Int -> a
.>>. Int
1
| Bool
otherwise = Int
l
inner2 :: Int -> a -> m Int
inner2 Int
l !a
sm
| Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sizeLst = do
LazySegTree (PrimState m) f a -> Int -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m ()
push LazySegTree (PrimState m) f a
self Int
l
let l' :: Int
l' = Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
l
!a
sm' <- (a
sm <>) (a -> a) -> m a -> m a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MVector (PrimState m) a -> Int -> m a
forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
VGM.read MVector (PrimState m) a
dLst Int
l'
Bool
b <- a -> m Bool
g a
sm'
if Bool
b
then Int -> a -> m Int
inner2 (Int
l' Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) a
sm'
else Int -> a -> m Int
inner2 Int
l' a
sm
| Bool
otherwise = Int -> m Int
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> m Int) -> Int -> m Int
forall a b. (a -> b) -> a -> b
$ Int
l Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
sizeLst
{-# INLINE freeze #-}
freeze :: (PrimMonad m, SegAct f a, VU.Unbox f, Monoid a, VU.Unbox a) => LazySegTree (PrimState m) f a -> m (VU.Vector a)
freeze :: forall (m :: * -> *) f a.
(PrimMonad m, SegAct f a, Unbox f, Monoid a, Unbox a) =>
LazySegTree (PrimState m) f a -> m (Vector a)
freeze self :: LazySegTree (PrimState m) f a
self@LazySegTree {Int
MVector (PrimState m) f
MVector (PrimState m) a
nLst :: forall s f a. LazySegTree s f a -> Int
sizeLst :: forall s f a. LazySegTree s f a -> Int
logLst :: forall s f a. LazySegTree s f a -> Int
dLst :: forall s f a. LazySegTree s f a -> MVector s a
lzLst :: forall s f a. LazySegTree s f a -> MVector s f
nLst :: Int
sizeLst :: Int
logLst :: Int
dLst :: MVector (PrimState m) a
lzLst :: MVector (PrimState m) f
..} = do
[Int] -> (Int -> m ()) -> m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Int
1 .. Int
sizeLst Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> m ()) -> m ()) -> (Int -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Int
i -> do
LazySegTree (PrimState m) f a -> Int -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m ()
push LazySegTree (PrimState m) f a
self Int
i
MVector (PrimState m) a -> m (Vector a)
forall a (m :: * -> *).
(Unbox a, PrimMonad m) =>
MVector (PrimState m) a -> m (Vector a)
VU.freeze (MVector (PrimState m) a -> m (Vector a))
-> (MVector (PrimState m) a -> MVector (PrimState m) a)
-> MVector (PrimState m) a
-> m (Vector a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> MVector (PrimState m) a -> MVector (PrimState m) a
forall a s. Unbox a => Int -> MVector s a -> MVector s a
VUM.take Int
nLst (MVector (PrimState m) a -> m (Vector a))
-> MVector (PrimState m) a -> m (Vector a)
forall a b. (a -> b) -> a -> b
$ Int -> MVector (PrimState m) a -> MVector (PrimState m) a
forall a s. Unbox a => Int -> MVector s a -> MVector s a
VUM.drop Int
sizeLst MVector (PrimState m) a
dLst
{-# INLINE unsafeFreeze #-}
unsafeFreeze :: (PrimMonad m, SegAct f a, VU.Unbox f, Monoid a, VU.Unbox a) => LazySegTree (PrimState m) f a -> m (VU.Vector a)
unsafeFreeze :: forall (m :: * -> *) f a.
(PrimMonad m, SegAct f a, Unbox f, Monoid a, Unbox a) =>
LazySegTree (PrimState m) f a -> m (Vector a)
unsafeFreeze self :: LazySegTree (PrimState m) f a
self@LazySegTree {Int
MVector (PrimState m) f
MVector (PrimState m) a
nLst :: forall s f a. LazySegTree s f a -> Int
sizeLst :: forall s f a. LazySegTree s f a -> Int
logLst :: forall s f a. LazySegTree s f a -> Int
dLst :: forall s f a. LazySegTree s f a -> MVector s a
lzLst :: forall s f a. LazySegTree s f a -> MVector s f
nLst :: Int
sizeLst :: Int
logLst :: Int
dLst :: MVector (PrimState m) a
lzLst :: MVector (PrimState m) f
..} = do
[Int] -> (Int -> m ()) -> m ()
forall (t :: * -> *) (f :: * -> *) a b.
(Foldable t, Applicative f) =>
t a -> (a -> f b) -> f ()
for_ [Int
1 .. Int
sizeLst Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> m ()) -> m ()) -> (Int -> m ()) -> m ()
forall a b. (a -> b) -> a -> b
$ \Int
i -> do
LazySegTree (PrimState m) f a -> Int -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m ()
push LazySegTree (PrimState m) f a
self Int
i
MVector (PrimState m) a -> m (Vector a)
forall a (m :: * -> *).
(Unbox a, PrimMonad m) =>
MVector (PrimState m) a -> m (Vector a)
VU.unsafeFreeze (MVector (PrimState m) a -> m (Vector a))
-> (MVector (PrimState m) a -> MVector (PrimState m) a)
-> MVector (PrimState m) a
-> m (Vector a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> MVector (PrimState m) a -> MVector (PrimState m) a
forall a s. Unbox a => Int -> MVector s a -> MVector s a
VUM.take Int
nLst (MVector (PrimState m) a -> m (Vector a))
-> MVector (PrimState m) a -> m (Vector a)
forall a b. (a -> b) -> a -> b
$ Int -> MVector (PrimState m) a -> MVector (PrimState m) a
forall a s. Unbox a => Int -> MVector s a -> MVector s a
VUM.drop Int
sizeLst MVector (PrimState m) a
dLst
{-# INLINE update #-}
update :: (HasCallStack, PrimMonad m, Monoid f, VU.Unbox f, Monoid a, VU.Unbox a) => LazySegTree (PrimState m) f a -> Int -> m ()
update :: forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, Monoid f, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m ()
update LazySegTree {Int
MVector (PrimState m) f
MVector (PrimState m) a
nLst :: forall s f a. LazySegTree s f a -> Int
sizeLst :: forall s f a. LazySegTree s f a -> Int
logLst :: forall s f a. LazySegTree s f a -> Int
dLst :: forall s f a. LazySegTree s f a -> MVector s a
lzLst :: forall s f a. LazySegTree s f a -> MVector s f
nLst :: Int
sizeLst :: Int
logLst :: Int
dLst :: MVector (PrimState m) a
lzLst :: MVector (PrimState m) f
..} Int
k = do
a
opL <- MVector (PrimState m) a -> Int -> m a
forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
VGM.read MVector (PrimState m) a
dLst (Int -> m a) -> Int -> m a
forall a b. (a -> b) -> a -> b
$ Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
k
a
opR <- MVector (PrimState m) a -> Int -> m a
forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
VGM.read MVector (PrimState m) a
dLst (Int -> m a) -> Int -> m a
forall a b. (a -> b) -> a -> b
$ Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
MVector (PrimState m) a -> Int -> a -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
VGM.write MVector (PrimState m) a
dLst Int
k (a -> m ()) -> a -> m ()
forall a b. (a -> b) -> a -> b
$! a
opL a -> a -> a
forall a. Semigroup a => a -> a -> a
<> a
opR
{-# INLINE allApply #-}
allApply :: (HasCallStack, PrimMonad m, SegAct f a, VU.Unbox f, Monoid a, VU.Unbox a) => LazySegTree (PrimState m) f a -> Int -> f -> m ()
allApply :: forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> f -> m ()
allApply LazySegTree {Int
MVector (PrimState m) f
MVector (PrimState m) a
nLst :: forall s f a. LazySegTree s f a -> Int
sizeLst :: forall s f a. LazySegTree s f a -> Int
logLst :: forall s f a. LazySegTree s f a -> Int
dLst :: forall s f a. LazySegTree s f a -> MVector s a
lzLst :: forall s f a. LazySegTree s f a -> MVector s f
nLst :: Int
sizeLst :: Int
logLst :: Int
dLst :: MVector (PrimState m) a
lzLst :: MVector (PrimState m) f
..} Int
k f
f = do
let !len :: Int
len = Int -> Int
forall a. Bits a => Int -> a
bit (Int -> Int) -> Int -> Int
forall a b. (a -> b) -> a -> b
$! Int
logLst Int -> Int -> Int
forall a. Num a => a -> a -> a
- (Int
63 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int -> Int
forall b. FiniteBits b => b -> Int
countLeadingZeros Int
k)
MVector (PrimState m) a -> (a -> a) -> Int -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> (a -> a) -> Int -> m ()
VGM.modify MVector (PrimState m) a
dLst (Int -> f -> a -> a
forall f a. SegAct f a => Int -> f -> a -> a
segActWithLength Int
len f
f) Int
k
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
k Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
sizeLst) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ do
MVector (PrimState m) f -> (f -> f) -> Int -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> (a -> a) -> Int -> m ()
VGM.modify MVector (PrimState m) f
lzLst (f
f <>) Int
k
{-# INLINE push #-}
push :: (HasCallStack, PrimMonad m, SegAct f a, VU.Unbox f, Monoid a, VU.Unbox a) => LazySegTree (PrimState m) f a -> Int -> m ()
push :: forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> m ()
push self :: LazySegTree (PrimState m) f a
self@LazySegTree {Int
MVector (PrimState m) f
MVector (PrimState m) a
nLst :: forall s f a. LazySegTree s f a -> Int
sizeLst :: forall s f a. LazySegTree s f a -> Int
logLst :: forall s f a. LazySegTree s f a -> Int
dLst :: forall s f a. LazySegTree s f a -> MVector s a
lzLst :: forall s f a. LazySegTree s f a -> MVector s f
nLst :: Int
sizeLst :: Int
logLst :: Int
dLst :: MVector (PrimState m) a
lzLst :: MVector (PrimState m) f
..} Int
k = do
f
lzK <- MVector (PrimState m) f -> Int -> m f
forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> m a
VGM.read MVector (PrimState m) f
lzLst Int
k
LazySegTree (PrimState m) f a -> Int -> f -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> f -> m ()
allApply LazySegTree (PrimState m) f a
self (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
k) f
lzK
LazySegTree (PrimState m) f a -> Int -> f -> m ()
forall (m :: * -> *) f a.
(HasCallStack, PrimMonad m, SegAct f a, Unbox f, Monoid a,
Unbox a) =>
LazySegTree (PrimState m) f a -> Int -> f -> m ()
allApply LazySegTree (PrimState m) f a
self (Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
k Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) f
lzK
MVector (PrimState m) f -> Int -> f -> m ()
forall (m :: * -> *) (v :: * -> * -> *) a.
(HasCallStack, PrimMonad m, MVector v a) =>
v (PrimState m) a -> Int -> a -> m ()
VGM.write MVector (PrimState m) f
lzLst Int
k f
forall a. Monoid a => a
mempty