I need a data structure that can perform the following three queries in log(n) where n is the range of numbers.
- insert(a,b) : Insert Interval [a,b].
- count(x) : Count number of intervals that contain the point x.
- remove(a,b) : Remove Interval [a,b] from the set of intervals.
I came across the interval trees and its implementation in C++ requires the use of Red Black Trees.
I am trying to avoid making the tree from scratch, is there a way I can implement the c++ stl like <map>
or <set>
to perform these operations ?
EDIT I realised that since all I need is the count of the intervals that contain a point, I used a Binary Indexed Tree, its implementation is simple and performs the above queries in O(log(n)) time.