I have an array of k integer intervals in [1,n], where k ≤ 105 and n ≤ 105. For example, if k is 3 and n is 5, the list might be: {[1,4], [3,5], [2,3]}.
For arbitrary sub-arrays of this array of intervals, I want to be able to count how many intervals contain a given value. For example, in the above example array, I might take the sub-array from index 1 to index 2 (namely {[1,4], [3,5]}) and count the number of intervals in that sub-array contain the value 2 (just 1: [1,4]). This query would be expressed as the 3-tuple (min_index,max_index,value), in this case (1,2,2).
My idea is to build a segment tree from interval array where each node contains sorted intervals (sorted by their start index) lying on that node. Ex: Root will contain all the intervals sorted in ascending order of start index, its left child will contain sorted intervals of first half of interval array and right child will contain sorted intervals of second half and so on.
So in order to solve a query (min_index,max_index,value), I can just traverse the tree and whenever I am in a node which is completely inside [min_index,max_index] I can do a binary search over intervals and count the occurrences. In the worst case each query will take O(log^2n)
operations.
Can we do better than it? Is there any way to process the data first and then answer each query in constant time?
l<=index<=r
, do you mean that the index of the interval in the interval array is bounded? Or that its endpoints are bounded somehow? Isindex
the same asidx
? – nneonneoN
are irrelevant? How many queries are you handling? – nneonneoLth
toRth
interval. – Rana