So here is a problem, I am given an integer array, whose number is all distinct, let's say it is
int[] data = {21, 34, 12, 88, 54, 73};
now that I would like to see if a subarray, or a range, contains a number is in a range(which is also given). In other words, I want to see if a range of the array contains a number that is in a range. For instance, if I have a function check(int a, int b, int l, int r) where a and b is the range of the array and l and r is the range of the number.
So for the array above, check(0, 2, 20, 50) should return true since from index = 0 to 2, there is 21, 34, 12 and there is two numbers,21, 34, is in range of 20 to 50.
So another example would be check(2, 3, 20, 80) should return false since there,12, 88, is no number in range of 20, 80.
I'm thinking about using Segment Tree, since as I know, RMQ(range minimum query) can be solved by using Segment Tree, thus I think Segment Tree would also work on this problem; however, all of the "get" function of Segment Tree is "single"(Perhaps not the best word), so, I would want to know what nodes should the Segment Tree hold. Is there any algorithm that can answer each query in O(log(n)) while the "build" time is not O(n^2), where n is the size of the array?
Note: Using Segment Tree is just my own thought, any other approach is appreciated.