I'm solving a problem and I realized I am need of a data structure with following properties but cant find one even after few hours of googling. I believe STL library is too rich to not have this one hence asking here.
- Insert any element(should be able to contain repetetive ones) in
O(log(n))
time - Remove an element in
O(log(n))
time as well. - If i want to query for the number of elemenes in range [a,b], I
should get that count in
O(log(n))
time..
If I were to write it from scratch, for part 1 and 2, I would use a set
or multiset
and I would modify their find()
method(which runs in O(log(N))
time) to return indices instead of iterators so that I can do
abs(find(a)-find(b))
so I get the count of elements in log(N) time. But unfortunately for me, find()
returns and iterator.
I have looked into multiset()
and I could not accomplish requirement 3 in O(log(n))
time. It takes O(n)
.
Any hints to get it done easily?