C++ || Very easy to understand || Clean Code
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Total
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Total time complexity: O(nlogn)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass CountIntervals {\npublic:\n    multiset<pair<int, int>> setIntervals;\n    int numIntegers = 0;\n\n    CountIntervals() {\n        \n    }\n    \n    void add(int left, int right) {\n        setIntervals.insert({left, right});\n        numIntegers += right - left + 1;\n\n        auto it = setIntervals.lower_bound({left, right});\n        while (next(it) != setIntervals.end() && next(it)->first <= right) {\n            it = mergeTwoIntervals(it, next(it));\n        }\n\n        while (it != setIntervals.begin() && prev(it)->second >= left) {\n            it = mergeTwoIntervals(prev(it), it);\n        }\n    }\n\n    multiset<pair<int, int>>::iterator mergeTwoIntervals(multiset<pair<int, int>>::iterator it1, multiset<pair<int, int>>::iterator it2) {\n        int rightRangeDuplicate = min(it1->second, it2->second);\n        int leftRangeDuplicate = max(it1->first, it2->first);\n        int newRightRange = max(it1->second, it2->second);\n        int newLeftRange = min(it1->first, it2->first);\n        setIntervals.erase(it2);\n        setIntervals.erase(it1);\n        setIntervals.insert({newLeftRange, newRightRange});\n        numIntegers -= rightRangeDuplicate - leftRangeDuplicate + 1;\n        return setIntervals.lower_bound({newLeftRange, newRightRange});\n    }\n    \n    int count() {\n        return numIntegers;\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```
['C++']