C++ set solution
Intuition\n Describe your first thoughts on how to solve this problem. \nThe implemeation is based on the python solution posted at:\nhttps://leetcode.com/probl
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe implemeation is based on the python solution posted at:\nhttps://leetcode.com/problems/count-integers-in-intervals/solutions/2039730/python3-built-in-list-beats-sortedlist\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(log n) for average, O(n) for worst\n\n- - Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(n)\n\n# Code\n```\n#include <set>\n\nusing std::set;\nusing PairSet = set<pair<int, int>>;\nusing PairSetIT = PairSet::iterator;\n\nclass CountIntervals {\npublic:\n    set<pair<int, int>> data;\n    int cover;\n\n    CountIntervals() : cover(0) {\n        data.insert({INT_MIN, INT_MIN});\n        data.insert({INT_MAX, INT_MAX});\n    }\n    \n    // O(log n) for average, O(n) for worst\n    void add(int left, int right) {\n        PairSetIT leftIt = data.lower_bound({left-1, INT_MIN});\n        if(std::prev(leftIt)->second >= left - 1){\n            leftIt = std::prev(leftIt);\n        }\n        int leftVal = std::min(leftIt->first, left);\n\n        PairSetIT rightIt = data.upper_bound({right+1, INT_MAX});\n        int rightVal = std::max(std::prev(rightIt)->second, right);\n\n        int toDel = 0;\n        for(PairSetIT curIt=leftIt; curIt!=rightIt; ++curIt) {\n            toDel += curIt->second - curIt->first + 1;\n        }\n\n        cover += rightVal - leftVal + 1 - toDel;\n        data.erase(leftIt, rightIt);\n        data.insert({leftVal, rightVal});\n    }\n    \n    // O(1)\n    int count() {\n        return cover;\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++']