https://leetcode.com/problems/data-stream-as-disjoint-intervals/
class SummaryRanges {
public:
/** Initialize your data structure here. */
void addNum(int val) {
auto it = st.lower_bound(Interval(val, val));
int start = val, end = val;
if(it != st.begin() && (--it)->end+1 < val) it++;
while(it != st.end() && val+1 >= it->start && val-1 <= it->end)
{
start = min(start, it->start);
end = max(end, it->end);
it = st.erase(it);
}
st.insert(it,Interval(start, end));
}
vector<Interval> getIntervals() {
vector<Interval> result;
for(auto val: st) result.push_back(val);
return result;
}
private:
struct Cmp{
bool operator()(Interval a, Interval b) { return a.start < b.start; }
};
set<Interval, Cmp> st;
};