Insert interval
Code
# Code
```cpp []
class CountIntervals {
public:
    int ans;
    set<pair<int, int>> st;
    CountIntervals() { ans = 0; }

    void add(int left, int right) {

        pair<int, int> curr = {left, right};
        auto it = st.lower_bound(curr);
        if (it != st.begin()) {
            auto prev = it;
            prev--;
            if (prev->second >= left)
                it = prev;
        }
        vector<pair<int, int>> rem;
        while (it != st.end()) {
            if (it->first > curr.second)
                break;
            curr.first = min(curr.first, it->first);
            curr.second = max(curr.second, it->second);
            ans -= (it->second - it->first + 1);
            rem.push_back(*it);
            it++;
        }
        for (auto i : rem)
            st.erase(i);
        st.insert(curr);
        ans += curr.second - curr.first + 1;
    }

    int count() { return ans; }
};

/**
 * Your CountIntervals object will be instantiated and called as such:
 * CountIntervals* obj = new CountIntervals();
 * obj->add(left,right);
 * int param_2 = obj->count();
 */
```
['C++']