


The fastest horse is the one that takes the minimum time to finish the race.
The first line of input contains an integer ‘T’ denoting the number of test cases to run. Then the test case follows.
The first line of each test case contains two space-separated integers ‘N’ and ‘Q’ that denote the number of horses and number of queries respectively.
The second line of each test case contains ‘N’ space-separated integers, denoting the time taken by each horse to complete the race.
The next ‘Q’ lines contain two space-separated integers ‘L’ and ‘R’ denoting the range of the query.
For each test case, return the list of integers, where the ith of them denotes the answer of the ith query.
Output for each test case will be printed in a new line.
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= T <= 50
1 <= N <= 10000
1 <= Q <= 10000
1 <= FINISHTIME[i] < 10 ^ 9
0 <= L <= R < N
Time Limit: 1 sec.
For each query, we can simply loop between ‘L’ and ‘R’ and store the minimum time taken by a horse to complete the race in a variable, and return the value stored in that variable.
Algorithm for Each Query:
A segment Tree is a data structure that allows us to perform range queries efficiently. In a minimum segment tree, at any node, we store the minimum value of all the leaf nodes present in the subtree rooted at that node. Similarly, for maximum segment trees, we store the maximum, for sum segment trees we store the sum, etc.
To learn more about segment trees, wiki is a good source to get started.
The first step in this approach is to build the minimum segment tree using ‘FINISHTIME’. Once we have built a segment tree, for each query calculate the range minimum for the given range i.e. for elements between ‘Lth’ and ‘Rth’ index.
Store the result of each query in an array and return the array.
Algorithm: