C++ | Shortest solution - set with custom comparer
\n```\nclass CountIntervals {\npublic:   \n    void add(int left, int right) {\n        const auto [lo, hi] = _s.equal_range(std::make_pair(left, ++right));\n  
\n```\nclass CountIntervals {\npublic:   \n    void add(int left, int right) {\n        const auto [lo, hi] = _s.equal_range(std::make_pair(left, ++right));\n        if (lo != hi) {\n            for (auto it = lo; it != hi; ++it)\n                _count -= it->second - it->first;\n            left = std::min(left, lo->first);\n            right = std::max(right, std::prev(hi)->second);\n            _s.erase(lo, hi);\n        }\n        _s.emplace(left, right);\n        _count += right - left;\n    }\n    \n    int count() const noexcept {\n        return _count;\n    }\n\nprivate:\n    struct Comparer { bool operator () (const std::pair<int, int> & a, const std::pair<int, int> & b) const noexcept { return a.second < b.first; }; };\n    std::set<std::pair<int, int>, Comparer> _s;\n    int _count = 0;\n};
['C++']