Simple & Easy Solution | with Comments
\nclass CountIntervals {\n    \n    //for storing intervals\n    TreeMap<Integer,Integer> map=new TreeMap<>();\n    int count=0;\n    public CountIntervals() {\
```\nclass CountIntervals {\n    \n    //for storing intervals\n    TreeMap<Integer,Integer> map=new TreeMap<>();\n    int count=0;\n    public CountIntervals() {\n    }\n    \n    public void add(int left, int right) {\n        //we are taking left side as reference and calculating ans\n        \n        //this is the current length\n        int add=right-left+1;\n        \n        //var is the default data type\n        //lowerentry gives the value of smaller(smaller largest) entry compared to (left+1)\n        //we added +1 bcz there may be a case where left is present in a map\n        //lowerentry gives the  value(that is)<left+1\n        var left_bound = map.lowerEntry(left + 1);\n        \n        \n        //if there is some value present then go\n        //getvalue gives the right positon\n        //if current left is cover under map.right value means there is a overlap\n        if(left_bound!=null && left_bound.getValue()>=left){\n            //positions are map.left <= left <= min(map.right,right) <= max(map.right,right)\n            \n//examples:   (1)                       (2)                     (3)\n//       ____________                 _________            _______________     (map)\n//                ___________                  ________           ____         (our)\n            \n            //now, left become the smallest value which in this case is map.left\n            left=left_bound.getKey();\n            \n            //same as right become the maximum value\n            right=Math.max(left_bound.getValue(),right);\n            \n            //value overlap means curr answer(add) also change\n            //we already calculated the map value (covered area) and added into ans variable\n            //calculate/add the new uncovered value\n            //    right            - map.right\n            //max(right,map.right) - map.right\n            //we are not adding +1 bcz that is already handled by overlaping area\n            add=right-left_bound.getValue();\n            \n            //this is taken so we have to remove\n            //at last we added the largest area (which is stored int left,right)\n            map.remove(left_bound.getKey());\n        }\n        \n        //check is right overlaping\n        //if yes then take the correct values\n        //higherEntry gives the largest(largest smallest) value in map\n        //we are not taking left+1 bcz that condition is already been satisfied\n        //eg. left=5 map contains (5,value)\n        //condition is checked in left_bound\n        //i.getKey()<=right means curr is beneath our map.left and covering area(overlaping)\n        for(var i=map.higherEntry(left); i!=null && i.getKey()<=right; i=map.higherEntry(left)){\n            //left <= map.left <= min(right,map.right) <= max(right,map.right)\n            \n            \n//examples:   (1)                       (2)                         (3)\n//            ____________                _________              _______             (map)\n//     ___________                ________                 _________________         (our)\n            \n            \n            //now we have our add which is current ans but some area is overlaping\n            //so we have to subtract overlaping area\n            //+1 bcz [2,5] we have 4 position (2,3,4,5) ; 5-2=>3 ; we ned to add 1\n            add-=Math.min(right,i.getValue())-i.getKey()+1;\n            \n            //right value become the largest among them\n            right=Math.max(right,i.getValue());\n            \n            //we have taken care this entry and calculated left and right now we don\'t need\n            map.remove(i.getKey());\n        }\n        \n        //we pushed the values\n        //this entry can be essential for next calls\n        map.put(left,right);\n        \n        //add the current ans\n        count+=add;\n        \n        \n    }\n    \n    public int count() {\n        return count;\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```\n\nCredits: @votrubac
['Tree', 'Java']