Straightforward SortedList O(NLogN) solution
Code\npython3 []\nfrom sortedcontainers import SortedList\nclass CountIntervals:\n\n    def __init__(self):\n        self.cur = 0\n        self.intervals = Sort
# Code\n```python3 []\nfrom sortedcontainers import SortedList\nclass CountIntervals:\n\n    def __init__(self):\n        self.cur = 0\n        self.intervals = SortedList()\n\n    def add(self, left: int, right: int) -> None:\n        i = self.intervals.bisect_left((left, right)) \n        to_remove = []\n        l = i - 1\n        while l >= 0 and self.intervals[l][1] >= left - 1:\n            to_remove.append(self.intervals[l])\n            left = min(left, self.intervals[l][0])\n            right = max(right, self.intervals[l][1])\n            l -= 1\n\n        r = i\n        while r < len(self.intervals) and self.intervals[r][0] <= right + 1:\n            to_remove.append(self.intervals[r])\n            right = max(right, self.intervals[r][1])\n            r += 1\n        \n        removed = 0\n        for interval in to_remove:\n            removed += interval[1] - interval[0] + 1\n            self.intervals.remove(interval)\n        self.intervals.add((left, right))\n        self.cur += right - left + 1 - removed\n\n    def count(self) -> int:\n        return self.cur\n        \n\n\n# Your CountIntervals object will be instantiated and called as such:\n# obj = CountIntervals()\n# obj.add(left,right)\n# param_2 = obj.count()\n```
['Python3']