File size: 3,903 Bytes
1f3a244
 
 
 
1
2
3
4
5
{"type": "session", "harness": "opencode", "id": "cf-1000-C", "name": "Codeforces: Covered Points Count"}
{"type": "message", "message": {"role": "system", "content": "You are an expert competitive programmer. You solve problems by analyzing requirements, designing algorithms, and writing clean, efficient code."}}
{"type": "message", "message": {"role": "user", "content": "Problem: Covered Points Count (Rating: 1700)\nTags: data structures, implementation, sortings\n\nYou 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.\n\nYour 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$.\n\nInput: The first line of the input contains one integer $n$ ($1 \\le n \\le 2 \\cdot 10^5$) — the number of segments.\n\nThe 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.\n\nOutput: 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$.\n\nExamples:\nInput: 3\n0 3\n1 3\n3 8\nOutput: 6 2 1\n\nInput: 3\n1 3\n2 4\n5 7\nOutput: 5 2 0\n"}}
{"type": "message", "message": {"role": "assistant", "content": "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)$$$?\nFor 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)$$$.\nBut 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.\nThe 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++).\nSo 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)$$$.\nSo the total complexity of the solution is $$$O(n \\log n)$$$."}}