Upating and merging intervals on every addition
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\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)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass CountIntervals {\npublic:\n\n    set<pair<int,int>> s;\n    int ct = 0;\n    CountIntervals() {\n        \n    }\n    \n    void add(int l, int r) {\n        if(s.size()==0){\n            s.insert({l,r});\n            ct = r-l+1;\n        }\n        else{\n            auto it = s.lower_bound({l,0});\n            auto st = it;\n            st--;\n            if(st->second>=l-1){\n                it = st;\n            }\n            int ml = l,mr = r;\n            int prec = 0;\n            while(it!=s.end()){\n                int ol = it->first,oor = it->second;\n                if(oor<=ml+1||ol<=mr+1){\n                    prec+=oor-ol+1;\n                    ml = min(ml,ol);\n                    mr = max(mr,oor);\n                    s.erase(it++);\n                }\n                else break;\n                \n            }\n            ct+=mr-ml+1-prec;\n            s.insert({ml,mr});\n        }\n    }\n\n\n    \n    int count() {\n        return ct;\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++']