|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class Main { |
| 6 | + |
| 7 | + public static void main(String[] args) throws Exception { |
| 8 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 9 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 10 | + StringBuilder sb = new StringBuilder(); |
| 11 | + |
| 12 | + int n = Integer.parseInt(st.nextToken()); |
| 13 | + int m = Integer.parseInt(st.nextToken()); |
| 14 | + int[] arr = new int[n]; |
| 15 | + for(int i=0;i<n;i++){ |
| 16 | + arr[i]= Integer.parseInt(br.readLine()); |
| 17 | + } |
| 18 | + |
| 19 | + int[] min_tree = new int[4*n]; |
| 20 | + int[] max_tree = new int[4*n]; |
| 21 | + |
| 22 | + init(arr,min_tree,1,0,n-1); |
| 23 | + initmax(arr,max_tree,1,0,n-1); |
| 24 | + |
| 25 | + for(int i=0;i<m;i++){ |
| 26 | + st = new StringTokenizer(br.readLine()); |
| 27 | + int a = Integer.parseInt(st.nextToken()); |
| 28 | + int b = Integer.parseInt(st.nextToken()); |
| 29 | + |
| 30 | + int min = query(min_tree,1,0,n-1,a-1,b-1); |
| 31 | + int max = querymax(max_tree,1,0,n-1,a-1,b-1); |
| 32 | + sb.append(min+" "+max+"\n"); |
| 33 | + } |
| 34 | + |
| 35 | + System.out.println(sb.toString()); |
| 36 | + } |
| 37 | + |
| 38 | + static void init(int[] arr, int[] tree, int node, int start, int end) { |
| 39 | + if (start == end) { |
| 40 | + tree[node] = arr[start]; |
| 41 | + } else { |
| 42 | + init(arr, tree, node * 2, start, (start + end) / 2); |
| 43 | + init(arr, tree, node * 2 + 1, (start + end) / 2 + 1, end); |
| 44 | + tree[node] = Math.min(tree[node * 2], tree[node * 2 + 1]); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + static int query(int[] tree, int node, int start, int end, int left, int right) { |
| 49 | + if (left > end || right < start) { |
| 50 | + return -1; |
| 51 | + } |
| 52 | + if (left <= start && end <= right) { |
| 53 | + return tree[node]; |
| 54 | + } |
| 55 | + int lmin = query(tree, node*2, start, (start+end)/2, left, right); |
| 56 | + int rmin = query(tree, node*2+1, (start+end)/2+1, end, left, right); |
| 57 | + if (lmin == -1) { |
| 58 | + return rmin; |
| 59 | + } else if (rmin == -1) { |
| 60 | + return lmin; |
| 61 | + } else { |
| 62 | + return Math.min(lmin, rmin); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + static void initmax(int[] arr, int[] tree, int node, int start, int end) { |
| 67 | + if (start == end) { |
| 68 | + tree[node] = arr[start]; |
| 69 | + } else { |
| 70 | + initmax(arr, tree, node * 2, start, (start + end) / 2); |
| 71 | + initmax(arr, tree, node * 2 + 1, (start + end) / 2 + 1, end); |
| 72 | + tree[node] = Math.max(tree[node * 2], tree[node * 2 + 1]); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + static int querymax(int[] tree, int node, int start, int end, int left, int right) { |
| 77 | + if (left > end || right < start) { |
| 78 | + return -1; |
| 79 | + } |
| 80 | + if (left <= start && end <= right) { |
| 81 | + return tree[node]; |
| 82 | + } |
| 83 | + int lmax = querymax(tree, node*2, start, (start+end)/2, left, right); |
| 84 | + int rmax = querymax(tree, node*2+1, (start+end)/2+1, end, left, right); |
| 85 | + if (lmax == -1) { |
| 86 | + return rmax; |
| 87 | + } else if (rmax == -1) { |
| 88 | + return lmax; |
| 89 | + } else { |
| 90 | + return Math.max(lmax, rmax); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +``` |
0 commit comments