I need a data structure that stores a set of intervals and allows to calculate the length of their union.
Suppose that one has a set of intervals on the line with integer ends between 1
and n
. Initially it is empty, and one can add or remove intervals to it. After each operation one should calculate the length of the union of all intervals in the set.
How can one implement it via the segment tree with O(log n)
time complexity for pushing, removing and finding the length? What should one store in the nodes?