Covered Points Count
Contest: Educational Codeforces Round 46 (Rated for Div. 2)
Difficulty rating: 1700
Tags: data structures, implementation, sortings

You are given $$$n$$$ segments on a coordinate line; each endpoint of every segment has integer coordinates. Some segments can degenerate to points. Segments can intersect with each other, be nested in each other or even coincide.

Your task is the following: for every $$$k \in [1..n]$$$, calculate the number of points with integer coordinates such that the number of segments that cover these points equals $$$k$$$. A segment with endpoints $$$l_i$$$ and $$$r_i$$$ covers point $$$x$$$ if and only if $$$l_i \le x \le r_i$$$.

Input format:
The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of segments.

The next $$$n$$$ lines contain segments. The $$$i$$$-th line contains a pair of integers $$$l_i, r_i$$$ ($$$0 \le l_i \le r_i \le 10^{18}$$$) — the endpoints of the $$$i$$$-th segment.

Output format:
Print $$$n$$$ space separated integers $$$cnt_1, cnt_2, \dots, cnt_n$$$, where $$$cnt_i$$$ is equal to the number of points such that the number of segments that cover these points equals to $$$i$$$.

Constraints:
Time limit: 3.0 s
Memory limit: 256.0 MB

Note:
The picture describing the first example:

Points with coordinates $$$[0, 4, 5, 6, 7, 8]$$$ are covered by one segment, points $$$[1, 2]$$$ are covered by two segments and point $$$[3]$$$ is covered by three segments.

The picture describing the second example:

Points $$$[1, 4, 5, 6, 7]$$$ are covered by one segment, points $$$[2, 3]$$$ are covered by two segments and there are no points covered by three segments.

Example input:
3
0 3
1 3
3 8

Example output:
6 2 1

Example input:
3
1 3
2 4
5 7

Example output:
5 2 0

Editorial:
This problem with small coordinates can be solved using partial sums and some easy counting. Let's carry an array $$$cnt$$$, where $$$cnt_i$$$ will be equal to the number of segments that cover the point with coordinate $$$i$$$. How to calculate $$$cnt$$$ in $$$O(n + maxX)$$$?
For each segment ($$$l_i, r_i$$$) let's add $$$+1$$$ to $$$cnt_{l_i}$$$ and $$$-1$$$ to $$$cnt_{r_i + 1}$$$. Now build on this array prefix sums and notice that $$$cnt_i$$$ equals the number of segments that cover the point with coordinate $$$i$$$. Then $$$ans_i$$$ will be equal to $$$\sum\limits_{j = 0}^{maxX} cnt_j = i$$$. All the answers can be calculated in $$$O(maxX)$$$ in total. So the total complexity of this solution is $$$O(n + maxX)$$$.
But in our problem it is too slow to build an entire array $$$cnt$$$. So what should we do? It is obvious that if any coordinate $$$j$$$ is not equals some $$$l_i$$$ or some $$$r_i + 1$$$ then $$$cnt_i = cnt_{i - 1}$$$. So we do not need carry all the positions explicitly. Let's carry all $$$l_i$$$ and $$$r_i + 1$$$ in some logarithmic data structure or let's use the coordinate compression method.
The coordinate compression method allows us to transform the set of big sparse objects to the set of small compressed objects maintaining the relative order. In our problems let's make the following things: push all $$$l_i$$$ and $$$r_i + 1$$$ in vector $$$cval$$$, sort this vector, keep only unique values and then use the position of elements in vector $$$cval$$$ instead of original value (any position can be found in $$$O(\log n)$$$ by binary search or standard methods as lower_bound in C++).
So the first part of the solution works in $$$O(n \log n)$$$. Answer can be calculated using almost the same approach as in solution to this problem with small coordinates. But now we know that between two adjacent elements $$$cval_i$$$ and $$$cval_{i + 1}$$$ there is exactly $$$cval_{i + 1} - cval_{i}$$$ points with answer equals to $$$cnt_i$$$. So if we will iterate over all pairs of the adjacent elements $$$cval_i$$$ and $$$cval_{i + 1}$$$ and add $$$cval_{i + 1} - cval_i$$$ to the $$$ans_{cnt_i}$$$, we will calculate all the answers in $$$O(n)$$$.
So the total complexity of the solution is $$$O(n \log n)$$$.