使用平衡树来维护,使用L,R两个map
L[x] = y 表示x的左端点为y
R[y] = x 表示y的右端点为x
class SummaryRanges {public:/** Initialize your data structure here. */map<int,int> L,R;SummaryRanges() {}void addNum(int x) {if(L.size()){auto it = L.lower_bound(x);if(it!=L.end()&&it->second<=x) return ;}int left = L.count(x-1), right = R.count(x+1);if(left&&right){R[L[x-1]] = R[x+1];L[R[x+1]] = L[x-1];L.erase(x-1), R.erase(x+1);}else if(left){R[L[x-1]] = x;L[x] = L[x-1];L.erase(x-1);}else if(right){L[R[x+1]] = x;R[x] = R[x+1];R.erase(x+1);}else {L[x] = x;R[x] = x;}}vector<vector<int>> getIntervals() {vector<vector<int>> res;for(auto&item:L){res.push_back({item.second,item.first});}return res;}};/*** Your SummaryRanges object will be instantiated and called as such:* SummaryRanges* obj = new SummaryRanges();* obj->addNum(val);* vector<vector<int>> param_2 = obj->getIntervals();*/
