Home BOJ. Minimum Value (10868)
Post
Cancel

BOJ. Minimum Value (10868)

[Link] https://www.acmicpc.net/problem/10868


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import java.util.*;
import java.io.*;

public class Main {
	static BufferedReader br;
	static StringBuilder sb = new StringBuilder();
	static int[] seg;
	static int n;
	public static void main(String[] args) throws IOException {
		br = new BufferedReader(new InputStreamReader(System.in));
		sb = new StringBuilder();
		int[] line = getArr();
		n = line[0];
		int m = line[1];
		int[] arr = new int[n];

		for(int i = 0; i < n; i++) arr[i] = toi(br.readLine());
		seg = new int[n << 1];
		init(arr);

		while(m-- > 0) {
			line = getArr();
			int a = line[0] - 1, b = line[1];
			sb.append(get(a, b)).append("\n");
		}
		print(sb);
	}

	public static void init(int[] arr) {
		for(int i = 0; i < n; i++) seg[i + n] = arr[i];
		for(int i = n - 1; i > 0; i--) seg[i] = Math.min(seg[i<<1], seg[i<<1|1]);
	}

	public static int get(int l, int r) {
		int ans = Integer.MAX_VALUE;
		for(l += n, r += n; l < r; l >>= 1, r >>= 1) {
			if((l & 1) != 0) ans = Math.min(ans, seg[l++]);
			if((r & 1) != 0) ans = Math.min(ans, seg[--r]);
		}
		return ans;
	}

	static int toi(String s) { return Integer.parseInt(s); }
	static long tol(String s) { return Long.parseLong(s); }
	static String[] getLine() throws IOException { return br.readLine().split(" "); }
	static int[] getArr() throws IOException { return Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); }
	static <T> void print(T s) { System.out.print(s); }
	static <T> void println(T s) { System.out.println(s); }
}
This post is licensed under CC BY 4.0 by the author.