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: Memory Limit Exceeded)

import Control.Applicative
import Control.Monad.State
data SegTree a = Leaf a
               | Elem Int a
               | Tree Int a (SegTree a) (SegTree a)
               deriving (Show)

main = do
    [n,q] <- map read.words<$>getLine
    let init = Elem n (2^31-1)
    evalStateT (query q) init

size (Leaf a) = 1
size (Elem n _) = n
size (Tree n _ _ _) = n

value (Leaf v) = v
value (Elem _ v) = v
value (Tree _ v _ _) = v

left (Elem n v)
  | n == 2 = Leaf v
  | n == 3 = Leaf v
  | otherwise = let n' = n `div` 2 in Elem n' v
left (Tree _ _ l _) = l
right (Elem n v)
  | n == 2 = Leaf v
  | otherwise = let n' = n `div` 2 in Elem (n-n') v
right (Tree _ _ _ r) = r

fromList xs = fl n xs
    where n = length xs

fl n xs = Tree n' m leftT rightT
    where n' = n `div` 2
          (xsL, xsR) = splitAt n' xs
          leftT = fl n' xsL
          rightT = fl n' xsR
          m = min (value leftT) (value rightT)

updateT s t x (Leaf v) = (Leaf x)
updateT s t x tr
  | n <= s = let lt = left tr
                 rt = updateT (s-n) (t-n) x (right tr)
                 m = min (value lt) (value rt)
              in Tree (size tr) m lt rt
  | n > t  = let lt = updateT s t x (left tr)
                 rt = right tr
                 m  = min (value lt) (value rt)
              in Tree (size tr) m lt rt
  | t-s+1 == size tr = Elem (size tr) x
  | otherwise = let lt = updateT s n x (left tr)
                    rt = updateT 0 (t-n) x (right tr)
                    m  = min (value lt) (value rt)
                 in Tree (size tr) m lt rt
  where n = size tr `div` 2

findT :: Ord a => Int -> Int -> SegTree a -> a
findT s t tr
  | n <= s = findT (s-n) (t-n) (right tr)
  | n > t  = findT s t (left tr)
  | t-s+1 == size tr = value tr
  | otherwise = min (findT s n (left tr)) (findT 0 (t-n) (right tr))
  where n = size tr `div` 2


query :: Int -> StateT (SegTree Int) IO ()
query 0 = return ()
query n = getQuery >> query (n-1)

getQuery :: StateT (SegTree Int) IO ()
getQuery = do
    (x:xs) <- lift $ map read.words<$>getLine
    case x of
      0 -> let [s,t,x] = xs in modify (updateT s t x)
      1 -> let [s,t] = xs in get >>= \tr -> lift $ print $ findT s t tr
      3 -> get >>= \tr -> lift $ print tr