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 OCaml (judge verdict: Accepted)

open Printf
open Scanf

module type Monoid = sig
    type t
    val append : t -> t -> t
    val default : t
end

module Segtree(M : Monoid) = struct
    type elt = M.t
    type t = Leaf of elt | Node of t * elt * t * int

    let value_of = function
        | Leaf x -> x
        | Node(_, x, _, _) -> x

    let size = function
        | Leaf _ -> 1
        | Node(_, _, _, n) -> n

    let rec range_query a b = function
        | Leaf x -> if a <= 0 && 1 <= b then x else M.default
        | Node(lch, x, rch, n) ->
                if a <= 0 && n <= b then x
                else if b <= 0 || n <= a then M.default
                else
                    let l_size = size lch in
                    M.append (range_query a b lch) (range_query (a - l_size) (b - l_size) rch)

    let rec updated k x = function
        | Leaf _ -> Leaf x
        | Node(lch, _, rch, n) ->
                if k < size lch then
                    let new_lch = updated k x lch in
                    Node(new_lch, M.append (value_of new_lch) (value_of rch), rch, n)
                else
                    let new_rch = updated (k - size lch) x rch in
                    Node(lch, M.append (value_of lch) (value_of new_rch), new_rch, n)

    let rec build n =
        if n <= 1 then Leaf M.default
        else Node(build (n / 2), M.default, build (n - n / 2), n)
end

module RMQ = Segtree(
    struct
        type t = int
        let append = min
        let default = int_of_float (2. ** 31.) - 1
    end)

let _ = scanf "%d %d\n" (fun n q ->
    let rec loop tree = function
        | 0 -> ()
        | i -> scanf "%d %d %d\n" (fun com x y ->
                if com = 0 then loop (RMQ.updated x y tree) (i - 1)
                else begin
                    printf "%d\n" (RMQ.range_query x (y + 1) tree);
                    loop tree (i - 1)
                end) in
    loop (RMQ.build n) q)