c++ solution using set
\nclass CountIntervals {\npublic:\n    int sum=0;\n    set<pair<int,int>>s;\n    CountIntervals() \n    {\n        s.clear();\n    }\n    \n    void add(int lef
```\nclass CountIntervals {\npublic:\n    int sum=0;\n    set<pair<int,int>>s;\n    CountIntervals() \n    {\n        s.clear();\n    }\n    \n    void add(int left, int right) \n    {\n        auto it=s.lower_bound({left,INT_MIN});\n        if(it!=s.begin()&&(--it)->second<left)\n        {\n            it++;\n        }\n        while(it!=s.end()&&(it->first)<=right)\n        {\n            left=min(left,it->first);\n            right=max(right,it->second);\n            sum-=(it->second-it->first+1);\n            s.erase(it++);\n        }\n        s.insert({left,right});\n        sum+=(right-left+1);\n    }\n    int count() \n    {\n        return sum;\n    }\n};\n\n```
['C', 'Ordered Set', 'C++']