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 JAVA (judge verdict: Wrong Answer)



import java.util.ArrayList;
import java.util.Scanner;

public class Main {
	private static final int INF = Integer.MAX_VALUE;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n = 0;
		int op_num = 0;
		n = sc.nextInt();
		op_num = sc.nextInt();
		int[] arr = new int[n];
		String[] op = new String[op_num+1];
		for(int i= 0;i<op.length;i++)
			op[i] = sc.nextLine();
		
		
		//get data
		
		int[][] data = new int[op_num][2];
		int num=0;
		for(int i= 1, n1= 0; i<= op_num;i++,n1++) {
			if(data[n1][0] == 0) {
				String[] Split= op[i].split(" ");
				data[n1] = new int[Split.length];
				for(int j = 0;j<Split.length;j++)
					data[n1][j] = Integer.parseInt(Split[j]);
			}else {
				String[] Split = op[i].split(" ");
				data[n1] = new int[Split.length];
				for(int j = 0; j< Split.length;j++)
				{
					data[n1][j] = Integer.parseInt(Split[j]);	
				}
			}
		}
		
		for(int i=0;i<data.length;i++) {
			if(data[i][0]==0) {
				if(num <n )
					arr[num++] = data[i][2];
			}
		}
		
		
		
		//operate data
		MinSegmentTree seg = new MinSegmentTree(arr);
		
		for(int i= 0 ;i < data.length;i++ ) {
			if(data[i][0] == 0) {
					seg.update(data[i][1], data[i][2]);
			}
			else if(data[i][0] == 1){
					int result = seg.minimum(data[i][1], data[i][2]);
					System.out.println(result);
			}
		}
		
		
	
	
	}
}

class MinSegmentTree{
	public ArrayList<Integer> minSegmentTree;
	private int n;
	
	public  MinSegmentTree(int[] arr) {
		n = arr.length;
		minSegmentTree = new ArrayList<>(2*n);
		
		for(int i=0; i<n;i++) {
			minSegmentTree.add(0);
		}
		for(int i=0;i<n;i++) {
			minSegmentTree.add(arr[i]);
		}
		for(int i=n-1;i>0;i--) {
			minSegmentTree.set(i, Math.min(minSegmentTree.get(2*i), minSegmentTree.get(2*i +1)));
		}
	}
	
	public void update(int i,int value) {
		i+=n;
		minSegmentTree.set(i, value);
		while(i>1) {
			i /= 2;
			minSegmentTree.set(i, Math.min(minSegmentTree.get(2*i),minSegmentTree.get(2*i+1)));
		}
	}
	
	public int minimum(int left,int right) {
		left+=n;
		right+=n;
		int min = Integer.MAX_VALUE;
		
		while(left<right) {
			if((left&1) == 1) {
				min = Math.min(min,minSegmentTree.get(left) );
				left++;
			}
			if((right&1)==1) {
				right--;
				min = Math.min(min, minSegmentTree.get(right));
			}
			
			left >>= 1;
			right >>= 1;
		}
		return min;
	}
}