Problem:
RMQ and RUQ

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

update(s, t, x): change as, as+1, ..., at to x.

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

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

Input

n q
query1
query2
:
queryq

In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format:

0 s t x

or

1 s t

The first digit represents the type of the query. '0' denotes update(s, t, x) and '1' denotes find(s, t).

Output

For each find operation, print the minimum value.

Constraints

1 ≤ n ≤ 100000

1 ≤ q ≤ 100000

0 ≤ s ≤ t < n

0 ≤ x < 231−1

Sample Input 1

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

Sample Output 1

1
2

Sample Input 2

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

Sample Output 2

2147483647
5

Solution in Haskell (judge verdict: Time Limit Exceeded)

{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE MultiWayIf #-}

import Control.Monad
import Control.Monad.ST
import Control.Monad.State
import Control.Applicative
import Control.Arrow
import Control.Monad.Primitive
import Data.List
import Data.Array
import Data.Array.IO
import Data.Maybe
import Data.Bits
import Data.Ord
import Data.Ratio
import Data.Int
import Data.Char
import qualified Data.ByteString.Char8 as BS
import qualified Data.Set as Set
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import qualified Data.Vector.Generic as VG
import qualified Data.Vector.Generic.Mutable as VGM
import Data.Tuple
import Data.Function
import Text.Printf
import Debug.Trace
import Numeric

modifyArray arr i f = readArray arr i >>= \x -> writeArray arr i (f x)

listToTuple2 [a,b] = (a,b)
listToTuple3 [a,b,c] = (a,b,c)
listToTuple4 [a,b,c,d] = (a,b,c,d)
listToTuple5 [a,b,c,d,e] = (a,b,c,d,e)

readIntBS = fst . fromJust . BS.readInt

for = flip map

infixr 1 ?
(?) :: Bool -> (a,a) -> a
(?) b t = (if b then fst else snd) t

main :: IO ()
main = do
  [n,q] <- map read . words <$> getLine :: IO [Int]
  queries <- replicateM q $ map readIntBS . BS.words <$> BS.getLine :: IO [[Int]]
  let
    inf = 2^31-1
    f x y = if x == inf then y else x
    test x = if x == inf then "INF" else show x
    
  seg <- newRangeSegmentTree n inf min inf f const :: IO (IOURangeSegmentTree Int)

  forM_ queries $ \query -> do
    case query of
      [0,s,t,x] -> modifyRangeSegmentTree seg s (t+1) (const x)
      [1,s,t] -> print =<< findRangeSegmentTree seg s (t+1)

    --mapM_ print =<< map (map (\(a,b) -> (test a, test b))) <$> showRangeSegmentTree seg
    --print ""
  

{-- 範囲更新・範囲取得のセグメント木--}
type MRangeSegmentTree v s a = ((v s a, a, (a -> a -> a)), (v s a, a, (a -> a -> a)), (a -> Int -> a))
-- 範囲取得用の(セグメント木,単位元,二項演算)、範囲更新用の(遅延セグメント木,単位元,二項演算)、
type STRangeSegmentTree s a = MRangeSegmentTree VM.MVector s a
type IORangeSegmentTree a = STRangeSegmentTree RealWorld a
type STURangeSegmentTree s a = MRangeSegmentTree VUM.MVector s a
type IOURangeSegmentTree a = STURangeSegmentTree RealWorld a


newRangeSegmentTree :: (PrimMonad m, VGM.MVector v a) => Int -> a -> (a -> a -> a) -> a -> (a -> a -> a) -> (a -> Int -> a) -> m (MRangeSegmentTree v (PrimState m) a)
newRangeSegmentTree n e1 f1 e2 f2 h = do
  tree1 <- VGM.replicate m e1
  tree2 <- VGM.replicate m e2
  return ((tree1,e1,f1),(tree2,e2,f2), h)
  where m = 2 * (2 ^ (ceiling $ logBase 2 (fromIntegral n))) - 1

modifyRangeSegmentTree :: (Eq a, PrimMonad m, VGM.MVector v a) => MRangeSegmentTree v (PrimState m) a -> Int -> Int -> (a -> a) -> m ()
modifyRangeSegmentTree ((tree1,e1,f1),(tree2,e2,f2),h) x y g = do
  aux 0 0 n >> return ()
  where
    n = (VGM.length tree1) `div` 2 + 1
    aux i l r = do
      propag i (r-l)
      if
        | r <= x || y <= l -> VGM.read tree1 i
        | x <= l && r <= y -> VGM.read tree2 i >>= \a -> VGM.write tree2 i (g a) >> return (g a)
        | otherwise -> aux (i*2+1) l ((l+r)`div`2) >>= \a -> aux (i*2+2) ((l+r)`div`2) r >>= \b -> VGM.write tree1 i (f1 a b) >> return (f1 a b)
    propag i len = do
      a <- VGM.read tree2 i
      when (a /= e2) $ do
        when (i<n-1) $ do
          VGM.read tree2 (i*2+1) >>= \b -> VGM.write tree2 (i*2+1) (f2 a b)
          VGM.read tree2 (i*2+2) >>= \b -> VGM.write tree2 (i*2+2) (f2 a b)
        VGM.read tree1 i >>= \a -> VGM.read tree2 i >>= \b -> VGM.write tree1 i (f2 (h b len) a)
        VGM.write tree2 i e2

findRangeSegmentTree :: (Eq a, PrimMonad m, VGM.MVector v a) => MRangeSegmentTree v (PrimState m) a -> Int -> Int -> m a
findRangeSegmentTree ((tree1,e1,f1),(tree2,e2,f2),h) x y = do
  aux 0 0 n
  where
    n = (VGM.length tree1) `div` 2 + 1
    aux i l r = do
      propag i (r-l)
      if
        | r <= x || y <= l -> return e1
        | x <= l && r <= y -> VGM.read tree1 i
        | otherwise -> aux (i*2+1) l ((l+r)`div`2) >>= \a -> aux (i*2+2) ((l+r)`div`2) r >>= \b -> return (f1 a b)
    propag i len = do
      a <- VGM.read tree2 i
      when (a /= e2) $ do
        when (i<n-1) $ do
          VGM.read tree2 (i*2+1) >>= \b -> VGM.write tree2 (i*2+1) (f2 a b)
          VGM.read tree2 (i*2+2) >>= \b -> VGM.write tree2 (i*2+2) (f2 a b)
        VGM.read tree1 i >>= \a -> VGM.read tree2 i >>= \b -> VGM.write tree1 i (f2 (h b len) a)
        VGM.write tree2 i e2


showRangeSegmentTree :: (PrimMonad m, VGM.MVector v a, Show a) => MRangeSegmentTree v (PrimState m) a -> m [[(a,a)]]
showRangeSegmentTree ((tree1,_,_),(tree2,_,_),_) = do
  let
    n = (VGM.length tree1) `div` 2 + 1
    xs = init $ scanl (+) 0 (takeWhile (<=n) (iterate (*2) 1))
    ys = reverse xs

  forM (zip xs ys) $ \(s,s') -> do
    forM [s..s*2] $ \i -> VGM.read tree1 i >>= \a -> VGM.read tree2 i >>= \b -> return (a,b)