Problem:
Range Minimum Query (RMQ)

Write a program which manipulates a sequence A = {a0, a1, . . . , an-1} with the following operations:

find(s, t): report the minimum element in as, as+1, . . . ,at.

update(i, x): change ai to x.

Note that the initial values of ai (i = 0, 1, . . . , n−1) are 231-1.

Input

n q
com0 x0 y0
com1 x1 y1
...
comq−1 xq−1 yq−1

In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, q queries are given where com represents the type of queries. '0' denotes update(xi, yi) and '1' denotes find(xi, yi).

Output

For each find operation, print the minimum element.

Constraints

1 ≤ n ≤ 100000

1 ≤ q ≤ 100000

If comi is 0, then 0 ≤ xi < n, 0 ≤ yi < 231-1.

If comi is 1, then 0 ≤ xi < n, 0 ≤ yi < n.

Sample Input 1

3 5
0 0 1
0 1 2
0 2 3
1 0 2
1 1 2

Sample Output 1

1
2

Sample Input 2

1 3
1 0 0
0 0 5
1 0 0

Sample Output 2

2147483647
5

Solution in Haskell (judge verdict: Time Limit Exceeded)

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}
{-# LANGUAGE BangPatterns #-}
module Main (main) where
import Data.Int (Int32)
import Data.Ix
import Control.Monad
import Control.Applicative
import Data.Monoid
import Data.Maybe (fromJust)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as B8
newtype Min = Min {fromMin :: Int32 } deriving (Ord,Show,Eq,Read)
instance Monoid Min where
  mempty = Min maxBound
  mappend = min

newtype Max = Max Int deriving (Ord,Show,Eq)
instance Monoid Max where
  mempty = Max minBound
  mappend = max

getInts :: IO [Int]
getInts =  map (fst . fromJust . B8.readInt) . B8.words <$> B8.getLine

main :: IO ()
main = do
  n:q:_ <- getInts
  let bst = fromList $ replicate n mempty :: STree Min
  queries <- replicateM q $ getInts
  go bst queries
  return ()
  where
    go _ [] = return ()
    go tree ((q:x:y:_):queries)
      -- | traceShow tree False = undefined
      | q == 0 = do -- update
          let tree' = update tree x (Min $ fromIntegral y)
          go tree' queries
      | otherwise = do -- find
          print . fromMin $ query tree (x, y)
          go tree queries
    go _ _ = error "undefined pattern match"

type Range = (Int,Int)
data STree v = Leaf {-# UNPACK #-}!Int !v
             | Branch {-# UNPACK #-}!Range !v !(STree v) !(STree v)
             deriving Show

range :: STree v-> Range
range !(Leaf r _) = (r,r)
range !(Branch r _ _ _) = r
{-# INLINE range #-}

val :: STree v -> v
val !(Leaf _ v) = v
val !(Branch _ v _ _) = v
{-# INLINE val #-}

left,right :: STree v -> Int
left = fst . Main.range
right = snd . Main.range
{-#INLINE left  #-}
{-#INLINE right #-}

fromList :: Monoid v => [v] -> STree v
fromList !xs = makeTree 0 (length xs) xs
{-# INLINE fromList #-}

makeTree :: Monoid v => Int -> Int -> [v] -> STree v
makeTree !l !r !es = loop $ map (uncurry f) (zip [l..r] es)
  where
    loop ![x] = x
    loop !xs = loop $ buildTree xs
    f :: Int -> v -> STree v
    f !ix !v = Leaf ix v
    buildTree !(a:b:ys) = let v = val a `mappend` val b
                          in Branch (left a,right b) v a b : buildTree ys
    buildTree !x = x
{-# SPECIALIZE INLINE makeTree :: Int -> Int -> [Min] -> STree Min #-}

query :: Monoid v => STree v -> (Int, Int) -> v
query !t shape = loop t
  where
    loop (Leaf ix v)
      = if shape `inRange` ix then v else mempty
    loop (Branch (leftist,rightist) v lt rt)
      | shape `inRange` leftist
        && shape `inRange` rightist = v
      | otherwise = loop lt `mappend` loop rt
{-# SPECIALIZE INLINE query :: STree Min -> (Int,Int) -> Min #-}

update :: Monoid v => STree v -> Int -> v -> STree v
update !tree !ix !v = case tree of
  Leaf i _ -> if ix == i
              then Leaf i v
              else tree
  Branch rng _ l r
    | Main.range l `inRange` ix
      -> let l' = update l ix v
         in Branch rng (val l' `mappend` val r) l' r
    | otherwise
      -> let r' = update r ix v
         in Branch rng (val l `mappend` val r') l r'
{-# SPECIALIZE INLINE
    update :: STree Min -> Int -> Min -> STree Min #-}
{-# SPECIALIZE INLINE
    update :: STree Max -> Int -> Max -> STree Max #-}
{-# SPECIALIZE INLINE
    update :: Num a => STree (Sum a)-> Int -> Sum a -> STree (Sum a) #-}
{-# SPECIALIZE INLINE
    update :: Num a => STree (Product a)-> Int -> Product a -> STree (Product a) #-}