SortedDict
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```python3 []
from sortedcontainers import SortedDict
class CountIntervals:

    def __init__(self):
        self._count = 0
        self.intervals = SortedDict()

    def _remove_overlap(self, new_left, new_right, iterator):
            to_be_deleted = []
            for left in iterator:
                left, right = left, self.intervals[left]
                if max(left, new_left) > min(right, new_right):
                    break
                

                self._count -= (right-left+1)
                new_left, new_right = min(new_left, left), max(new_right, right)
                to_be_deleted.append(left)

            for left in to_be_deleted:
                del self.intervals[left]

            return new_left, new_right

    def add(self, left: int, right: int) -> None:
        forward_iterator = self.intervals.irange(minimum=left, reverse=False)
        left, right = self._remove_overlap(left, right, forward_iterator)

        reverse_iterator = self.intervals.irange(maximum=left, reverse=True)
        left, right = self._remove_overlap(left, right, reverse_iterator)
        
        self.intervals[left] = right
        self._count+=(right-left+1)

    def count(self) -> int:
        return self._count
        


# Your CountIntervals object will be instantiated and called as such:
# obj = CountIntervals()
# obj.add(left,right)
# param_2 = obj.count()
```
['Python3']