[C++] Map solution with explaination in detail
NOTE\nThis problem is similar to 715. Range Module. You can start that first and will find it easy to implement an additional counting system required in this p
#### NOTE\nThis problem is similar to [715. Range Module](https://leetcode.com/problems/range-module/). You can start that first and will find it easy to implement an additional counting system required in this problem.\n```\nclass CountIntervals {\npublic:\n    // use map to store interval [start, end)\n    map<int, int> mp;\n    int length;\n\t// the following function returns a new interval given one interval\n\t// overlapped ones will be deleted from map and merged with the current one\n    pair<int, int> find(int left, int right) {\n        // find segment.first > left and segment.first > right\n        auto l = mp.upper_bound(left);\n        auto r = mp.upper_bound(right);\n        // move back first segment and check if its end < left\n        // if yes, move forward first segment\n        if (l != mp.begin() && (--l)->second < left) ++l;\n        \n        // if now l == r, it does not overlap with others\n        if (l == r) {\n            // update the total covered length\n            length += (right - left);\n            return {left, right};\n        }\n        \n        // get the min val\n        int minVal = min(l->first, left);\n        // get the max val by moving back the second segment\n        int maxVal = max(right, (--r)->second);\n        \n        // update the total covered length by deleting overlapped ones\n        // r++: [l, r] -> [l, r) to half-open interval\n        auto it = l;\n        r++; \n        while (it != r) {\n            length -= (it->second - it->first);\n            it++;\n        }\n        // update the total covered length by the new interval\n        length += (maxVal - minVal);\n        \n        // remove overlap segment\n        mp.erase(l, r);\n        // return new interval\n        return {minVal, maxVal};\n    }\n    CountIntervals() {\n        length = 0;\n    }\n    \n    void add(int left, int right) {\n        // use half-open interval to simply the implementation\n        right++;\n        auto x = find(left, right);\n        mp[x.first] = x.second;\n    }\n    \n    int count() {\n        return length;\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']