使用平衡树来维护,使用L,R两个map
    L[x] = y 表示x的左端点为y
    R[y] = x 表示y的右端点为x

    1. class SummaryRanges {
    2. public:
    3. /** Initialize your data structure here. */
    4. map<int,int> L,R;
    5. SummaryRanges() {
    6. }
    7. void addNum(int x) {
    8. if(L.size()){
    9. auto it = L.lower_bound(x);
    10. if(it!=L.end()&&it->second<=x) return ;
    11. }
    12. int left = L.count(x-1), right = R.count(x+1);
    13. if(left&&right){
    14. R[L[x-1]] = R[x+1];
    15. L[R[x+1]] = L[x-1];
    16. L.erase(x-1), R.erase(x+1);
    17. }
    18. else if(left){
    19. R[L[x-1]] = x;
    20. L[x] = L[x-1];
    21. L.erase(x-1);
    22. }
    23. else if(right){
    24. L[R[x+1]] = x;
    25. R[x] = R[x+1];
    26. R.erase(x+1);
    27. }
    28. else {
    29. L[x] = x;
    30. R[x] = x;
    31. }
    32. }
    33. vector<vector<int>> getIntervals() {
    34. vector<vector<int>> res;
    35. for(auto&item:L){
    36. res.push_back({item.second,item.first});
    37. }
    38. return res;
    39. }
    40. };
    41. /**
    42. * Your SummaryRanges object will be instantiated and called as such:
    43. * SummaryRanges* obj = new SummaryRanges();
    44. * obj->addNum(val);
    45. * vector<vector<int>> param_2 = obj->getIntervals();
    46. */