O(LogN) Solution in Python with step by step detailed explanation
Intuition\n Describe your first thoughts on how to solve this problem. \n1. Keep track of all intervals and counts of integers\n2. In Add() Find the left index 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n1. Keep track of all intervals and counts of integers\n2. In Add() Find the left index and right index to insert/merge/update the intervals. Update the count of integers during Add(). This is O(logN)\n3. In Count(), just return the calculated number, it\'s O(1). \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(log(N))$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```python3 []\nclass CountIntervals:\n\n    def __init__(self):\n        self.intervals = []\n        self.totalCount = 0\n\n    def add(self, left: int, right: int) -> None:\n        # If exists, intervals[left_idx - 1] is smaller than left\n        left_idx = bisect_left(self.intervals, left, key=itemgetter(1))\n        # If exists, intervals[right_idx] is larger than right\n        right_idx = bisect_right(self.intervals, right, key=itemgetter(0))\n\n        # Update left side of the new interval if it\'s merging with intervals[left_idx]\n        if left_idx < len(self.intervals):\n            left = min(left, self.intervals[left_idx][0])\n        # Update right side of the new interval if it\'s merging with intervals[right_idx - 1]\n        if right_idx > 0:\n            right = max(right, self.intervals[right_idx - 1][1])\n        \n        # Update the count\n        for i in range(left_idx, right_idx):\n            self.totalCount -= self.intervals[i][1] - self.intervals[i][0] + 1\n        \n        # Update with new interval\n        self.intervals[left_idx:right_idx] = [[left, right]]\n        self.totalCount += right - left + 1\n\n    def count(self) -> int:\n        return self.totalCount\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']