Split into segments that don't overlap partially
\nfrom sortedcontainers import SortedList\n\nclass CountIntervals:\n    def __init__(self):\n        self.L = 1\n        self.R = 10 ** 9\n        self.parts = 
```\nfrom sortedcontainers import SortedList\n\nclass CountIntervals:\n    def __init__(self):\n        self.L = 1\n        self.R = 10 ** 9\n        self.parts = SortedList()\n        self.res = 0\n        \n    def split(self, x, y, i, j):\n        if y < i or x > j:\n            return []\n        if x >= i and y <= j:\n            return [(x, y)]\n        mid = (x + y) // 2\n        return self.split(x, mid, i, j) + self.split(mid + 1, y, i, j)\n\n    def add(self, left: int, right: int) -> None:\n        for l, r in self.split(self.L, self.R, left, right):\n            x = self.parts.bisect_right((l, float(\'inf\'))) - 1\n            if 0 <= x < len(self.parts) and self.parts[x][1] >= r:\n                continue\n            x = self.parts.bisect_left((l, float(\'-inf\')))\n            while x < len(self.parts) and self.parts[x][1] <= r:\n                rm = self.parts.pop(x)\n                self.res -= rm[1] - rm[0] + 1\n            self.parts.add((l, r))\n            self.res += r - l + 1\n\n    def count(self) -> int:\n        return self.res\n```
['Python']