Covered Points Count (Codeforces 1000C)
Tags: Implement, 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: 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: 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$$$.

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.

Sample Inputs:
3
0 3
1 3
3 8

3
1 3
2 4
5 7

Sample Outputs:
6 2 1

5 2 0

Solution (Python):
import sys
N = int(input())
beg = [0] * N
end = [0] * N
for i in range(N):
    (beg[i], end[i]) = list(map(int, input().split()))
    end[i] += 1
beg.sort()
end.sort()
p1 = 0
p2 = 0
count = 0
prev = 0
res = [0] * (N + 1)
while p1 < len(beg):
    if beg[p1] < end[p2]:
        res[count] += beg[p1] - prev
        count += 1
        prev = beg[p1]
        p1 += 1
    else:
        res[count] += end[p2] - prev
        count -= 1
        prev = end[p2]
        p2 += 1
for i in range(p2, len(end)):
    res[count] += end[i] - prev
    prev = end[i]
    count -= 1
print(' '.join((str(res[i]) for i in range(1, len(res)))))

Solution (Python):
import sys
N = int(input())
l = [0] * N
r = [0] * N
for i in range(N):
    (l[i], r[i]) = map(int, input().split())
l.sort()
r.sort()
i = 0
j = 0
res = [0] * (N + 1)
cnt = 0
last = 0
while j < N:
    if i < N and l[i] <= r[j]:
        res[cnt] += l[i] - last
        cnt += 1
        last = l[i]
        i += 1
    else:
        res[cnt] += r[j] + 1 - last
        cnt -= 1
        last = r[j] + 1
        j += 1
res.pop(0)
print(*res, sep=' ')

Solution (Python):
from sys import stdin, stdout
get_string = lambda : stdin.readline().strip('\n')
get_intmap = lambda : map(int, get_string().split(' '))

def testcase():
    n = int(input())
    cnt = dict()
    for i in range(n):
        (l, r) = get_intmap()
        cnt[l] = cnt.get(l, 0) + 1
        cnt[r + 1] = cnt.get(r + 1, 0) - 1
    ans = [0] * (n + 1)
    sk = sorted(cnt.keys())
    cnt_i = 0
    for (ind, i) in enumerate(sk[:-1]):
        cnt_i += cnt[i]
        ans[cnt_i] += sk[ind + 1] - i
    print(' '.join([str(i) for i in ans[1:]]))
testcase()
quit()
for t in range(int(input())):
    testcase()