Python Easy Binary Search
Idea: stored a list of interval points, where the even indices are the start points (included) of the intervals and odd indices are the end points (excluded) of
**Idea**: stored a list of interval points, where the even indices are the start points (included) of the intervals and odd indices are the end points (excluded) of the intervals. For example, \nthe intervals [[2, 3], [7, 10]] is stored as [2, 4, 7, 11]\n\nWhen adding a new interval, I use binary search to find the insertion points (bisect_left for the start points and bisect_right to the end points). I add the new intervals and delete the intervals in between.\n\nHowever, I need to handle the case when the indicies of the insertion points are odd, which means the points are within existing intervals. For example, adding [5, 9) to [2, 4, 7, 11], the insertion point of 5 is 2, so it is fine, while the insertion point of 9 is 3, so I need to move it to the right (11). \nAnother example, adding [3, 5) to [2, 4, 7, 11], the insertion points are 1 and 2, so I moved the insertion points of 3 to the left (2).\n\nWhen adding and deleting the intervals, I also store the value of the total interval count.\n\n**Python**\n\n```\nclass CountIntervals:\n\n    def __init__(self):\n        self.intervals = []\n        self.c = 0\n\n    def add(self, left: int, right: int) -> None:\n        right += 1\n        i = bisect_left(self.intervals, left)\n        j = bisect_right(self.intervals, right)\n\n        if i % 2 == 1:\n            left = self.intervals[i-1]\n            i -= 1\n            \n        if j % 2 == 1:\n            right = self.intervals[j]\n            j += 1\n            \n        for k in range(i, j, 2):\n             self.c -= self.intervals[k+1] - self.intervals[k]        \n        \n        self.c += right - left\n        self.intervals[i:j] = [left, right]\n        \n    def count(self) -> int:\n        return self.c\n```\n\n
[]