Easy | Short | C++
\tclass CountIntervals {\n\tpublic:\n\t\tset>s;\n\t\tint cnt = 0;\n\t\tCountIntervals() {\n\n\t\t}\n\n\t\tvoid add(int left, int right) {\n\t\t\tauto it = s.upp
\tclass CountIntervals {\n\tpublic:\n\t\tset<pair<int,int>>s;\n\t\tint cnt = 0;\n\t\tCountIntervals() {\n\n\t\t}\n\n\t\tvoid add(int left, int right) {\n\t\t\tauto it = s.upper_bound({left,-1});\n\t\t\tif(it!=begin(s) and (--it)->second<left) ++it;\n\n\t\t\twhile(it!=end(s) and it->first<=right){\n\t\t\t\tleft = min(left,it->first);\n\t\t\t\tright = max(right,it->second);\n\t\t\t\tcnt-=(it->second-it->first)+1;\n\t\t\t\tit = s.erase(it);\n\t\t\t}\n\t\t\tcnt+=(right-left+1);\n\t\t\ts.insert({left,right});\n\t\t}\n\n\t\tint count() {\n\t\t\treturn cnt;\n\t\t}\n\t};
[]