Java Sol
\nclass CountIntervals {\n    \n    TreeMap<Integer, Integer> intervals;\n    int sz = 0;\n        \n    public CountIntervals() {\n        intervals = new Tree
```\nclass CountIntervals {\n    \n    TreeMap<Integer, Integer> intervals;\n    int sz = 0;\n        \n    public CountIntervals() {\n        intervals = new TreeMap();\n        sz = 0;\n    }\n    \n    public void add(int left, int right) {\n        \n        int s = left, e = right; \n        Integer s1 = intervals.floorKey(left);\n        \n        if (s1 != null) {\n            int e1 = intervals.get(s1);\n            \n            if (e1 >= e) return;\n            if (e1 >= s) {\n                sz -= (e1 - s1 + 1);\n                s = Math.min(s1, s);\n                intervals.remove(s1);\n            }\n        }\n        \n        while (intervals.floorKey(e) !=null && intervals.floorKey(e) >= s) {\n            \n            int s2 = intervals.floorKey(e);\n            int e2 = intervals.get(s2);\n            sz -= (e2 - s2 + 1);\n            \n            e = Math.max(e2, e);\n            intervals.remove(s2);\n        }\n        \n        sz += (e - s + 1);\n        intervals.put(s, e);\n    }\n    \n    public int count() {\n        return sz;\n    }\n}\n\n/**\n * Your CountIntervals object will be instantiated and called as such:\n * CountIntervals obj = new CountIntervals();\n * obj.add(left,right);\n * int param_2 = obj.count();\n */\n```
[]