Problem:
Score : 100 points

Problem Statement

You are given an array a_0, a_1, ..., a_{N-1} of length N. Process Q queries of the following types.

The type of i-th query is represented by T_i.

T_i=1: You are given two integers X_i,V_i. Replace the value of A_{X_i} with V_i.

T_i=2: You are given two integers L_i,R_i. Calculate the maximum value among A_{L_i},A_{L_i+1},\cdots,A_{R_i}.

T_i=3: You are given two integers X_i,V_i. Calculate the minimum j such that X_i \leq j \leq N, V_i \leq A_j. If there is no such j, answer j=N+1 instead.

Constraints

1 \leq N \leq 2 \times 10^5

0 \leq A_i \leq 10^9

1 \leq Q \leq 2 \times 10^5

1 \leq T_i \leq 3

1 \leq X_i \leq N, 0 \leq V_i \leq 10^9 (T_i=1,3)

1 \leq L_i \leq R_i \leq N (T_i=2)

All values in Input are integer.

Input

Input is given from Standard Input in the following format:

N Q
A_1 A_2 \cdots A_N
First query
Second query
\vdots
Q-th query

Each query is given in the following format:

If T_i=1,3,

T_i X_i V_i

If T_i=2,

T_i L_i R_i

Output

For each query with T_i=2, 3, print the answer.

Sample Input 1

5 5
1 2 3 2 1
2 1 5
3 2 3
1 3 1
2 2 4
3 1 3

Sample Output 1

3
3
2
6

First query: Print 3, which is the maximum of (A_1,A_2,A_3,A_4,A_5)=(1,2,3,2,1).

Second query: Since 3>A_2, j=2 does not satisfy the condition．Since 3 \leq A_3, print j=3.

Third query: Replace the value of A_3 with 1. It becomes A=(1,2,1,2,1).

Fourth query: Print 2, which is the maximum of (A_2,A_3,A_4)=(2,1,2).

Fifth query: Since there is no j that satisfies the condition, print j=N+1=6.

Solution in Haskell (GHC 8.8.3) (judge verdict: Accepted)

{-# OPTIONS_GHC -O2 #-}
{-# LANGUAGE BangPatterns, BinaryLiterals, CPP, DerivingStrategies  #-}
{-# LANGUAGE DerivingVia, FlexibleContexts, FlexibleInstances       #-}
{-# LANGUAGE GeneralizedNewtypeDeriving, KindSignatures, LambdaCase #-}
{-# LANGUAGE MagicHash, MultiParamTypeClasses, MultiWayIf           #-}
{-# LANGUAGE NumericUnderscores, OverloadedStrings, PatternSynonyms #-}
{-# LANGUAGE RankNTypes, RecordWildCards, ScopedTypeVariables       #-}
{-# LANGUAGE StandaloneDeriving, TupleSections, TypeApplications    #-}
{-# LANGUAGE TypeFamilies, TypeInType, UnboxedTuples, ViewPatterns  #-}

import           Control.Applicative
import           Control.Exception
import           Control.Monad
import           Control.Monad.Primitive
import           Control.Monad.Reader
import           Control.Monad.ST
import           Control.Monad.State.Strict
import           Data.Bifunctor
import           Data.Bool
import qualified Data.ByteString                   as B
import qualified Data.ByteString.Builder           as B
import qualified Data.ByteString.Char8             as C
import qualified Data.ByteString.Internal          as B
import qualified Data.ByteString.Unsafe            as B
import           Data.Char
import qualified Data.Foldable                     as F
import           Data.Function
import           Data.Functor.Identity
import qualified Data.IntMap.Strict                as IM
import qualified Data.IntSet                       as IS
import qualified Data.List                         as L
import qualified Data.Map.Strict                   as M
import           Data.Monoid
import           Data.Ord
import           Data.Primitive
import           Data.Proxy
import           Data.Ratio
import           Data.Semigroup
import qualified Data.Set                          as S
import           Data.Tuple
import qualified Data.Vector                       as V
import qualified Data.Vector.Algorithms.Intro      as Intro
import qualified Data.Vector.Fusion.Stream.Monadic as MS
import qualified Data.Vector.Fusion.Bundle.Monadic as MB
import           Data.Vector.Fusion.Util
import qualified Data.Vector.Generic               as G
import qualified Data.Vector.Generic.Mutable       as GM
import qualified Data.Vector.Mutable               as VM
import qualified Data.Vector.Primitive             as P
import qualified Data.Vector.Primitive.Mutable     as PM
import qualified Data.Vector.Unboxed               as U
import qualified Data.Vector.Unboxed.Mutable       as UM
import           Debug.Trace
import           Foreign                           hiding (void)
import           GHC.Exts
import           GHC.TypeLits
import           System.IO
import           Unsafe.Coerce

#define MOD 1000000007

main :: IO ()
main = do
    [n, q] <- map read.words <$> getLine
    xs <- U.unfoldrN n (runParser int) <$> C.getLine
    qs <- U.unfoldrN q (runParser $ (,,) <$> int <*> int <*> int) <$> C.getContents
    putStr.unlines.map show.U.toList $ solve n q xs qs

solve :: Int -> Int -> U.Vector Int -> U.Vector (Int, Int, Int) -> U.Vector Int
solve n q xs qs = runST $ do
    seg <- buildSegTree $ U.map Max xs
    fmap (U.map snd.U.filter((/= (1 :: Int)).fst))$ U.forM qs $ \case
        (1, x, v) -> do
            writeSegTree seg (x - 1) (Max v)
            return (1, -1)
        (2, l, r) -> do
            res <- getMax <$>  mappendFromTo seg (l - 1) r
            return (2, res)
        (3, x, v) -> do
            res <- lowerBoundM (x - 1) n $ \i -> do
                (Max v<=) <$> mappendFromTo seg (x - 1) (i + 1)
            return (3, res + 1)

-------------------------------------------------------------------------------
-- Utils
-------------------------------------------------------------------------------
rep :: (Monad m) => Int -> (Int -> m ()) -> m ()
rep n = flip MS.mapM_ (stream 0 n)
{-# INLINE rep #-}
rev :: (Monad m) => Int -> (Int -> m ()) -> m ()
rev !n = flip MS.mapM_ (streamR 0 n)
{-# INLINE rev #-}
stream :: (Monad m) => Int -> Int -> MS.Stream m Int
stream !l !r = MS.Stream step l where { step x | x < r = return $ MS.Yield x (x + 1) | otherwise = return MS.Done; {-# INLINE [0] step #-}}
{-# INLINE [1] stream #-}
streamR :: (Monad m) => Int -> Int -> MS.Stream m Int
streamR !l !r = MS.Stream step (r - 1) where { step x | x >= l = return $ MS.Yield x (x - 1) | otherwise = return MS.Done; {-# INLINE [0] step #-}}
{-# INLINE [1] streamR #-}
stream' :: (Monad m) => Int -> Int -> Int -> MS.Stream m Int
stream' !l !r !d = MS.Stream step l where { step x | x < r = return $ MS.Yield x (x + d) | otherwise = return MS.Done; {-# INLINE [0] step #-}}
{-# INLINE [1] stream' #-}
infixl 8 `shiftRL`, `unsafeShiftRL`
shiftRL :: Int -> Int -> Int
shiftRL = unsafeShiftRL
{-# INLINE shiftRL #-}
unsafeShiftRL :: Int -> Int -> Int
unsafeShiftRL (I# x#) (I# i#) = I# (uncheckedIShiftRL# x# i#)
{-# INLINE unsafeShiftRL #-}
type Parser a = StateT C.ByteString Maybe a
runParser :: Parser a -> C.ByteString -> Maybe (a, C.ByteString)
runParser = runStateT
{-# INLINE runParser #-}
int :: Parser Int
int = coerce $ C.readInt . C.dropWhile isSpace
{-# INLINE int #-}
int1 :: Parser Int
int1 = fmap (subtract 1) int
{-# INLINE int1 #-}
char :: Parser Char
char = coerce C.uncons
{-# INLINE char #-}
byte :: Parser Word8
byte = coerce B.uncons
{-# INLINE byte #-}
skipSpaces :: Parser ()
skipSpaces = modify' (C.dropWhile isSpace)
{-# INLINE skipSpaces #-}
lowerBoundM :: (Monad m) => Int -> Int -> (Int -> m Bool) -> m Int
lowerBoundM low high p = go low high where { go !low !high | high <= low = return high | otherwise = p mid >>= bool (go (mid + 1) high) (go low mid) where { mid = low + unsafeShiftRL (high - low) 1}}
{-# INLINE lowerBoundM #-}
upperBoundM :: (Monad m) => Int -> Int -> (Int -> m Bool) -> m Int
upperBoundM low high p = do { flg <- p high; if flg then return high else subtract 1 <$!> lowerBoundM low high (fmap not . p)}
{-# INLINE upperBoundM #-}
lowerBound :: Int -> Int -> (Int -> Bool) -> Int
lowerBound low high p = runIdentity (lowerBoundM low high (return . p))
{-# INLINE lowerBound #-}
upperBound :: Int -> Int -> (Int -> Bool) -> Int
upperBound low high p = runIdentity (upperBoundM low high (return . p))
{-# INLINE upperBound #-}
-------------------------------------------------------------------------------
-- Data.SegTree
-------------------------------------------------------------------------------
newtype SegTree s a = SegTree{getSegTree :: UM.MVector s a}
newSegTree :: (Monoid a, U.Unbox a, PrimMonad m) => Int -> m (SegTree (PrimState m) a)
newSegTree n = SegTree <$> UM.replicate (2 * extendToPowerOfTwo n) mempty
buildSegTree :: (Monoid a, U.Unbox a, PrimMonad m) => U.Vector a -> m (SegTree (PrimState m) a)
buildSegTree vec = do { let { n = extendToPowerOfTwo $ U.length vec}; tree <- UM.replicate (2 * n) mempty; U.unsafeCopy (UM.unsafeSlice n (U.length vec) tree) vec; U.forM_ (U.iterateN (n - 1) (subtract 1) (n - 1)) $ \ i -> do { x <- mappend <$> UM.unsafeRead tree (unsafeShiftL i 1) <*> UM.unsafeRead tree (unsafeShiftL i 1 .|. 1); UM.unsafeWrite tree i x}; return $ SegTree tree}
readSegTree :: (Monoid a, U.Unbox a, PrimMonad m) => SegTree (PrimState m) a -> Int -> m a
readSegTree segtree k = do { let { tree = getSegTree segtree}; let { n = unsafeShiftRL (UM.length tree) 1}; UM.unsafeRead tree (k + n)}
writeSegTree :: (Monoid a, U.Unbox a, PrimMonad m) => SegTree (PrimState m) a -> Int -> a -> m ()
writeSegTree segtree k v = do { let { tree = getSegTree segtree}; let { n = unsafeShiftRL (UM.length tree) 1}; UM.unsafeWrite tree (k + n) v; flip fix (k + n) $ \ loop !i -> when (i > 1) $ do { x <- mappend <$> UM.unsafeRead tree i <*> UM.unsafeRead tree (i `xor` 1); UM.unsafeWrite tree (unsafeShiftRL i 1) x; loop $ unsafeShiftR i 1}}
modifySegTree :: (Monoid a, U.Unbox a, PrimMonad m) => SegTree (PrimState m) a -> (a -> a) -> Int -> m ()
modifySegTree segtree f k = do { let { tree = getSegTree segtree}; let { n = unsafeShiftRL (UM.length tree) 1}; UM.unsafeModify tree f (k + n); flip fix (k + n) $ \ loop !i -> when (i > 1) $ do { x <- mappend <$> UM.unsafeRead tree i <*> UM.unsafeRead tree (i `xor` 1); UM.unsafeWrite tree (unsafeShiftRL i 1) x; loop $ unsafeShiftR i 1}}
mappendFromTo :: (Monoid a, U.Unbox a, PrimMonad m) => SegTree (PrimState m) a -> Int -> Int -> m a
mappendFromTo segtree l r = do { let { tree = getSegTree segtree}; let { n = unsafeShiftRL (UM.length tree) 1}; let { stepL l | l .&. 1 == 1 = \ acc -> mappend acc <$> UM.unsafeRead tree l | otherwise = return; stepR r | r .&. 1 == 1 = \ acc -> mappend acc <$> UM.unsafeRead tree (r - 1) | otherwise = return; go l r k | l < r = go (unsafeShiftRL (l + l .&. 1) 1) (unsafeShiftRL (r - r .&. 1) 1) $ stepL l >=> (stepR r >=> k) | otherwise = k}; go (n + l) (n + r) return mempty}
mappendTo :: (Monoid a, U.Unbox a, PrimMonad m) => SegTree (PrimState m) a -> Int -> m a
mappendTo segtree = mappendFromTo segtree 0
{-# INLINE mappendTo #-}
mappendAll :: (U.Unbox a, PrimMonad m) => SegTree (PrimState m) a -> m a
mappendAll segtree = UM.unsafeRead (getSegTree segtree) 1
{-# INLINE mappendAll #-}
extendToPowerOfTwo :: Int -> Int
extendToPowerOfTwo x | x > 1 = unsafeShiftRL (-1) (countLeadingZeros (x - 1)) + 1 | otherwise = 1