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: Accepted)

{-# LANGUAGE FlexibleContexts #-}

import Control.Applicative
import Control.Monad
import Control.Monad.ST
import qualified Data.Vector.Mutable as VM
import qualified Data.Vector as V
import qualified Data.ByteString.Char8 as BS
import Data.Maybe
import Data.Bits
import Data.Int
import Debug.Trace

main = do
  [n,q] <- map read . words <$> getLine :: IO [Int]
  queries <- replicateM q $ map (fst . fromJust . BS.readInt) . BS.words <$> BS.getLine :: IO [[Int]]


  let inf = 2147483647

  seg <- stToIO $ newSegmentTree n inf :: IO (IOSegmentTree Int)

  forM_ queries $ \[com,x,y] -> do
    when (com == 0) $ do
      stToIO $ updateSegmentTree seg x y min
      --print =<< V.freeze seg
    when (com == 1) $ do
      a <- stToIO $ findSegmentTree seg x (y+1) inf min
      --print =<< V.freeze seg
      print a



type STSegmentTree s a = VM.MVector s a
type IOSegmentTree a = STSegmentTree RealWorld a

newSegmentTree :: Num a => Int -> a ->  ST s (STSegmentTree s a)
newSegmentTree n x = V.thaw $ V.replicate m x
  where m = 2 * (2 ^ (ceiling $ logBase 2 (fromIntegral n))) - 1


updateSegmentTree :: Num a => STSegmentTree s a -> Int -> a -> (a -> a -> a) -> ST s ()
updateSegmentTree tree i x f = do
  let
    n = (VM.length tree + 1) `div` 2
    j = i + n - 1
  VM.write tree j x
  aux ((j-1)`div`2) x
  where
    aux i x = do
      when (i >= 0) $ do
        a <- VM.read tree (i*2+1)
        b <- VM.read tree (i*2+2)
        VM.write tree i (f a b)
        aux ((i-1)`div`2) x
      

findSegmentTree :: Num a => STSegmentTree s a -> Int -> Int -> a -> (a -> a -> a) -> ST s a
findSegmentTree tree x y e f = do
  let n = (VM.length tree) `div` 2 + 1
  aux 0 0 n

  where
    aux i l r
      | r <= x || y <= l = return e
      | x <= l && r <= y = VM.read tree i
      | otherwise = f <$> aux (i*2+1) l ((l+r)`div`2) <*> aux (i*2+2) ((l+r)`div`2) r