Java/Python segment tree
\nclass Node {\n    int lower, upper, val;\n    Node left, right;\n    public Node(int lower, int upper, int val) {\n        this.lower = lower;\n        this.u
```\nclass Node {\n    int lower, upper, val;\n    Node left, right;\n    public Node(int lower, int upper, int val) {\n        this.lower = lower;\n        this.upper = upper;\n        this.val = val;\n    }\n}\nclass CountIntervals {\n    Node root;\n    public CountIntervals() {\n        root = new Node(0, 1000000000, 0);\n    }\n    \n    private void setRange(Node node, int left, int right) {\n        if (left <= node.lower && node.upper <= right) {\n            node.val = node.upper - node.lower + 1;\n            node.left = null;\n            node.right = null;\n            return;\n        } \n        int mid = (node.upper + node.lower) / 2;\n        if (node.left == null && node.right == null) {\n            node.left = new Node(node.lower, mid, node.val > 0 ? mid - node.lower + 1 : 0);\n            node.right = new Node(mid + 1, node.upper, node.val > 0 ? node.upper - (mid + 1) + 1 : 0);\n        }\n        if (left <= mid) \n            setRange(node.left, left, right);\n        if (right > mid) \n            setRange(node.right, left, right);\n        node.val = node.left.val + node.right.val;\n    }\n    \n    public void add(int left, int right) {\n        setRange(root, left, right);\n    }\n    \n    public int count() {\n        return root.val;\n    }\n}\n```\n\n```\nclass Node:\n    def __init__(self, lower, upper, val):\n        self.lower = lower\n        self.upper = upper\n        self.left = None\n        self.right = None\n        self.val = val\n        \nclass CountIntervals:\n\n    def __init__(self):\n        self.root = Node(0, 1000000000, 0)\n\n    def _set_range(self, node, left, right): \n        # print(left, right, node.lower, node.upper)\n        if left <= node.lower and node.upper <= right: \n            node.val = node.upper - node.lower + 1\n            node.left = node.right = None\n            return \n        mid = (node.upper + node.lower) // 2\n        if not node.left and not node.right:\n            node.left = Node(node.lower, mid, mid - node.lower + 1 if node.val else 0)\n            node.right = Node(mid + 1, node.upper, node.upper - (mid + 1) + 1 if node.val else 0)\n        if left <= mid: \n            self._set_range(node.left, left, right)\n        if right > mid: \n            self._set_range(node.right, left, right)\n        node.val = node.left.val + node.right.val\n        \n    def add(self, left: int, right: int) -> None:\n        self._set_range(self.root, left, right)\n\n    def count(self) -> int:\n        return self.root.val\n\n```
[]