Clean Java
For here, we need to consider 3 cases.\nwe use \'us\' as current interval will be insert.\n1. most close left interval which has overlap with us.\n2. most close
For here, we need to consider 3 cases.\nwe use \'us\' as current interval will be insert.\n1. most close left interval which has overlap with us.\n2. most close right interval which has overlap with us (included in case3)\n3. clean up all the intervals within us range\n\nwe can guarantee step3 does out bound of our range due to we do it everytime so there would only be 1 left close, 1 right close to merge. And all the interval in the middle can be safely pruned.\n```\nclass CountIntervals {\n    TreeMap<Integer, Integer> map = new TreeMap<>();\n    int sum = 0;\n    public void add(int left, int right) {\n        Integer leftIntervalStart = map.floorKey(left);\n        Integer rightIntervalStart = map.ceilingKey(left);\n        if (leftIntervalStart != null) {\n            int leftIntervalEnd = map.get(leftIntervalStart);\n            if (leftIntervalEnd >= left) {\n                int preLen = leftIntervalEnd - leftIntervalStart + 1;\n                sum = sum - preLen;\n                map.remove(leftIntervalStart);\n                left = Math.min(leftIntervalStart, left);\n                right = Math.max(right, leftIntervalEnd);\n            }\n        }\n        while (map.ceilingKey(left) != null && map.ceilingKey(left) <= right) {\n            int nextIntervalStart = map.ceilingKey(left);\n            int nextIntervalEnd = map.get(nextIntervalStart);\n            right = Math.max(nextIntervalEnd, right);\n            left = Math.min(nextIntervalStart, left);\n            sum = sum - (nextIntervalEnd - nextIntervalStart + 1);\n            map.remove(nextIntervalStart);\n        }\n        map.put(left, right);\n        sum += (right - left + 1);\n    }\n    public int count() {\n        return sum;\n    }\n}\n```
['Tree', 'Java']