What I have as input is a Range[] arr
of size n
where Range
is
class Range{
int upper, lower;
Range(int upper,int lower){
this.upper=upper;
this.lower=lower;
}
}
Now, for q
queries, I get a Range r
as input and I have to report the number of values in arr
from index L
to R
(which is also given as input for each query),where a Range
in arr
is considered valid only if it's upper value is greater than r.upper
and lower is less than r.lower
.
I was thinking of using a merge sort tree, but I cannot figure out how to integrate both the conditions. Please suggest some data structure for which construction complexity as well as query complexity is O(n.log n)
and O(q.log n)
respectively.
Also, it should be noted that only the ranges within a particular segment is considered, which is also given as input.
PS: Some modification on merge sort tree or segment tree seems probable, so any answer with merge sort tree or segment tree is welcome.
EDIT : R[] = { {3,7} , {5,9} , {2,10} , {4,10} , {3,5} }
query #1 : {4,6}, l = 2 , r = 5
here, the ranges considered are {5,9} , {2,10} , {4,10} ,{3,5}
Out of these, only {5,9} , {2,10} , {4,10} are valid. So answer for this query is 3.