2276. Count Integers in Intervals.cpp
Code\n\nclass CountIntervals {\n    map<int, int> intervals; \n    int number = 0; \npublic:\n    CountIntervals() {}\n    \n    void add(int left, int right) {
Code\n```\nclass CountIntervals {\n    map<int, int> intervals; \n    int number = 0; \npublic:\n    CountIntervals() {}\n    \n    void add(int left, int right) {\n        auto it = intervals.upper_bound(left); \n        if (it != intervals.begin() && prev(it)->second >= left) {\n            number -= prev(it)->second - prev(it)->first + 1; \n            left = prev(it)->first; \n            right = max(right, prev(it)->second); \n            intervals.erase(prev(it)); \n        }\n        while (it != intervals.end() && right >= it->first) {\n            number -= it->second - it->first + 1; \n            right = max(right, it->second); \n            intervals.erase(it++); \n        }         \n        intervals.insert({left, right}); \n        number += right - left + 1; \n    }\n    \n    int count() {\n        return number; \n    }\n};\n```
['C']