import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class SegmentTreeRMQ { int st[]; //array to store segment tree // A utility function to get minimum of two numbers int minVal(int x, int y) { return (x < y) ? y : x; } // A utility function to get the middle index from corner // indexes. int getMid(int s, int e) { return s + (e - s) / 2; } int RMQUtil(int ss, int se, int qs, int qe, int index) { // If segment of this node is a part of given range, then // return the min of the segment if (qs <= ss && qe >= se) return st[index]; // If segment of this node is outside the given range if (se < qs || ss > qe) return Integer.MIN_VALUE; // If a part of this segment overlaps with the given range int mid = getMid(ss, se); return minVal(RMQUtil(ss, mid, qs, qe, 2 * index + 1), RMQUtil(mid + 1, se, qs, qe, 2 * index + 2)); } int RMQ(int n, int qs, int qe) { // Check for erroneous input values if (qs < 0 || qe > n - 1 || qs > qe) { System.out.println("Invalid Input"); return -1; } return RMQUtil(0, n - 1, qs, qe, 0); } int constructSTUtil(int arr[], int ss, int se, int si) { // If there is one element in array, store it in current // node of segment tree and return if (ss == se) { st[si] = arr[ss]; return arr[ss]; } // If there are more than one elements, then recur for left and // right subtrees and store the minimum of two values in this node int mid = getMid(ss, se); st[si] = minVal(constructSTUtil(arr, ss, mid, si * 2 + 1), constructSTUtil(arr, mid + 1, se, si * 2 + 2)); return st[si]; } void constructST(int arr[], int n) { // Allocate memory for segment tree //Height of segment tree int x = (int) (Math.ceil(Math.log(n) / Math.log(2))); //Maximum size of segment tree int max_size = 2 * (int) Math.pow(2, x) - 1; st = new int[max_size]; // allocate memory // Fill the allocated memory st constructSTUtil(arr, 0, n - 1, 0); } static int solve(int[] A,int n) { SegmentTreeRMQ tree = new SegmentTreeRMQ(); tree.constructST(A, n); int len = (n * (n+1)) / 2; int[] a = new int[len]; int count = 0; for(int i=0;i