Merge Intervals
\nclass CountIntervals {\nprivate:\n    set<pair<int, int>> interval;\n    int ans;\npublic:\n    CountIntervals() {\n        interval = * new set<pair<int, int
```\nclass CountIntervals {\nprivate:\n    set<pair<int, int>> interval;\n    int ans;\npublic:\n    CountIntervals() {\n        interval = * new set<pair<int, int>>();\n        ans = 0;\n    }\n    \n    void add(int left, int right) {\n        if (interval.size() == 0) {\n            interval.insert(make_pair(left, right));\n            ans += right - left + 1;\n        } else {\n            pair<int, int> num = make_pair(left, right);\n            int sub = 0;\n            \n            if (interval.lower_bound(num) == interval.begin()) {\n                while (interval.begin()->second <= right) {\n                    sub += interval.begin()->second - interval.begin()->first + 1;\n                    interval.erase(interval.begin());\n                    \n                    if (interval.empty())\n                        break;\n                }\n                \n                if (!interval.empty() && interval.begin()->first <= right) {\n                    sub += right - interval.begin()->first + 1;\n                    num.second = interval.begin()->second;\n                    interval.erase(interval.begin());\n                }\n            } else {\n                if (prev(interval.lower_bound(num))->second >= left) {\n                    int ll = prev(interval.lower_bound(num))->first;\n                    \n                    if (prev(interval.lower_bound(num))->second >= right) {\n                        sub += right - left + 1;\n                        num.second = prev(interval.lower_bound(num))->second;\n                        interval.erase(prev(interval.lower_bound(num)));\n                    } else {\n                        sub += prev(interval.lower_bound(num))->second - left + 1;\n                        interval.erase(prev(interval.lower_bound(num)));\n                    }\n                    \n                    num.first = ll;\n                }\n                \n                while (interval.lower_bound(num) != interval.end() && right >= interval.lower_bound(num)->first) {\n                    if (right > interval.lower_bound(num)->second)\n                        sub += interval.lower_bound(num)->second - interval.lower_bound(num)->first + 1;\n                    else {\n                        sub += right - interval.lower_bound(num)->first + 1;\n                        num.second = interval.lower_bound(num)->second;\n                    }\n                    \n                    interval.erase(interval.lower_bound(num));\n                }\n            }\n            \n            interval.insert(num);\n            ans += right - left + 1 - sub;\n        }\n    }\n    \n    int count() {\n        return ans;\n    }\n};\n```
['C']