

L and R in each query follow 0-based indexing.
For each query, do it faster than linear time.
If N = 7, ARR = {1, 2, 3, 4, 3, 2, 1}, Q = 2 and Queries = { {0, 3}, {3, 5} }
For the first query, we need to find the smallest number between the Arr[0], Arr[1], Arr[2] and Arr[3], clearly, the smallest number is Arr[0] = 1, so we will print 1.
For the second query, we need to find the smallest number amongst Arr[3], Arr[4] and Arr[5], clearly, the smallest number is Arr[5] = 2, so we will print 2.
The first line contains a single integer ‘T’ denoting the number of test cases, then each test case follows:
The first line of each test case contains a single integer ‘N’, denoting the number of elements in the array.
The second line of each test case contains N integers ‘ARR’, denoting the array elements.
The third line of each test case contains a single integer ‘Q’, denoting the number of queries.
The next Q lines, each contain two integers ‘L’ and ‘R’, denoting the left end and the right end of the range.
For each test case, print the smallest element in the range [L, R].
Output for each test case will be printed in a separate line.
You are not required to print anything; it has already been taken care of. Just implement the function.
1 ≤ T ≤ 10
1 ≤ N ≤ 1000
0 ≤ Arr[i] ≤ 10^9
1 ≤ Q ≤ 50
0 ≤ L ≤ R < N
Time limit: 1 sec
For each query iterate all the elements in the range [L, R] and return the minimum element.
The steps are as follows :
This is a standard question on the segment tree. In general, many questions involving range query can be solved using a segment tree, segment tree reduces the linear search time into logarithmic time for each query, though there is an initial preprocessing cost that is linear in time. Rather than directly jumping to the implementation of a segment tree, we will give an overview of this data structure.
Suppose you have an array = {1, 2, 1, 3, 4, 2, 5, 3, 6, 5, 4, 8, 6}, and we have to do multiple queries on this array, let one the query’s for the time being be to find the minimum in the range [3, 12].
Refer to the image above, we add some extra elements at the end of the array, such that these elements do not alter our answer, simply choosing elements equal to infinity works here as we need to find the minimum element in a range and an infinite number won’t alter our result. We add these elements to the end so that the tree that we build in future is a full binary tree, hence we need to add elements until the total elements in the array are equal to 2x - 1 (x is any natural number).
We will now start building a segment tree (a full binary tree in our case), we can easily store this tree as an array (recall that we don’t need adjacency list, etc. to store a complete/ full binary tree, an array is just sufficient to do it). Fill all the leaf nodes of this tree with the array elements, now simply fill the rest of the elements of this segment tree, to do this refer to the things in Yellow in the image above, we can easily fill the ith position with the minimum of the values at 2*i’th and 2*i + 1’th positions. This will finally help us, as now we need not have to search each element in a range, we are precomputing things and we can directly use that later, for example: if a query has the range [4, 7] inside it, then we will not need to check for each of the element from Arr[4] to Arr[7], we can simply consider the value Seg[2] as it stores the minimum of that range.
Now let’s discuss how to process a query, for the given array if we have a query to find the minimum from the range [3, 12] for the above array, then instead of finding the minimum of each element in this range, we can simply find the minimum of Seg[3], Seg[5] and Seg[19] (marked with green circles in the image), this is because Seg[3] stores the minimum element for the range [8, 12], Seg[5] stores the minimum element for the range [4, 7] and Seg[19] stores the element equal to Arr[3], this clearly shows that by just considering three elements of our segment tree we are able to find the minimum element of the range [3, 12]. You can easily notice that as the range increases, the number of lookups required will be much less than the linear time complexity of the brute force method.
There is a standard procedure to implement segment trees, to process the query we start from the root node of the tree (1’st node of the segment tree), and then recursively call both the children. We return the recursive call if a segment covered by the current node completely lies in our query range.
The steps are as follows :
Count Even Or Odd
Count Even Or Odd
Count Even Or Odd
Count Even Or Odd
Count Even Or Odd
Count Even Or Odd
Count Even Or Odd
Count Even Or Odd
Ninja and Time
Ninja and Time
Ninja and Time
Ninja and Time
Ninja and Time
Ninja Land
Ninja Land
Ninja Land
Ninja Land
Ninja Land
Ninja Land
Ninja Land
Ninja Land
Ninja Land
Ninja Land
Ninja Land
Ninja and Tree
Distinct Queries on Tree