Solution using list and binary search
Intuition\n Describe your first thoughts on how to solve this problem. \nThis problem reminds me of the merging interval problem where given a sorted list of in
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis problem reminds me of the merging interval problem where given a sorted list of intervals we need to insert a new interval. \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe initialize the object with intervals to keep a track of intervals added using add method and a counter that keeps track of the count of the unique numbers.\nIn the add method we find the location for inserting the new interval (ie the first location where the start of new interval is greater than end of the interval in the list) using binary search. Then we remove all the overlapping intervals and their counts. In the end we add the new count and add the new interval to the list at the insertion location.\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass CountIntervals:\n\n    def __init__(self):\n        self.intervals = []\n        self.counter = 0\n\n    def add(self, left: int, right: int) -> None:\n        intersection = self.binary_search(left)\n        temp = [left, right]\n        i = intersection\n        while i < len(self.intervals) and self.is_overlap(temp, self.intervals[i]):\n            temp[0] = min(temp[0], self.intervals[i][0])\n            temp[1] = max(temp[1], self.intervals[i][1])\n            self.counter -= self.intervals[i][1] - self.intervals[i][0] + 1\n            i += 1\n        \n        self.counter += temp[1] - temp[0] + 1\n        self.intervals[intersection:i] = [temp]\n\n    def count(self) -> int:\n        return self.counter\n        \n    def is_overlap(self, int_1, int_2):\n        return (int_1[0] <= int_2[0] and int_2[0] <= int_1[1]) or (int_2[0] <= int_1[0] and int_1[0] <= int_2[1])\n\n    def binary_search(self, left):\n        start = 0\n        end = len(self.intervals)-1\n\n        while start <= end:\n            mid = start + (end-start)//2\n            if self.intervals[mid][1] < left:\n                start = mid + 1\n            else:\n                end = mid - 1\n        return start\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']