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


import random


class Node:
    __slots__ = ('x', 'y', 'left', 'right', 'valid')

    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.left = None
        self.right = None
        self.valid = True

    def __str__(self):
        if self.valid:
            return "({} [{},{}] {})".format(self.left, self.x,
                                            self.y, self.right)
        else:
            return "({} *[{},{}] {})".format(self.left, self.x,
                                             self.y, self.right)


class KdTree:
    MAX = 2**31-1

    def __init__(self, size):
        self.root = None
        self.index = [None] * size

    def insert(self, x, y):
        self._delete(self.index[x])
        self.root = self._insert(self.root, x, y)

    def _insert(self, node, x, y, level=0):
        if node is None:
            node = Node(x, y)
            self.index[x] = node
            return node
        if level % 2 == 0:
            if node.x > x:
                node.left = self._insert(node.left, x, y, level+1)
            else:
                node.right = self._insert(node.right, x, y, level+1)
        else:
            if node.y > y:
                node.left = self._insert(node.left, x, y, level+1)
            else:
                node.right = self._insert(node.right, x, y, level+1)
        return node

    def miny(self, x1, x2):
        def _miny(node, y, level):
            if node is None:
                return y
            left, right = self.MAX, self.MAX
            if level % 2 == 0:
                if x2 < node.x:
                    left = _miny(node.left, y, level+1)
                elif x1 > node.x:
                    right = _miny(node.right, y, level+1)
                else:
                    if self._is_valid(node) and node.y < y:
                        y = node.y
                    left = _miny(node.left, y, level+1)
                    if left < y:
                        y = left
                    right = _miny(node.right, y, level+1)
            else:
                if node.y > y:
                    left = _miny(node.left, y, level+1)
                elif node.y < y:
                    if x1 <= node.x <= x2 and self._is_valid(node):
                        left = _miny(node.left, node.y, level+1)
                    else:
                        left = _miny(node.left, y, level+1)
                        if left < y:
                            y = left
                        right = _miny(node.right, y, level+1)
                else:
                    left = _miny(node.left, y, level+1)

            if left < y:
                y = left
            if right < y:
                y = right
            return y

        assert x1 <= x2
        return _miny(self.root, self.MAX, 0)

    def _delete(self, node):
        if node is not None:
            node.valid = False

    def _is_valid(self, node):
        if node is not None:
            return node.valid
        else:
            return False


def run():
    n, q = [int(i) for i in input().split()]
    tree = KdTree(n)
    buff = {}
    k = 0

    for _ in range(q):
        com, u, v = [int(i) for i in input().split()]
        if com == 0:
            if u not in buff:
                k += 1
            buff[u] = v
        elif com == 1:
            if k > 0:
                for x, y in random.sample(buff.items(), k=k):
                    tree.insert(x, y)
                buff = {}
                k = 0
            print(tree.miny(u, v))
        else:
            raise ValueError('invalid command: {}'.format(com))


if __name__ == '__main__':
    run()