Set
Limits: 4 sec., 512 MiB
This statement was translated automatically from Ukrainian.
Schoolboy, do you know what a set is? A set of numbers is a collection of numbers that do not repeat.
There is a set \(S\), which is initially empty. You need to process \(q\) such queries.
\(l\ r\) — add all integers from \(l\) to \(r\) inclusive to \(S\). After executing each query, output the size of the set \(S\).
Input
The first line contains an integer \(q\) — the number of queries.
The next \(q\) lines each contain two integers \(l\), \(r\) — the parameters of the queries.
Output
For each of the \(q\) queries, output a single line — the size of the set \(S\) after the query.
Constraints
\(1 \le q \le 4 \cdot 10^5\),
\(1 \le l_i \le r_i \le 10^9\).
The scoring consists of the following subtasks:
1 point for each example from the problem statement,
20 points: \(r_{i - 1} < l_i\),
17 points: \(q \le 1000\), \(l_i \le r_i \le 1000\),
30 points: \(l_i \le r_i \le 10^6\),
31 points: no additional constraints.
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 4 1 4 7 10 2 3 6 12 | 4 8 8 11 |
| Input (stdin) | Output (stdout) |
|---|---|
| 7 2 3 7 11 44 47 1 10 23 34 777 796 4 7 | 2 7 11 15 27 47 47 |
Notes
In the first example, the set will change as follows: \(\{\} \rightarrow \{ 1, 2, 3, 4 \} \rightarrow \{ 1, 2, 3, 4, 7, 8, 9, 10 \} \rightarrow \{ 1, 2, 3, 4, 7, 8, 9, 10 \} \rightarrow \{ 1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12 \}\).
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Set - Editorial
Consider a solution for the subtask \(l_i
\le r_i \le 10^6\). We store all elements not yet added to the
set in a special structure, for example std::set in
C++.
For each query \([l_i, r_i]\) we:
Find the first number in the
setthat is greater than or equal to \(l_i\).If this number is \(\leq r_i\), remove it.
Repeat until all such numbers are removed.
Since we started with \(10^6\) numbers and only remove numbers from the set, we cannot remove more elements than there were initially. So the number of removals is bounded by the number of numbers, and each search or removal takes \(O(\log maxR)\), where \(maxR\) is the maximum value of \(r_i\).
The total complexity of this approach is \(O((q + maxR) \cdot \log maxR)\), where \(q\) is the number of queries.
Solution code in C++ for this subtask
To get the full score, we add to the set not all
possible numbers but only the interesting ones. The interesting numbers
are \(l_i\) and \(r_i+1\).
Having all interesting numbers, we sort them. They can be seen as coordinates on the number line.
Then we do the same as for the smaller subtask, except that each time we remove a whole segment instead of a single number.
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|