Length of union of ranges

Viewed 994

I need to find length of union of ranges in one dimension coordinate system. I have many ranges of form [a_i,b_i], and I need to find the length of union of these ranges. The ranges can be dynamically added or removed and can be queried at any state for length of union of ranges.

for example: is ranges are:

[0-4]
[3-6]
[8-10]

The output should be 8.

Is there any suitable data structure for the purpose with following upper bounds on complexity:

Insertion - O(log N)
Deletion - O(log N)
Query - O(log N)
3 Answers
Related