# 将数据流变为多个不相交区间

<p>&nbsp;给你一个由非负整数&nbsp;<code>a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub></code> 组成的数据流输入，请你将到目前为止看到的数字总结为不相交的区间列表。</p>

<p>实现 <code>SummaryRanges</code> 类：</p>

<div class="original__bRMd">
<div>
<ul>
	<li><code>SummaryRanges()</code> 使用一个空数据流初始化对象。</li>
	<li><code>void addNum(int val)</code> 向数据流中加入整数 <code>val</code> 。</li>
	<li><code>int[][] getIntervals()</code> 以不相交区间&nbsp;<code>[start<sub>i</sub>, end<sub>i</sub>]</code> 的列表形式返回对数据流中整数的总结。</li>
</ul>

<p>&nbsp;</p>

<p><strong>示例：</strong></p>

<pre>
<strong>输入：</strong>
["SummaryRanges", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals"]
[[], [1], [], [3], [], [7], [], [2], [], [6], []]
<strong>输出：</strong>
[null, null, [[1, 1]], null, [[1, 1], [3, 3]], null, [[1, 1], [3, 3], [7, 7]], null, [[1, 3], [7, 7]], null, [[1, 3], [6, 7]]]

<strong>解释：</strong>
SummaryRanges summaryRanges = new SummaryRanges();
summaryRanges.addNum(1);      // arr = [1]
summaryRanges.getIntervals(); // 返回 [[1, 1]]
summaryRanges.addNum(3);      // arr = [1, 3]
summaryRanges.getIntervals(); // 返回 [[1, 1], [3, 3]]
summaryRanges.addNum(7);      // arr = [1, 3, 7]
summaryRanges.getIntervals(); // 返回 [[1, 1], [3, 3], [7, 7]]
summaryRanges.addNum(2);      // arr = [1, 2, 3, 7]
summaryRanges.getIntervals(); // 返回 [[1, 3], [7, 7]]
summaryRanges.addNum(6);      // arr = [1, 2, 3, 6, 7]
summaryRanges.getIntervals(); // 返回 [[1, 3], [6, 7]]
</pre>

<p>&nbsp;</p>

<p><strong>提示：</strong></p>

<ul>
	<li><code>0 &lt;= val &lt;= 10<sup>4</sup></code></li>
	<li>最多调用&nbsp;<code>addNum</code> 和 <code>getIntervals</code> 方法 <code>3 * 10<sup>4</sup></code> 次</li>
</ul>
</div>
</div>

<p>&nbsp;</p>

<p><strong>进阶：</strong>如果存在大量合并，并且与数据流的大小相比，不相交区间的数量很小，该怎么办?</p>


## template

```cpp
#include <bits/stdc++.h>
using namespace std;

class SummaryRanges
{
public:
    vector<vector<int>> ans;
    unordered_map<int, int> use;
    int size;
    /** Initialize your data structure here. */
    SummaryRanges()
    {
        size = 0;
    }

    void addNum(int val)
    {
        if (use.count(val) != 0)
            return;
        use[val] = 1;
        int i;
        for (i = 0; i < size; i++)
            if (val < ans[i][0])
                break;
        if (i == 0)
        {
            if (size == 0)
            {
                ans.insert(ans.begin(), {val, val});
                size++;
            }
            else if (val + 1 == ans[i][0])
                ans[0][0] = val;
            else
            {
                ans.insert(ans.begin(), {val, val});
                size++;
            }
        }
        else if (i == size)
        {
            if (val - 1 == ans[i - 1][1])
                ans[i - 1][1] = val;
            else
            {
                ans.push_back({val, val});
                size++;
            }
        }
        else
        {
            if (val + 1 != ans[i][0] && val - 1 != ans[i - 1][1])
            {
                ans.insert(ans.begin() + i, {val, val});
                size++;
            }
            else if (val + 1 == ans[i][0] && val - 1 == ans[i - 1][1])
            {
                ans[i - 1][1] = ans[i][1];
                ans.erase(ans.begin() + i);
                size--;
            }
            else if (val + 1 == ans[i][0])
            {
                ans[i][0] = val;
            }
            else
            {
                ans[i - 1][1] = val;
            }
        }
    }

    vector<vector<int>> getIntervals()
    {
        return ans;
    }
};

/**
 * Your SummaryRanges object will be instantiated and called as such:
 * SummaryRanges* obj = new SummaryRanges();
 * obj->addNum(val);
 * vector<vector<int>> param_2 = obj->getIntervals();
 */


```

## 答案

```cpp

```

## 选项

### A

```cpp

```

### B

```cpp

```

### C

```cpp

```