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



use std::cmp::min;

struct RMQSegmentTree<T> {
    n: usize,
    dat: Vec<T>,
}

impl RMQSegmentTree<i32> {
    // 要素数nの配列に対し、セグメント木を構築する。
    pub fn new(n: usize) -> RMQSegmentTree<i32> {
        let mut n_ = 1;
        while n_ < n {
            n_ *= 2;
        }
        let n = n_;
        let dat = vec![i32::max_value(); 2 * n - 1];

        RMQSegmentTree {
            n,
            dat
        }
    }

    /// k番目の値をaに変更する
    pub fn update(&mut self, k: usize, a: i32) {
        let mut k = k + self.n - 1;
        self.dat[k] = a;

        while k > 0 {
            // k > 0 => k - 1 >= 0
            // k points index of parent
            k = (k - 1) / 2;

            self.dat[k] = min(self.dat[2 * k + 1].clone(), self.dat[2 * k + 2].clone());
        }
    }

    // 区間[a, b)の最小値を求める
    pub fn query(&self, a: usize, b: usize) -> Option<i32> {
        self._query(a, b, 0, 0, self.n)
    }

    fn _query(&self, a: usize, b: usize, k: usize, l:usize, r: usize) -> Option<i32> {
        // [a, b) と[l, r)が交差しなければ None
        if r <= a || b <= l {
            None
        } // [a, b)が[l, r)を内包するとき 
        else if a <= l && r <= b {
            Some(self.dat[k].clone())
        } else {
            let vl = self._query(a, b, k * 2 + 1, l, (l + r) / 2);
            let vr = self._query(a, b, k * 2 + 2, (l + r) / 2, r);

            match vl {
                Some(min_vl) => {
                    match vr {
                        Some(min_vr) => Some(min(min_vl, min_vr)),
                        None => Some(min_vl),
                    }
                },
                None => {
                    // match vr { Some(min_vr) => Some(min_vr), None => None,}
                    vr
                },
            }
        }
    }
}


fn main() {
    let (n, q) = read_tuple();
    let mut seq = RMQSegmentTree::new(n);

    for _ in 0..q {
        let (com, x, y) = {
            let temp = read_vec();
            (temp[0], temp[1], temp[2])
        };
        if com == 0 {
            seq.update(x, y as i32);
        } else {
            println!("{:?}", seq.query(x, y + 1).or(Some(i32::max_value())).unwrap());
        }
    }
}
fn read_tuple<T: std::str::FromStr + Copy>() -> (T, T) {
    let temp = read_vec();
    (temp[0], temp[1])
}
fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().split_whitespace()
        .map(|e| e.parse().ok().unwrap()).collect()
}