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 ∈ [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 ≤ x ≤ r_i.
Input
The first line of the input contains one integer n (1 ≤ n ≤ 2 ⋅ 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 ≤ l_i ≤ r_i ≤ 10^{18}) — the endpoints of the i-th segment.
Output
Print n space separated integers cnt_1, cnt_2, ..., 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.
Examples
Input
3
0 3
1 3
3 8
Output
6 2 1
Input
3
1 3
2 4
5 7
Output
5 2 0
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.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We are given n segments on a coordinate line, integer endpoints, and we need for each k from 1 to n, the count of integer points x such that exactly k segments cover x.
n up to 2e5, coordinates up to 1e18, so we cannot iterate over all integer points.
Standard approach: treat segments as intervals on integer lattice, we want distribution of coverage counts across all integer points. Since coordinates are large but number of segments moderate, we can use sweep line with coordinate compression of all relevant points: all segment endpoints l_i and r_i+1 perhaps. Actually typical method: For intervals covering integer points, coverage changes only at integer points which are either start of an interval (l_i) or at point r_i+1 (since interval covers x if x <= r_i). So we can collect all critical points: all l_i, and all r_i+1. But careful: r_i may be up to 1e18, so r_i+1 might overflow? 1e18+1 fits within 64-bit (2^63-1 is 9.22e18) so fine.
We sort these critical points and process differences between consecutive unique points. For each segment between two consecutive critical points, the coverage count is constant (since no event in between). Then the number of integer points in that segment is the length (diff) of that interval of integers: if we have points a and b (a < b), where coverage is constant c, then integer points in [a, b-1]? Wait careful: events: we define that at point x, coverage changes as we move from x-1 to x? Let's define properly.
We have integer points. Consider intervals over the integer line. For each integer x, coverage is number of intervals with l_i <= x <= r_i.
If we have an event at point x where the count of active intervals changes: when we pass x, what happens? Typically, we can use difference array: for each interval [l, r], we increment at l, and decrement at r+1. Then prefix sum at x gives coverage at x. So if we collect all distinct points where changes occur: l_i and r_i+1. Then we sort them, and process in order. For each consecutive pair of event points: say we have points p_i and p_{i+1}, with p_i < p_{i+1}. For any integer x in [p_i, p_{i+1}-1], the prefix sum (i.e., coverage) is constant (equal to the current active count after processing events at p_i). But note that if p_i itself is an event point, the coverage at x = p_i is determined after applying the increment/decrement at p_i? Typically, we want coverage at integer points. In difference array method, we can set diff[l] += 1, diff[r+1] -= 1, and then compute prefix sum over sorted keys. The coverage at x is prefix sum after processing all events up to x inclusive? Actually the typical approach: for each integer coordinate, we can think of an array where we mark start and end. With difference technique, we would for each point x compute sum of diff at all keys <= x. If we process sorted events, we can maintain a current active count after applying the changes at that point. Usually we do: sort all events (positions with delta). Then iterate: keep current = 0, for each event position pos, we apply all deltas at that pos (so current += sum of deltas at pos). Then for any x from this pos to next event-1, the current remains same. Also note that the coverage at the event position itself (pos) is the current after applying the deltas at pos? It depends: If a segment starts at l, it covers l. So the increment at l should be effective for x >= l. So if we process events in increasing order, we apply the delta at position l before considering coverage at l. So we can define that when we arrive at position p, we first update current by adding all deltas at p, then coverage for x = p is current. Then for the next positions up to next event-1, coverage remains current. Similarly, for r+1, we decrement at r+1; that means coverage for x = r+1 should not include the segment that ended at r, so before we consider coverage at r+1, we apply the decrement, so coverage at r+1 is current after decrement, correct. So consistent: at each event point, we apply changes at that point first, then coverage for that point is current. Then for any integer x between this event point (inclusive) and next event point (exclusive), coverage is constant and equal to current after processing this event. But careful: if there is a gap between p_i and p_{i+1}, then the integer points from p_i to p_{i+1}-1 have the same coverage (including p_i). But p_{i+1} will be processed next. So essentially, each interval between consecutive distinct event points defines a contiguous block of integer points where coverage is constant. The block includes the start point (the event point) and goes up to just before the next event point. The length of the block is (p_{i+1} - p_i). Because the integer points are p_i, p_i+1, ..., p_{i+1}-1. So number of points = p_{i+1} - p_i.
Thus we can compute: sort all unique critical points: all l_i and r_i+1. Also note that there might be points beyond the last event? After processing all events, current becomes 0, but there might be points after the last r_i+1? The last event point is the maximum of r_i+1. After that, coverage is zero (since all intervals ended). Since we only care about points covered by at least one segment (k>=1), we can ignore points with coverage 0. So we only need to consider intervals from the minimum event point (which would be some l_i) up to the maximum r_i (since beyond max(r_i) coverage is 0). However, we need to include integer points up to max(r_i) because they might be covered. Let's consider: The events are l_i and r_i+1. The maximum event point might be max(r_i+1). After that point, coverage = current (which should be 0). The integer points up to max(r_i) are covered possibly. But the block from the largest event point (which is some r_max+1) to infinity has coverage 0. So we can just process blocks up to that largest event point. But careful: Suppose the largest event point is r_max+1. Then there is a block from previous event point (call it prev) to r_max+1 - 1 = r_max. This block includes r_max. So that's fine.
Thus algorithm: collect all points: For each segment (l, r), add l and r+1 to a list. We need to handle possible overflow? r up to 1e18, r+1 up to 1e18+1, still okay. Then sort unique points. Let the sorted unique points be P[0..m-1]. We'll also need to consider that the smallest point might be >0? But we don't care about uncovered points. The intervals coverage will be constant between P[i] and P[i+1]-1.
We'll iterate i from 0 to m-2 (since we need next point). At each step, we have current coverage count after applying all events at P[i] (we need to compute that). Then the number of integer points with that coverage is (P[i+1] - P[i]). Add that to answer array at index = current (coverage count). Then we move to next point: we need to update current by applying all events at P[i+1]. But careful: When we start, before processing any point, we have current = 0. However, the first event point P[0] is the minimum l_i maybe, but note there could be points less than P[0]? Those are uncovered (coverage 0). So we start at i = 0: we need to apply events at P[0] first? Actually we can process as: current = 0. For i from 0 to m-1: gather all deltas at P[i] and apply to current. Then after updating, we consider the interval from P[i] to (next_event - 1). But if we apply at P[i] before counting, then the interval starting at P[i] uses the updated current. However, what about the interval between previous event (if any) and P[i]? That interval would have been handled in previous step. So indeed we can maintain a pointer to next event.
Simpler: sort all events (position, delta). Delta is +1 for start, -1 for end (r+1). Then we traverse events in order of position. We'll keep current active count before applying events at current position? Actually typical sweep: initialize cur = 0, prev_pos = first event position. Then for each group of events at same position pos: first, the segment from prev_pos to pos-1 (if prev_pos < pos) has coverage = cur (the value before applying events at pos). So we add (pos - prev_pos) to ans[cur]. Then we apply all deltas at pos to cur. Then set prev_pos = pos. After processing all events, there may be remaining after last event? But after last event, cur becomes something (probably 0). The integer points from last event pos to ...? Actually after last event, there is no further event, but coverage from last event pos onward is cur after applying its deltas. However, we need to cover points up to max r_i. But note that the last event might be some r+1, after applying delta, cur might be 0. And there might be no more events. But there might be points after the last event? The last event is the maximum (r_i+1). Points from that position onward have coverage cur (0). But we only care about coverage >=1, so we don't need to count them. However, we must also consider points at the last event itself? The last event position is r+1, which is not covered by that segment (since it ended at r). But there might be other segments that extend beyond that? No, because we took maximum r_i+1, so any segment that ends later would have a larger r+1. So after processing last event, all segments have ended. So coverage 0. So we don't need to count beyond last event. However, we need to consider the points from prev_pos to pos-1 before last event; that includes points up to pos-1 = (max(r_i+1)-1) = max(r_i). Good. Also, what about points before first event? The first event is min(l_i). Points < that are uncovered. So we start with prev_pos = first event position? But then we would miss counting points at the first event position? Because when we first process, we set prev_pos = first event pos. Then for the first group of events at pos = first event, we will compute (pos - prev_pos) = 0, so no contribution. Then we apply deltas, then set prev_pos = pos. Then next pos, we count from previous pos to current pos-1, which includes first event pos? Wait, we need to count points starting from first event pos because coverage at that position should be after applying start deltas. In our described approach, we count intervals between events using cur before applying events at current position. This means that the coverage for the interval [prev_pos, pos-1] corresponds to the state before processing pos. So the points at prev_pos itself were already counted in the previous step? Let's simulate:
We start with cur = 0, prev_pos = first event position? Actually we should set prev_pos to the first event position initially? But then we haven't applied any deltas, and we would count from prev_pos to pos-1 = first event position to first event position-1 => empty. Then we apply deltas at first event, then set prev_pos = first event. Then we move to next event, and we count from prev_pos (first event) to next_pos-1, which includes first event. So indeed, points at first event are counted in the interval when we go from first event to next event. That is correct because after applying deltas at first event, coverage for first event is cur (new), and that coverage holds for all points from first event up to next event-1. So the method works: Initialize prev_pos = the position of the first event (the smallest l_i). But we need to have cur value appropriate for the interval [prev_pos, next_pos-1] after applying events at prev_pos. However, in the algorithm described, we count before applying events at current position. So to make it work, we need to set cur after applying events at prev_pos before counting the first interval. That suggests we should process events differently: For each position in sorted order, we apply deltas first, then count to next. Let's restructure:
We have events grouped by position. Let positions sorted: pos1 < pos2 < ... < pos_m.
We can maintain cur = 0 initially. Set prev = pos1. But we need to apply deltas at pos1 before counting anything? Actually we want coverage for points starting at pos1 to be after applying deltas at pos1. So we should apply deltas at pos1 first. Then after applying, cur becomes coverage for pos1. Then we consider the interval from pos1 to (pos2-1). Number of integer points = pos2 - pos1. So we add to ans[cur] this length. Then we move to pos2, apply its deltas, then count to pos3, etc. This works.
Thus algorithm:
Build a map (or vector of pairs) of position -> net delta (sum of +1 for starts, -1 for ends).
For each segment (l, r): delta[l] += 1; delta[r+1] -= 1.
Sort positions in increasing order.
Initialize cur = 0, prev_pos = first position? Actually we need to apply deltas at first position before counting. So we can set cur = 0, and iterate over positions in order:
For each position pos in sorted list:
// Before processing, we have cur reflecting coverage for points >= previous pos? Let's design loop:
Option: For i from 0 to m-1:
pos = positions[i]
// The coverage for points from previous_pos to pos-1 is previous_cur (before updating at pos)
// But we want coverage after updating at pos for points from pos onward.
// So the typical method: maintain previous position and current coverage.
Standard method:
cur = 0
prev = positions[0]
for each pos in sorted positions (including maybe after last we need to handle something?):
// Actually we want to count the interval from prev to pos-1 using cur (which is coverage after events at prev)
// Then we update cur with deltas at pos, set prev = pos.
But that requires that before entering loop, we have already applied events at the first position to set cur? We can do: first, apply deltas at first position to cur, then set prev = first position. Then for each subsequent position pos: count length = pos - prev, add to ans[cur]; then apply deltas at pos to cur; then set prev = pos. After loop, we may need to count beyond last? There's no further event, but coverage after last event may be non-zero? But after processing last event, cur might be something. However, after the last event (which is maximum r_i+1), cur should be 0 because all segments ended. But just in case, we could consider that there are no points with coverage>0 after last event because last event is r_max+1, and points from that position onward have coverage cur. But we only care about points up to r_max. Since we already counted interval from prev (which was the last event) to (??). Actually after loop, we have processed last event: we applied deltas at last event, and set prev = last event. But we haven't counted the interval from that last event to the next event. Since there is no next event, we could consider that there are no integer points with coverage >0 beyond last event. However, if cur after last event is not zero, that would mean some intervals are still active after r_max+1, but that cannot happen because r_max+1 is the maximum event and corresponds to the end of the longest segment. After applying the decrement at r_max+1, cur should become 0. But could there be segments that start after some end? No, events are l and r+1; if there is a segment that ends at r_max, its r_max+1 decrement is included, so after processing that, cur reduces. If there are segments that start at r_max+1? But start at r_max+1 would be possible? l_i can be up to 1e18, could be > r_max? But r_max is max(r_i). If there is a segment with l_i > r_max, then its r_i >= l_i > r_max, contradicting r_max being max r_i. So no. So cur becomes 0. So we don't need to count after last event.
But the interval from prev (last event) to something: The points from last event (which is r_max+1) to infinity: coverage 0, so ignore. So we don't need to do anything after loop.
But we missed counting the interval from the first event to the second event. In this scheme we need to handle the first interval specially. Let's detail:
We have sorted positions: p1 < p2 < ... < pk.
We can do:
cur = 0
prev = p1
// apply deltas at p1 to cur
cur += delta[p1] // because we need coverage for points starting at p1
Now we need to count the points from p1 to p2-1 (if p2 exists). That's length = p2 - p1.
Add to ans[cur] this length.
Then for i=2 to k:
// before processing pi, cur is coverage after applying deltas at prev (which is p_{i-1})
// Now we need to update for pi:
// But we have already counted interval from prev to pi-1. So now we apply deltas at pi:
cur += delta[pi]
// then set prev = pi
// If i < k, then count interval from pi to p_{i+1}-1: length = p_{i+1} - pi
// and add to ans[cur]
// else (i==k) we don't count after last event.
This works but we have to be careful to apply the delta at each position before counting the interval that starts at that position.
Alternatively, we can use the "count before update" method but then we need to have a fake initial point. Let's adopt a simpler loop:
Collect all distinct positions, sorted. Let them be in array xs.
Also we have a map delta.
Initialize cur = 0, last_x = xs[0].
// First, apply deltas at xs[0] to cur.
cur += delta[xs[0]];
// Now iterate i from 1 to m-1:
// current position xs[i]
// The number of integer points with coverage cur from last_x to xs[i]-1 is xs[i] - last_x.
ans[cur] += xs[i] - last_x;
// Then apply deltas at xs[i] to cur
cur += delta[xs[i]];
// Update last_x = xs[i];
After loop, // after processing last event, we could optionally count beyond? But as argued, no need.
But wait: This will count interval from last_x (which after first step is xs[0]) to xs[1]-1, which includes xs[0]. Good. Then after processing xs[1], last_x becomes xs[1]. Then we count from xs[1] to xs[2]-1, etc. After processing the last event xs[m-1], we don't count anything beyond. But there might be integer points from xs[m-1] to something? However, coverage for xs[m-1] itself: we have applied deltas at xs[m-1]; but if xs[m-1] is some r+1, then coverage at that point is cur after decrement, which might be 0. But we might want to count points from xs[m-1] to ...? Actually we should only count points up to max(r_i). Let's see: The last event is the maximum among all l_i and r_i+1. Suppose the maximum event point is some r_max+1. Then after processing that event, we have applied the decrement(s) at r_max+1, cur becomes 0. The points from r_max+1 onward have coverage 0. But what about point r_max? That was already counted in the interval from the previous event (which could be some earlier point) to r_max+1 - 1 = r_max. So yes, r_max is covered. Good.
But what if the maximum event point is a l_i (i.e., some start after all ends)? That would mean there is a segment that starts after all other segments have ended, and its r_i maybe even larger. But then its r_i+1 would be even larger and thus would be the maximum event point, not the l_i. Because if there is a segment with l_i as the maximum event, then its r_i+1 would be > l_i, so the maximum event is actually r_i+1. So the maximum event is always a r_i+1 (or possibly also l_i if no r_i+1 greater, but r_i+1 > l_i always, so the max is a r_i+1). So indeed, last event is a r_i+1. So after processing it, cur reduces to 0. So no further points.
Thus the loop described works.
But note: The initial cur after applying delta at xs[0] might be 0 if xs[0] is a r+1? Could the smallest event be a r+1? Possibly: Suppose all segments start after 0, and there is a segment starting at 5, but also a segment that ends at 0? Actually r+1 must be at least 1 if r=0. So l_i can be 0. The smallest event could be a l_i (0) or r_i+1 (some positive). But if the smallest event is a r+1, that would imply there is a segment with r+1 small, meaning there is a segment ending at r such that r+1 is the minimum coordinate among all events. That could happen if there is a segment [0,0] then r+1=1, and if all other segments start at >=1, then the minimum event is 0? Actually [0,0] has l=0, r+1=1. So events are 0 and 1. Minimum is 0 (l). So if there is any segment with l=0, the minimum event is 0. But if all segments start at >0, say [5,10], then events are 5 and 11, min is 5. So it's a start. Could we have a case where min event is a r+1? Suppose we have a segment [2,2] then l=2, r+1=3. If no other segment with l<2, then min event is 2 (l), not 3. So it seems min event is always a l_i because l_i <= r_i < r_i+1, so l_i is always <= r_i and <= r_i+1, but there could be a case where the smallest l_i is greater than some r_j+1? For example, we have a segment that ends early and starts later? No, r_j+1 is >= l_j+1 maybe > l_j? Actually compare l_i and r_j+1: Since l_i and r_j could be anything, it's possible that some r_j+1 is less than some l_i. Example: segment A: [10,20] -> events 10,21. segment B: [0,5] -> events 0,6. So min event is 0 (l of B). Not an r+1. For r+1 to be smallest, you'd need a segment that ends at small coordinate but starts even smaller? But l <= r, so l of that segment would be <= r, so l <= r, so l <= r < r+1, so l is less than r+1. So for any segment, its own l is less than its r+1. So the minimum of all events across all segments must be the minimum of all l_i, because each l_i is less than its own r_i+1, and any other segment's r_j+1 might be smaller than some other l_i but it's not smaller than its own l_j. However, it's possible that the overall minimum among all l_i is greater than some other segment's r_j+1? For segment j, r_j+1 > l_j. If l_min is the smallest l across all segments, then for any segment j, l_j >= l_min, so r_j+1 > l_j >= l_min, so r_j+1 > l_min. Therefore l_min is smaller than every r_j+1. Thus the global minimum event is always a start (l_i). Good. So xs[0] is always a l_i, and delta at xs[0] is positive (at least +1). So cur after first update is >=1. So we count from that start.
Thus algorithm is safe.
We need to store answer array of size n+1 (since coverage can be up to n) and output cnt_1..cnt_n.
Complexities: O(n log n) for sorting events.
But careful: number of distinct event points can be up to 2n, which is 4e5, fine.
Now check examples.
Example1:
n=3
segments: [0,3], [1,3], [3,8]
Compute events:
0: +1
3+1=4: -1? Actually [0,3] => +1 at 0, -1 at 4.
[1,3] => +1 at 1, -1 at 4.
[3,8] => +1 at 3, -1 at 9.
So events: 0:+1, 1:+1, 3:+1, 4:-1 (two times -> -2), 9:-1.
Sorted positions: 0,1,3,4,9.
Initialize:
cur = delta[0]=1, last_x=0.
i=1: pos=1, ans[1] += (1-0)=1 => ans[1]=1.
cur += delta[1] => cur=2.
last_x=1.
i=2: pos=3, ans[2] += (3-1)=2 => ans[2]=2.
cur += delta[3] => cur=3.
last_x=3.
i=3: pos=4, ans[3] += (4-3)=1 => ans[3]=1.
cur += delta[4] => cur=3-2=1? Actually delta[4] = -2, so cur=1.
last_x=4.
i=4: pos=9, ans[1] += (9-4)=5 => ans[1]=1+5=6.
cur += delta[9] => cur=0.
last_x=9, loop ends.
We have ans[1]=6, ans[2]=2, ans[3]=1. Others zero. Output: 6 2 1. matches.
Example2:
[1,3], [2,4], [5,7]
Events: 1:+1, 4:-1? Actually [1,3]: +1 at1, -1 at4 (3+1)
[2,4]: +1 at2, -1 at5 (4+1)
[5,7]: +1 at5, -1 at8 (7+1)
So events: 1:+1, 2:+1, 4:-1, 5:+1-1? Actually at 5 we have both -1 from second and +1 from third => net 0? Let's compute: For [2,4], r+1=5 -> -1. For [5,7], l=5 -> +1. So delta[5] = 0. We might still include 5 as event? But if net delta is 0, we could skip it because it doesn't change cur. However, it's still a point where coverage could change? Actually if net delta=0, there is no change, so coverage remains same. But we must be careful: Even if net delta=0, the point 5 is still an event? In the difference array, we only need to consider positions where total delta is non-zero, because they are the only ones causing change. However, there might be a scenario where we have both +1 and -1 at the same point, but that cancels; the coverage doesn't change, so we don't need to treat it as a boundary. In our algorithm, we would process positions with net delta = 0; but that would just add a length of 0 (since next position minus current might be positive? Actually if we include it, we would break the interval at that point unnecessarily. But since delta=0, cur remains same, and we would then count a zero-length interval from that point to next? Let's see: If we include a position with net delta 0, we would do:
At previous step, last_x was something.
Then we encounter pos with net delta 0, we first add length = pos - last_x (which could be >0). That's fine; it counts points up to pos-1 with previous cur.
Then we update cur += 0, so cur unchanged.
Then set last_x = pos.
Then next pos, we count length = next - pos. That splits the interval at pos, but since cur unchanged, the two intervals have same coverage, and total length = (pos - last_prev) + (next - pos) = next - last_prev, same as if we skipped the middle point. So including zero-delta positions doesn't change answer, but adds extra iteration. However, we must ensure we don't double count the point pos itself? The first interval counted points from last_x to pos-1, not including pos. The second interval then counts from pos to next-1, which includes pos. So point pos is accounted in the second interval. But if cur unchanged, coverage at pos is the same as before, so it's fine to assign it to the second interval. So it's still correct. But to be safe, we could compress only positions with non-zero net delta, but it's okay to keep all l and r+1 and then combine deltas. Sorting 2n points, some may coincide, we should combine deltas at the same coordinate anyway. If after combining, delta is 0, we could skip adding that coordinate to the list to reduce processing, but it's not necessary. However, need to be careful: If we skip a coordinate where net delta is 0, but there might be a need to break the interval? No, because coverage doesn't change, so no need. So we can skip. But if we include, it's fine but might cause extra operations; negligible. But we should ensure that our algorithm's counting logic works even if we include zero-delta points. In the described loop, we do:
add ans[cur] += pos - last_x (points from last_x to pos-1)
then cur += delta[pos] (which is 0)
then last_x = pos
So effectively we start a new interval at pos, and next step will count from pos to next-1. That's okay, because coverage remains same. The only subtlety is that the point pos itself is not counted in the first addition (since it's from last_x to pos-1). It gets counted in the subsequent addition when we count from pos to next-1. So as long as we don't miss counting any point, it's fine. However, we must ensure that if there are consecutive zero-delta points with no change, we still count all points correctly. For instance, if we have positions a and b both with zero delta, and a<b. Then we would count interval from last_x to a-1, then set last_x=a, then process b: count from a to b-1, etc. So total from previous last_x to b-1 = (a - last_x) + (b - a) = b - last_x. Works. So including zero-delta positions is okay.
But there is an important edge: If two events at same coordinate (like multiple starts/ends) we combine them into a net delta. That's necessary. So we'll use a map to aggregate deltas.
Now complexity: we have up to 2e5 segments, so up to 4e5 events (l and r+1). Sorting 4e5 numbers is okay.
But we must consider that coordinates are up to 1e18, so we need 64-bit ints.
Now we should produce answer array of length n (1-indexed). Use vector ans(n+2, 0) maybe. But final output cnt_1..cnt_n should be printed as space-separated integers. They can be up to 1e18? Actually maximum number of integer points covered could be up to range of coordinates, which could be up to 1e18. But n is 2e5, but the total number of integer points covered by at least one segment could be huge (e.g., one segment covers 0..1e18). So answer values can be up to 1e18. So need 64-bit (long long). Output as long long.
Now we need to be careful with off-by-one issues. Let's test more cases.
Case: single segment [0,0]: events: +1 at 0, -1 at 1. Sorted positions: 0,1. Process: cur = delta[0]=1, last_x=0. i=1: pos=1, ans[1] += 1-0=1 => cnt1=1. cur += delta[1] = -1 => cur=0. End. So answer: 1. Good.
Case: two segments: [0,2] and [3,5]. They are disjoint. events: 0:+1, 3:+1, (2+1=3): -1, (5+1=6): -1. So events: 0:+1, 3: +1-1 =0? Actually at 3 we have +1 from second segment start and -1 from first segment end. Combined delta = 0. But if we include 3, then process: sorted: 0,3,6. Process: cur=1, last=0; i=1: pos=3, ans[1] += 3-0=3 => points 0,1,2. cur += delta[3]=0 => cur=1; last=3; i=2: pos=6, ans[1] += 6-3=3 => points 3,4,5? But careful: point 3 is covered by which segments? Segment1 ends at 2, segment2 starts at 3, so point 3 is covered by segment2 only? Actually segment2 covers x if l<=x<=r: 3 <= x <=5, so point 3 is covered. So yes, coverage is 1. So ans[1] gets total 6, which is correct: points 0..2 and 3..5 => 6 points. But we double counted? Let's check: first addition gave points 0,1,2 (since last_x=0 to 3-1=2). Second addition gave points 3,4,5 (last_x=3 to 6-1=5). So correct. So zero delta at 3 didn't cause issue.
Now case: nested segments: [0,5] and [1,4]. Events: 0:+1, 1:+1, (5+1=6): -1, (4+1=5): -1. So events: 0:+1, 1:+1, 5:-1, 6:-1. Sorted: 0,1,5,6. Process:
cur=1 (delta0), last=0
i=1 pos=1: ans[1] += (1-0)=1 => point 0 coverage? cur=1? Actually after first cur=1, we count points from 0 to 0, so point 0 covered by 1 segment. Good.
cur += delta[1]=+1 => cur=2, last=1
i=2 pos=5: ans[2] += (5-1)=4 => points 1,2,3,4 coverage 2. Good.
cur += delta[5] = -1 => cur=1, last=5
i=3 pos=6: ans[1] += (6-5)=1 => point 5 coverage 1. cur += -1 => cur=0.
Ans: cnt1 = 1+1=2 (points 0 and 5), cnt2=4. Total points = 6, matches integer points 0..5.
Now case: coincident segments: [0,3] and [0,3]. Events: 0:+2, 4:-2. Process: cur=2, last=0; i=1 pos=4: ans[2] += 4-0=4 => points 0,1,2,3 coverage 2. So cnt2=4. Others zero.
All good.
Now we must handle large n and coordinates. Use long long for positions and counts.
Implementation steps:
Read n.
Use a map<long long, int> delta (or unordered_map, but we need sorted, so we can use vector of pairs and sort after collecting).
For each segment (l, r):
delta[l] += 1;
delta[r+1] -= 1;
After reading, transfer to a vector of (pos, delta) pairs. It's better to create a vector<pair<long long, int>> events and sort by pos. Since we have many, using map might be O(n log n) anyway, but we can simply push two entries for each segment and then sort and combine. Sorting 2n entries and then merging adjacent equal positions is also O(n log n). That is simpler: create vector<pair<long long, int>> ev; for each segment, ev.emplace_back(l, 1); ev.emplace_back(r+1, -1); then sort ev by first. Then iterate through sorted ev, merging same positions: create vector pos, vector d; iterate over sorted ev, if same as previous, add to net, else push new. This avoids map overhead, but both are fine. I'll use vector approach.
After we have distinct positions and net deltas, apply algorithm.
Pseudo:
vector<pair<ll, int>> ev; // ll = long long
for i in 1..n:
read l, r;
ev.push_back({l, 1});
ev.push_back({r+1, -1});
sort(ev.begin(), ev.end());
// compress to distinct pos with net delta
vector<pair<ll, int>> points; // pos, delta
for i from 0 to ev.size()-1:
if i==0 or ev[i].first != ev[i-1].first:
points.push_back({ev[i].first, ev[i].second});
else:
points.back().second += ev[i].second;
// Now points.size() = m
// Initialize
vector ans(n+2, 0); // index up to n
int cur = points[0].second; // after applying at first position
ll last = points[0].first;
for (int i = 1; i < points.size(); ++i) {
ll pos = points[i].first;
// number of integer points from last to pos-1
ans[cur] += pos - last;
// update cur with delta at pos
cur += points[i].second;
last = pos;
}
// After loop, no further points.
// But we must ensure that after processing the last point, there are no uncovered points with coverage >0. Since cur becomes 0, and last is the last point (which is some r+1). Points from last onward have coverage cur (0), so ignore.
// Print ans[1]..ans[n]
Check: In the loop, we rely on points[0] being start with positive delta. As argued, first position is smallest l, and its delta is positive (since it's sum of +1's from segments starting at that point; there could be multiple segments starting at same l, so delta positive). So cur after first assignment is >=1.
But is it always correct that we apply delta at first point before counting any interval? In our initialization, we set cur = points[0].second, last = points[0].first. This means we have already applied the delta at the first position. So for the interval from last to the next position-1, coverage = cur. So we will count that in the first iteration. That's correct.
Now we need to consider if there are multiple points and the last point has delta causing cur to become zero. That's fine.
Edge case: what if there is only one point? That would happen if all segments have same l and same r+1? Actually we have at least two points? Because for each segment we add both l and r+1. Even if all segments are exactly the same, we will have two distinct points: l and r+1 (unless l = r+1? impossible because r+1 > l). So points.size() >= 2. So loop with i from 1 is safe.
But consider a scenario where after merging, some point might have net delta 0, and it could be the first point? Could the first point have net delta 0? As argued, the smallest point among all l_i and r_i+1 is a l_i, so delta at that point is sum of +1's from segments that start there. Could it be that some segments start there and also some segments end there? r_i+1 could equal that l_i? For a segment, r_i+1 > l_i, so r_j+1 could equal some other l_i. It is possible that the smallest point is both a start of some segments and an end (r+1) of some other segments that end at that point-1. For example, segment A: [0,0] -> start 0, end 1; segment B: [1,2] -> start 1, end 3. The smallest point is 0 (start of A). At 0, there is no r+1=0 because r+1>=1. So delta at 0 is +1. But consider another: segment C: [5,10] start 5; segment D: [0,4] end at 4, so r+1=5. So the smallest point is 0 (start of D), delta +1. But 5 is not smallest. So first point's delta cannot be zero because it's a start and there is no r+1 smaller than it. So it's positive.
Thus algorithm stands.
Now we need to be careful with the data type of ans. ans[k] can be up to 10^18, but sum of ans[1..n] is total number of integer points covered by at least one segment, which could be up to 10^18 as well. So use long long (64-bit signed). In C++, we can use long long (or unsigned long long). Output with printf or cout.
Now complexity O(n log n). Memory O(n).
Let's test with a more tricky case: segments that degenerate to points, overlapping at same point. Works.
Now we must ensure that we don't miss points exactly at the boundary where r+1 is large. The algorithm counts integer points from last_x to pos-1 inclusive. So if pos = r+1, then points up to r are included. Good.
Now consider a case where there are gaps: For example, segments: [0,0] and [2,2]. Events: 0:+1,1:-1,2:+1,3:-1. Sorted: 0,1,2,3. Process:
cur=1,last=0; i=1: pos=1, ans[1]+=1 -> point 0; cur+= -1 =0; last=1
i=2: pos=2, ans[0]? Actually ans[cur] where cur=0. So ans[0] gets 1? But we don't track ans[0]. But we should count points from last=1 to 2-1=1: point 1. This point is not covered by any segment (since segments cover 0 and 2). So ans[0] would get incremented. But we are only interested in k>=1. However, we currently add to ans[cur] regardless, and ans[0] is not part of output. That's fine, we just ignore ans[0]. But we must ensure we don't accidentally index out of bounds if cur > n? Could cur exceed n? cur is the number of active segments, which at most n. So cur <= n. But when cur = 0, ans[0] gets incremented but we ignore. That's okay. However, ans array size should be n+1 (indices 0..n) to avoid out-of-bounds when cur=0. We'll allocate ans of size n+2 to be safe.
Now for this example, after i=2: cur=0, ans[0] gets 1 (point 1). Then we update cur += delta[2]=+1 => cur=1, last=2. i=3: pos=3, ans[1] += (3-2)=1 -> point 2; cur += -1=0. So ans[1] total = 2 (points 0 and 2). So correct.
Thus algorithm works.
Now we need to output cnt_1..cnt_n. So we print ans[1] ... ans[n] as space-separated.
Now consider integer overflow when adding to ans[cur] with (pos - last). pos and last are up to 1e18+1, difference can be up to 1e18, so ans values can be up to maybe 1e18 (sum of many differences could exceed 1e18? The total points covered could be up to range of union of segments, which could be up to 1e18 as well, but if multiple disjoint segments sum, it could be up to n * (range) but that's not possible because segments might be disjoint but then union's total length is sum of lengths, each length could be up to 1e18, so total sum could be n * 1e18 which exceeds 1e18 but is still within 64-bit? n=2e5, product = 2e5 * 1e18 = 2e23, which is > 2^63-1 (9e18). So ans values could exceed 64-bit signed range. But careful: The total number of integer points covered by at least one segment is the size of the union of intervals. That could be as large as 1e18 (if intervals overlap) but if intervals are disjoint and each is large, union could be sum of lengths, which could be huge. However, is it possible that the union size exceeds 1e18? Coordinates are up to 1e18, so each interval length <= 1e18. If intervals are disjoint, they could be placed far apart, but the coordinate line only goes from 0 to 1e18? Actually constraints: 0 ≤ l_i ≤ r_i ≤ 10^18. So all coordinates are within [0, 10^18]. So the whole line is from 0 to 10^18. The union of any intervals within [0,10^18] cannot exceed 10^18+1 integer points (since there are only that many integer points from 0 to 10^18 inclusive). Because the coordinate line is bounded? Wait, the constraint says l_i and r_i are between 0 and 10^18 inclusive. So yes, all endpoints are within [0, 10^18]. So the integer points that can be covered are within [0, 10^18]. The total number of integer points in that range is 10^18 + 1, which is about 1e18+1. So the maximum possible cnt_k sum across k is at most 1e18+1, which fits in 64-bit unsigned? 1e18+1 is less than 2^63-1? 2^63-1 is about 9.22e18, so 1e18+1 fits within signed 64-bit. Indeed, 2^63-1 ≈ 9.22e18 > 1e18. So ans values can be at most ~1e18. So long long (signed) is fine.
Thus we are safe.
Now implement.
But there is an alternative approach: using a Fenwick tree? Not needed.
Now we must be careful with potential memory: ev vector size 2n = 400k, fine.
Now double-check: Are we sure that we treat integer points correctly? The segments include both endpoints. So a point x is covered if l_i ≤ x ≤ r_i. Our difference method: +1 at l_i, -1 at r_i+1. So that matches.
Now test with a random small case manually, or with brute force for small ranges to ensure correctness.
Let's think about a case with overlapping at a single point. Eg: [0,0], [0,0], [0,0] three segments. events: 0:+3, 1:-3. Process: cur=3, last=0; i=1: ans[3] += (1-0)=1 => point 0 covered by 3. So cnt3=1. Good.
If there are segments with l_i = r_i+1? impossible.
If r_i = 10^18, then r_i+1 = 10^18+1, which is > 10^18. But constraint says r_i ≤ 10^18, but r_i+1 can be 10^18+1, which is allowed as an event coordinate. Since we only consider integer points up to 10^18 (because l_i and r_i are within 0..10^18), the point 10^18+1 is not covered anyway. So it's fine.
Now consider scenario where there are many events with same coordinate: we combine delta.
Implementation details:
We'll use vector<pair<long long, int>> ev; ev.reserve(2*n). For each segment, we do:
ev.emplace_back(l, 1);
ev.emplace_back(r+1, -1);
But careful: r can be 10^18, r+1 may overflow if we use int? We'll use long long for positions.
After sorting, we iterate to combine:
vector<pair<long long, int>> points;
for (size_t i = 0; i < ev.size(); ++i) {
if (i == 0 || ev[i].first != ev[i-1].first) {
points.emplace_back(ev[i].first, ev[i].second);
} else {
points.back().second += ev[i].second;
}
}
Now the sweep:
vector ans(n+1, 0); // index 0..n
int cur = points[0].second;
long long last = points[0].first;
for (size_t i = 1; i < points.size(); ++i) {
long long pos = points[i].first;
ans[cur] += pos - last; // this counts points from last to pos-1
cur += points[i].second;
last = pos;
}
Now note: cur could be 0 after last update. But we don't need to add anything after loop.
Now output:
for (int k = 1; k <= n; ++k) {
cout << ans[k] << (k == n ? "\n" : " ");
}
Now we must consider that ans[cur] might be indexed with cur possibly > n? No, cur is sum of deltas, which is at most total starts, which is n. So fine.
Now test with a tricky case where some intervals are nested and have same start/end:
Now test where there are many intervals with same start and end: works.
Now test where there is a gap between intervals but we have zero delta events: we already tested.
Now potential pitfalls: When we combine events, we might end up with a final points list where the last point's delta is negative, causing cur to become 0. That's fine.
But what about if after combining, points[0] is not a start? As argued, it must be a start, but we should ensure that our code doesn't assume points[0].second > 0; but even if it's zero (in some degenerate case, maybe all segments are empty? But segments are at least points, so l_i <= r_i. Could there be a segment with l_i = r_i+1? No. So if all segments have l_i > some r_j+1? But still smallest is l_min, positive delta. So fine.
Now let's consider scenario where n=0? Not allowed, n>=1.
Now time complexity: O(n log n). Good.
Now there's also the possibility to avoid combining and directly sort the 2n events and then sweep while merging on the fly. That is also possible: sort ev; then iterate through ev, keeping track of current position and active count, and we can accumulate lengths between consecutive distinct positions. But we need to group same positions because we need to apply all deltas at that position before counting the next interval. But we can do:
sort ev.
cur = 0
last_pos = ev[0].first
i = 0
while i < ev.size():
pos = ev[i].first
// count from last_pos to pos-1
ans[cur] += pos - last_pos
// now apply all deltas at this position
while i < ev.size() && ev[i].first == pos:
cur += ev[i].second
i++
last_pos = pos
// after loop, we might need to count something? Actually after processing last group, there is no next position, so we stop. But we haven't counted points from last_pos to ...? Actually after processing the last group, we have updated cur to whatever (should be 0) and last_pos is the position of the last group. But what about integer points from that position onward? Those points have coverage cur (0) and are beyond the maximum r_i? As before, they are not needed. So we can stop. But note: we didn't count points at the last_pos itself? In this method, before processing a group at position pos, we count from last_pos to pos-1. So at the start, we need an initial last_pos that is the first position? But before processing the first group, we haven't applied its deltas, so if we set last_pos = first position, then we would count 0 length before processing first group, then apply deltas, then set last_pos = first position. Then for next group, we count from first position to next-1, which includes first position. That's same as earlier. So we need to set last_pos = first position before loop? Let's simulate:
We can do:
sort ev.
cur = 0
// We'll treat that before first group, we need to have last_pos = ev[0].first and we haven't applied deltas yet? Actually we need after applying deltas at first position to count from that position to next-1. So we could:
// first, set last_pos = ev[0].first
last_pos = ev[0].first;
// apply all deltas at last_pos
i=0
while i < ev.size() && ev[i].first == last_pos:
cur += ev[i].second
i++
// Now we have cur for coverage at last_pos
// Then for each subsequent group:
while i < ev.size():
pos = ev[i].first
// Count points from last_pos to pos-1
ans[cur] += pos - last_pos
// Apply all deltas at pos
while i < ev.size() && ev[i].first == pos:
cur += ev[i].second
i++
last_pos = pos
// After loop, done.
This works without explicit combining. It's essentially the same but more efficient because we avoid extra vector. However, we need to be careful with indexing ans[cur] when cur could be 0. This method might be simpler.
vector ans(n+1, 0);
int cur = 0;
long long last_pos = ev[0].first; // will set after we apply deltas? Actually we need to apply first group's deltas first before counting, so we'll do:
size_t i = 0;
// Process first position group
long long cur_pos = ev[0].first;
while (i < ev.size() && ev[i].first == cur_pos) {
cur += ev[i].second;
i++;
}
last_pos = cur_pos;
// Now cur is coverage for points >= last_pos
while (i < ev.size()) {
long long pos = ev[i].first;
// count interval [last_pos, pos-1]
if (cur <= n) // but cur can be 0, which we ignore but we can still add to ans[0] if we have space; we'll just ensure ans size n+1, index 0 is available but we don't use.
ans[cur] += pos - last_pos;
// process all events at pos
while (i < ev.size() && ev[i].first == pos) {
cur += ev[i].second;
i++;
}
last_pos = pos;
}
// After loop, no need to count further.
But careful: At the start, after processing first group, we set last_pos = cur_pos. Then we go into while loop if i < ev.size(). In that loop, we first compute pos (next group). Then we add ans[cur] for points from last_pos to pos-1. This includes last_pos. That's correct because after applying first group deltas, coverage at last_pos is cur, and it stays until next event.
Now after processing the last group, i becomes ev.size(), loop ends. At that moment, we have last_pos = position of last group. Should we count points from that last_pos to ...? That would be points from last_pos to infinity. But coverage after last group should be 0, and those points are beyond max r_i. So we can ignore. However, it's possible that after processing last group, cur != 0? Could be if there is an event that only has starts but no ends? But the last event is the maximum coordinate among l_i and r_i+1. Since all segments have ends, the last event must be an r+1, which reduces cur. However, what if the last event is a start (l_i) and there is no corresponding r_i+1? That would mean there's a segment with l_i at the maximum coordinate, but then its r_i+1 would be > l_i, so r_i+1 would be even larger and be the maximum. So impossible. So after last group, cur = 0. So we don't need to count.
But note: There might be a scenario where some segments are [10^18, 10^18] -> r+1 = 10^18+1, which is > 10^18. So last event is 10^18+1, after processing it, cur becomes 0. The integer points from 10^18+1 onward are >10^18, not in our range, so ignore.
Thus algorithm is good.
But we need to be careful: If there is only one group? That would mean all events are at the same coordinate. But we have at least two events from each segment: l and r+1. If all segments have same l and same r+1, we have two distinct positions unless l == r+1? impossible. So ev has at least two distinct positions. However, it's possible that after sorting, all events are at the same coordinate? That would require all l_i and all r_i+1 equal to the same value. But l_i <= r_i, so if l_i = c, then r_i+1 = c implies r_i = c-1, but then l_i = c > r_i = c-1 contradicts l_i <= r_i. So impossible. So there are at least two distinct positions. So our loop will have i processing first group, then i < ev.size() for next groups, so while loop will execute at least once.
Now also need to handle the case where cur may become negative? Should not, because deltas are balanced: total sum of all deltas = 0. And we process in order, cur should never go negative if we apply starts before ends at the same position? At a given position, there could be both starts and ends. If we apply all deltas at once, the net delta could be negative? For example, a position that is both a start of some segments and end of others. The net could be negative if more ends than starts. But that's okay: cur could become negative if we apply all deltas together? However, in reality, at a given point x, coverage is defined for integer x. For a point that is both a start and an end, the segments that start at x should cover x, and segments that end at x-1? Wait, our representation: +1 at l, -1 at r+1. So at position x, we have starts (if x == l_i) and ends (if x == r_j+1). The ends correspond to segments that ended at r_j = x-1. For coverage at point x, the segments that end at r_j = x-1 should not cover x. So the decrement at x should apply before counting coverage at x. At a point x that is both a start and an end, we need to apply the decrements (for segments that ended at x-1) before applying starts? Or apply both? The coverage at x should include segments that start at x, and exclude those that ended at x-1. Since we define coverage as l_i ≤ x ≤ r_i, a segment that ends at x-1 does not cover x, and a segment that starts at x covers x. So the order at the same point: the decrement (for ending at x-1) should take effect before x, and the increment (for starting at x) should take effect at x. If we apply both deltas at the same time (as a net), the result is net change = (#starts - #ends). But will that accurately represent coverage at x? Let's check: Suppose we have one segment ending at x-1 (so -1 at x) and one segment starting at x (+1 at x). The net delta is 0. If we apply both at the same time before counting x, then cur remains unchanged. But what should be the coverage at x? Initially, before x, the segment ending at x-1 was active for points <= x-1. At x, that segment is no longer active. The segment starting at x becomes active. So net active count at x is (previous active - 1 + 1) = previous active. So indeed, cur remains the same. So applying net delta 0 yields correct coverage at x. So order doesn't matter for the net effect at x because both changes happen at the same point and the coverage at that point is after both changes? Actually we must decide: when we process events at position pos, we want coverage for points >= pos after applying both increments and decrements that are associated with that position. In our algorithm, we apply all deltas at that position before counting the interval that starts at that position. So that means at pos, we apply both starts and ends simultaneously, and then coverage for pos is current after that. Is that correct? For point pos, segments that start at pos should be included, segments that end at pos-1 should be excluded. So both changes are effective at pos. So applying them together is correct. So cur after applying all deltas at pos equals coverage at pos. That's fine. So cur should never become negative because coverage is non-negative. However, if we apply a large negative net delta without having enough active segments, cur could become negative temporarily if we don't have the proper order? But if we apply all deltas at once, net delta might be negative enough to make cur negative if there were more ends than starts and cur was small. But is that possible in a valid scenario? Consider at a position pos, there are some ends (decrements) and some starts (increments). At the point just before pos (i.e., pos-1), the active count should be such that the segments that end at pos-1 are active, and segments that start at pos are not yet active. When we move to pos, we need to remove those ending segments and add those starting segments. So cur_before (coverage at pos-1) includes the ending segments. After removing them, cur becomes cur_before - #ends. Then we add #starts to get cur_after = cur_before - #ends + #starts. Since cur_before >= #ends (because those ending segments were active), cur_before - #ends is >=0. Then adding #starts gives non-negative. So if we apply net delta directly: cur_after = cur_before + (#starts - #ends). Since cur_before >= #ends, we have cur_before + (#starts - #ends) >= #starts >= 0. So it's non-negative. So if we apply net delta, it should also be non-negative because cur_before is at least #ends. But our algorithm does not know cur_before? Actually we are applying net delta to cur (which is cur_before) to get cur_after. So as long as cur_before is correctly representing coverage at previous points, it should be >= #ends because those ending segments were active. However, could there be a scenario where at the same position, we have more ends than starts, and cur_before is less than #ends? That would imply that some segments that are supposed to end at pos-1 were not active before pos, which is impossible because if a segment ends at pos-1, it was active for all points up to pos-1, including pos-1. So cur_before must be at least the number of segments that end at pos-1. So it's safe. So applying net delta is okay, and cur will remain non-negative.
Thus algorithm is correct.
Now we need to be careful about large n and many events: sorting 400k pairs is okay.
Now finalize code.
But there might be an even more elegant approach: using a multiset? Not needed.
Now also note that answer values can be large, so use long long.
Potential pitfalls: In the loop, we add ans[cur] with pos - last. Since cur could be 0, we might be adding to ans[0] which we ignore. That's fine.
But we must ensure ans has size at least n+1, so indexing cur up to n is safe. But cur could be 0, so we need ans[0] as well to avoid out-of-range? We'll allocate n+1, indices 0..n. So ans[0] is available.
Now test with extreme values: n=200k, all segments [0, 10^18]. Then events: 0: +200k, 10^18+1: -200k. Process: cur=200k, last=0; i=1: ans[200k] += (10^18+1 - 0) = 10^18+1. That's fine.
Now consider that ans array is of long long, and adding such large numbers many times, but total sum <= 10^18+1, so fine.
Now confirm with sample2 using our algorithm:
ev: (1,+1),(4,-1),(2,+1),(5,-1),(5,+1),(8,-1). Sorted: (1,+1),(2,+1),(4,-1),(5,+1),(5,-1),(8,-1). Combine on the fly? Our algorithm groups them:
First group: pos=1: apply +1 => cur=1, last=1.
Next group: pos=2: count ans[1] += 2-1 =1 (point1). Apply +1 => cur=2, last=2.
Next group: pos=4: count ans[2] += 4-2 =2 (points2,3). Apply -1 => cur=1, last=4.
Next group: pos=5: count ans[1] += 5-4 =1 (point4). Apply +1 and -1 (both) net 0 => cur=1, last=5.
Next group: pos=8: count ans[1] += 8-5 =3 (points5,6,7). Apply -1 => cur=0, last=8.
Done. ans[1]=1+1+3=5, ans[2]=2. So output "5 2 0". Good.
Now edge: what if we have a segment [0,10^18] and another [10^18,10^18]? Actually second is a point at the end. Events: first: +1 at 0, -1 at 10^18+1; second: +1 at 10^18, -1 at 10^18+1. So events: 0:+1, 10^18:+1, (10^18+1):-1 (twice). Sorted: 0,1e18,1e18+1. Process:
first group: pos=0: cur=1, last=0.
second group: pos=1e18: count ans[1] += 1e18 - 0 = 1e18 (points 0..1e18-1). Wait careful: from last=0 to pos-1 = 1e18-1, that's 1e18 points. cur becomes after applying +1? Actually we haven't applied second group yet. So at this point, cur still 1. Then we apply delta at 1e18: +1 => cur=2, last=1e18.
third group: pos=1e18+1: count ans[2] += (1e18+1 - 1e18) =1 (point 1e18). Then apply -2 => cur=0.
So ans[1] = 1e18, ans[2] = 1. This seems correct: point 1e18 is covered by both segments, all others only by the first segment. Good.
Now consider overlapping at boundaries: For example, [0,5] and [5,10]. According to coverage, point 5 is covered by both? Because condition l_i ≤ x ≤ r_i, so if second segment starts at 5, then point 5 is covered by second segment as well as first segment (since first ends at 5). So point 5 is covered by both. Our events: first: +1 at0, -1 at6; second: +1 at5, -1 at11. Sorted: 0,5,6,11. Process:
first group: pos=0: cur=1, last=0.
second group: pos=5: count ans[1] += 5-0=5 (points0..4). cur += +1 => cur=2, last=5.
third group: pos=6: count ans[2] += 6-5=1 (point5). cur += -1 => cur=1, last=6.
fourth group: pos=11: count ans[1] += 11-6=5 (points6..10). cur += -1 =>0.
So ans[1]=5+5=10, ans[2]=1. That matches: point 5 covered by two segments, rest each by one. Good.
Now we must ensure that our algorithm doesn't miss the point at the last event? Not needed.
Now think about integer overflow when adding ans[cur] with (pos - last). We'll use long long for both. In C++, long long is typically 64-bit signed. Since max difference is up to 10^18+1, safe.
Now let's consider potential time: reading input, sorting vector of 400k pairs. Should be fine.
Now we should test a case with many segments that cause many event points (2n up to 400k). That's fine.
Now we should also ensure we handle negative coordinates? But constraints say l_i >= 0, so non-negative.
Now finalize solution.
Now I'd like to think about alternative approach: using a difference map and then prefix sums. But already done.
Now we should also ensure that we output exactly n numbers. If some cnt_i are 0, they print 0.
Now produce final answer.
But wait: There is a nuance: The problem states "for every k ∈ [1..n], calculate the number of points with integer coordinates such that the number of segments that cover these points equals k." It doesn't ask for points with zero coverage. So we ignore ans[0].
Now we might consider that ans array may be large (n up to 200k) and we need to initialize to zero. That's fine.
Now write code.
Potential issues: In the sweep method without explicit merging, we need to handle the first group specially. Implementation can be done elegantly:
vector<pair<long long, int>> events;
events.reserve(2*n);
for (int i=0; i<n; i++) {
long long l, r;
cin >> l >> r;
events.emplace_back(l, 1);
events.emplace_back(r+1, -1);
}
sort(events.begin(), events.end());
vector ans(n+1, 0);
int cur = 0;
long long last = events[0].first; // will be overwritten after processing first group? We'll process first group outside loop.
int i = 0;
// Process first position
long long cur_pos = events[0].first;
while (i < events.size() && events[i].first == cur_pos) {
cur += events[i].second;
i++;
}
last = cur_pos;
while (i < events.size()) {
long long pos = events[i].first;
// Count points from last to pos-1
if (cur <= n) // but cur could be > n? no, but safe
ans[cur] += pos - last;
// Process all events at pos
while (i < events.size() && events[i].first == pos) {
cur += events[i].second;
i++;
}
last = pos;
}
But we should also consider the case where after processing first group, there might be no more events? That would mean all events were at same position? Not possible as argued, but we can still handle: if i == events.size() after processing first group, then there are no further events. But then we would have processed only first group. However, we would not have counted any interval. But we need to count points from last (which is first position) to ...? The first position is l_min, and after processing first group, cur = number of segments starting at that point. But we need to count points from that position up to the next event - but there is no next event. That would mean there is no r+1? But every start has an end. Since we added both l and r+1 for each segment, and we have at least one segment, there must be an event for r+1. Unless all segments have r+1 equal to the same l_min? That would require l_min = r+1, impossible. So there must be at least two distinct positions. So i will not equal events.size() after first group because there is at least one more group. But to be safe, we could check and if i == events.size(), then we would need to count points from last to maybe some maximum? Actually if all events are at same position, then there is no interval; but that can't happen. So fine.
Now to be efficient, we could avoid special handling by using a more uniform approach:
Initialize cur = 0, last = events[0].first (the smallest coordinate). Then iterate through events in order, but we need to apply deltas before counting the interval that starts at that position? Alternative: Use the method where we count before update, but set last to a sentinel? Let's think of a simpler loop:
We can set last = events[0].first, and cur = 0. Then iterate over groups:
for (int j = 0; j < events.size(); ) {
long long pos = events[j].first;
// At this point, cur is the coverage for points >= last (since we've applied all previous deltas)
// Count points from last to pos-1 (if last < pos) - but at first iteration, last == pos, so no count.
ans[cur] += pos - last; // if cur is within bounds
// Now apply all deltas at pos
while (j < events.size() && events[j].first == pos) {
cur += events[j].second;
j++;
}
last = pos;
}
After loop, we might have to count points from last to ...? Actually after processing the last group, we have set last = pos, but there is no further event, so we should count the points from last to ...? But the coverage after last event is cur. But we need to count points up to max(r_i). However, after processing the last group, the last group corresponds to the maximum coordinate among events. For points from that coordinate onward, coverage is cur. But do we need to count them? The last coordinate is the maximum of all l_i and r_i+1. As argued, it's an r+1, and after processing, cur=0. So points from that coordinate onward have coverage 0. So we don't need to count. But if we were to count, we would need to know the end of domain. Since domain is only up to max(r_i), which is last-1? Actually if last is r_max+1, then points from last to r_max? r_max is last-1, so points from last onward start at r_max+1, which are not covered. So no need to count. So we can stop.
But careful: In the loop above, after processing the last group, we do last = pos. Then we exit loop. We haven't counted the interval from that last pos to some end. Since coverage after last pos is cur (0), and we don't care about coverage 0, it's fine to not count. But is there any scenario where after last group, cur > 0? Could the last event be a start? If there is a segment that starts at a very large coordinate and has no end? But we added its r+1, so there is always an end. So after processing the event with the largest coordinate, which is r_max+1, cur becomes 0. So it's fine.
Thus we can use a loop that at each group counts the interval from previous last to current position (exclusive), then updates cur, then sets last = current position. This works seamlessly, and we don't need special handling for first group if we set initial last to the first position and cur to 0? But we need to ensure that the coverage for the interval before the first event is not counted (since coverage 0). At first iteration, last = first position, cur = 0. Then we get to the first group: pos = first position. We compute ans[0] += pos - last = 0. That's fine. Then we apply deltas at first position, so cur becomes positive. Then we set last = first position. Then next iteration we will count from first position to next position-1 with cur positive. That will correctly include first position. So this uniform approach works and avoids special case. However, we must ensure that we have at least one event (we do). This is more elegant.
Let's test with earlier examples:
Example1: events sorted: (0,+1),(1,+1),(3,+1),(4,-1),(4,-1),(9,-1) -> after grouping we will process as:
Initialize last = 0, cur = 0.
First group: pos=0. ans[0] += 0-0=0. Apply deltas: cur = 1. last = 0.
Second group: pos=1. ans[1] += 1-0 =1. Apply deltas: cur = 2. last =1.
Third group: pos=3. ans[2] += 3-1 =2. Apply deltas: cur =3. last=3.
Fourth group: pos=4. ans[3] += 4-3 =1. Apply deltas: cur = 3 + (-2) =1. last=4.
Fifth group: pos=9. ans[1] += 9-4 =5. Apply deltas: cur = 1 + (-1) =0. last=9.
After loop, no further. So ans[1]=1+5=6, ans[2]=2, ans[3]=1. Works.
Example2: events sorted: (1,+1),(2,+1),(4,-1),(5,+1),(5,-1),(8,-1). Process:
last=1? Actually first event pos=1. So initialize last=1, cur=0.
Group1: pos=1, ans[0]+=0? Actually 1-1=0. Apply: cur=1, last=1.
Group2: pos=2, ans[1]+=2-1=1. Apply: cur=2, last=2.
Group3: pos=4, ans[2]+=4-2=2. Apply: cur=1, last=4.
Group4: pos=5, ans[1]+=5-4=1. Apply: cur=1+0=1? Actually at pos=5, we have +1 and -1, net 0, so cur stays 1. last=5.
Group5: pos=8, ans[1]+=8-5=3. Apply: cur=0, last=8.
Ans: [1]=1+1+3=5, [2]=2. Works.
Thus this uniform method is simpler: we don't need to treat first group separately. We just need to ensure we correctly group events with same position.
Implementation plan:
Read n.
Create vector events (pair<ll, int>). Use long long for coordinate, int for delta (could be large if many segments start/end at same point, but delta can be up to n (2e5) which fits in int. However, cur will be int, but we may need to store delta as int, but when we add to cur, cur can be up to n, so int is fine (since n <= 2e5). But delta could be negative, also within int range. So use int for delta.
For each segment: events.emplace_back(l, 1); events.emplace_back(r+1, -1). Note: r+1 might exceed 2^63? 1e18+1 fits.
Sort events.
Initialize vector ans(n+1, 0).
Process events:
int cur = 0;
long long last = events[0].first; // but we might also set last to the first event position after sorting, but careful: if we set last = events[0].first, we will later count from last to pos for the first group, which yields 0. But we need to set last before loop. We'll set last = events[0].first after sorting? Actually we can set last = events[0].first; but what if there are events with same first position? That's okay. Then we will iterate through groups.
size_t i = 0;
while (i < events.size()) {
long long pos = events[i].first;
// count from last to pos-1
ans[cur] += pos - last;
// process all events at this pos
while (i < events.size() && events[i].first == pos) {
cur += events[i].second;
++i;
}
last = pos;
}
But after loop, we might need to consider that after the last group, there could be points from last to something? As argued, not needed. However, what if after processing the last group, there are still points with positive coverage? That would mean the last event was not an r+1? Could the last event be a start? Suppose we have only one segment [0,10^18]. Events: (0,+1), (10^18+1,-1). Sorted: 0, 10^18+1. Process:
Initialize: last = 0, cur = 0.
First group: pos=0, ans[0] += 0-0=0; apply delta -> cur=1; last=0.
Second group: pos=10^18+1, ans[1] += (10^18+1 - 0) = 10^18+1; apply delta -> cur=0; last=10^18+1.
Loop ends. No further. Now note that ans[1] gets 10^18+1, but that includes point 10^18+1? Let's check: pos=10^18+1, last was 0 before this group. So ans[1] added (10^18+1 - 0) = 10^18+1 points from last=0 to pos-1 = 10^18. That's points 0..10^18 inclusive? Wait: The interval from last to pos-1 includes points from 0 to 10^18? Because pos-1 = 10^18, so the integer points included are 0,1,...,10^18. That's 10^18+1 points. That matches the coverage: the segment covers 0..10^18 (both inclusive), which is 10^18+1 points. So ans[1] correct. After this group, we set last = 10^18+1. Then loop ends. There is no further counting. So we don't count points from 10^18+1 onward, which have coverage 0. So fine.
Now consider scenario where after last group, last is set to the last position (which is r_max+1), and there are no further events. We don't count beyond. Good.
Now check with earlier example where there is a gap with uncovered points: segments [0,0] and [2,2]. Events: (0,+1),(1,-1),(2,+1),(3,-1). Sorted: 0,1,2,3.
Process:
last=0,cur=0.
Group1: pos=0, ans[0] +=0; apply +1 => cur=1; last=0.
Group2: pos=1, ans[1] += (1-0)=1 (points 0). apply -1 => cur=0; last=1.
Group3: pos=2, ans[0] += (2-1)=1 (point1). apply +1 => cur=1; last=2.
Group4: pos=3, ans[1] += (3-2)=1 (point2). apply -1 => cur=0; last=3.
End. ans[1]=1+1=2 (points 0 and 2). ans[0] got some, ignored. Correct.
Now check with nested segments overlapping at same start and end. Works.
Thus the uniform method is robust.
Now we must ensure that we handle the grouping correctly: while i < events.size() and events[i].first == pos, we accumulate cur.
Now there is a potential bug: The initial last set to events[0].first works fine because we count from that position to pos-1 where pos is the same, zero. But what if the smallest coordinate is not covered? But as argued, smallest is a start, so coverage after first group becomes positive. But in our algorithm, we start with cur=0, last=first_pos. That's fine because we count zero from last to first_pos-1 (nonexistent). Then we apply first group, cur becomes positive, set last = first_pos. Then next group we count from first_pos to next-1, which includes first_pos. So coverage at first_pos is counted.
Now what if the smallest coordinate is an r+1? It can't be as argued. So fine.
But just to be thorough, consider a degenerate case where there is a segment with l=5 and r=4? Not allowed because l <= r.
Thus algorithm stands.
Now let's consider memory usage: events vector of size 2n, each pair of long long and int, about 16 bytes each? ~6.4MB, fine.
Now we need to output as space-separated integers (long long).
Now we will code in C++.
Potential pitfalls: The answer array size is n+1, and we access ans[cur] when cur <= n. cur can be up to n, so ans[n] is valid. However, if cur is 0, ans[0] is valid but we don't output. So allocate n+1.
Now ensure we use long long for counting differences. In the addition ans[cur] += pos - last, pos and last are long long, difference is long long.
Now we need to be careful about reading input: n up to 2e5, so use scanf or fast cin with sync.
Now final code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
code
int n;
cin >> n;
vector<pair<ll, int>> events;
events.reserve(2 * n);
for (int i = 0; i < n; i++) {
ll l, r;
cin >> l >> r;
events.emplace_back(l, 1);
events.emplace_back(r + 1, -1);
}
sort(events.begin(), events.end());
vector<ll> ans(n + 1, 0);
int cur = 0;
ll last = events[0].first;
size_t i = 0;
while (i < events.size()) {
ll pos = events[i].first;
ans[cur] += pos - last;
// process all events at pos
while (i < events.size() && events[i].first == pos) {
cur += events[i].second;
i++;
}
last = pos;
}
for (int k = 1; k <= n; k++) {
cout << ans[k] << (k == n ? "\n" : " ");
}
return 0;
}
Test with examples:
Example1: Already tested mentally.
Example2: Works.
Now test with random small cases using brute force. Since I can't run code here, but reasoning seems solid.
Now consider n=2, segments: [0,1], [2,3] disjoint. events: (0,+1),(2,+1),(2? Actually first: [0,1] => +1 at0, -1 at2; second: [2,3] => +1 at2, -1 at4). So events: (0,+1),(2,-1),(2,+1),(4,-1). Sorted: (0,+1),(2,-1),(2,+1),(4,-1). Process: last=0,cur=0. Group1: pos=0, ans[0]+=0; cur=1; last=0. Group2: pos=2, ans[1]+=2-0=2 (points0,1). Process events at 2: first -1 then +1? Actually we process all: cur += -1 =>0, then +1 =>1. So cur becomes 1. last=2. Group3: pos=4, ans[1]+=4-2=2 (points2,3). cur += -1 =>0; last=4. Output ans[1]=4, ans[2]=0. That's correct: each point covered by exactly one segment.
Now consider overlapping at a single point: [0,2], [2,4]. Process earlier gave cnt1=5? Actually earlier we had [0,5] and [5,10] gave cnt1=10, cnt2=1. For [0,2],[2,4], points: 0,1 covered by first only; 2 covered by both; 3,4 covered by second only. So cnt1=4, cnt2=1. Let's test: events: first: +1 at0, -1 at3; second: +1 at2, -1 at5. Sorted: (0,+1),(2,+1),(3,-1),(5,-1). Process: last=0,cur=0. Group1: pos0, ans[0]+=0; cur=1; last=0. Group2: pos2, ans[1]+=2-0=2 (points0,1). cur: +1 =>2; last=2. Group3: pos3, ans[2]+=3-2=1 (point2). cur: -1 =>1; last=3. Group4: pos5, ans[1]+=5-3=2 (points3,4). cur: -1 =>0; last=5. So ans[1]=2+2=4, ans[2]=1. Good.
Now consider many segments with same start and end. Works.
Now check if ans[cur] indexing when cur > n? Not possible.
Now check if cur can become negative? As argued, no. But if there is a bug, it's safe to ensure we don't index negative. But cur is int, and we only index ans[cur] when cur is non-negative. Since it should be non-negative, it's fine.
Now ensure that we use long long for positions and ans.
Potential performance: While processing events, we have nested while loops; total operations O(2n). Good.
Now final answer is ready.
But there is one more nuance: The problem states "Some segments can degenerate to points." That's fine.
Now final code as above.
But I want to double-check the initial last assignment: We set last = events[0].first before loop. However, if events vector is empty? Not possible because n>=1.
What about the possibility that after sorting, the first event is not the smallest coordinate? Actually it is.
Now consider if there is a segment with l=0 and r=10^18, and another segment with l=10^18 and r=10^18. That we tested.
Now think about a scenario where there are many segments that cause cur to be very large, but that's fine.
Now let's test with a random case using a brute force for small ranges to ensure algorithm matches.
1: only seg1 -> 1
2: seg1, seg2 -> 2
3: seg1, seg2, seg3 -> 3
4: seg1, seg2, seg3? seg1 covers 4, seg2 covers 4, seg3 covers 3..6 so yes 4 covered by all three? seg3 covers 3-6, so 4 covered. So 4: 3
5: seg2 and seg3 -> 2
6: seg3 -> 1
So cnt1: points 1 and 6 -> 2
cnt2: points 2 and 5 -> 2
cnt3: points 3 and 4 -> 2
But wait, point 3 is covered by seg1? seg1 is [1,4] so yes; seg2 [2,5] yes; seg3 [3,6] yes -> 3. point 4 also 3. So cnt3=2. So total points =6. So answer should be: 2 2 2.
Now test scenario where intervals have huge gaps but we only care about points with coverage>0.
Now consider overlapping at many points: okay.
Now final code seems correct.
But there is one more nuance: The problem statement's sample output for first example is "6 2 1". We got that.
Second: "5 2 0". Good.
Now potential pitfalls: Overflow in r+1 when r=10^18. 10^18+1 fits within 64-bit signed (max ~9.22e18). So safe.
Now we should use long long. In C++, long long is at least 64-bit.
Now additional consideration: The events vector might contain duplicates, but our grouping handles them.
Now we also need to ensure that we use .reserve to avoid reallocation.
Now finalize.
But there might be a more efficient way using only one pass and counting length between events, but we already have that.
Now think about memory usage of ans: n+1 long longs, ~1.6 MB for n=2e5, fine.
Now we can provide final solution.
But wait: There is a subtlety: The points we count are integer points. In our method, we count from last to pos-1 inclusive. This counts pos - last points. That is correct only if last and pos are integers. They are integers because l_i and r_i+1 are integers (since l_i and r_i are integers). So all event positions are integers. So difference is integer count of points.
Now confirm that we are not double counting or missing points at boundaries: For each pair of consecutive event positions p_i and p_{i+1}, we count points p_i, p_i+1, ..., p_{i+1}-1 exactly once when we process p_{i+1} as the current pos. Because at that time, last = p_i (the previous event position). So we add p_{i+1} - p_i points. This includes p_i (since p_i is the start of interval) and excludes p_{i+1}. Then after updating, we set last = p_{i+1}, and later when we encounter next event, we count from p_{i+1} to that next-1, which includes p_{i+1} at that time. So all points are covered exactly once, partitioned by event positions. The points at the very first event position are included in the first interval after it becomes last? Actually at the start, last = first event position, cur is still 0. Then we process first event: we add ans[0] += pos - last = 0. Then we update cur and set last = first event position. Then when we process second event, we add from last (first event) to second-1, which includes the first event position. So yes, first event position is counted with coverage after applying first events.
Thus all integer points that lie between the smallest event position and the largest event position-1 are counted exactly once. Points smaller than smallest event position are never counted because they are uncovered (coverage 0). Points >= largest event position are uncovered (coverage 0). So we have counted all points with coverage >=1.
Thus correct.
Now I think solution is done.
Now just produce final answer.
But before final, let's think about possible integer overflow when adding to ans[cur] if ans[cur] accumulates many intervals. The total sum of ans[1..n] is at most number of integer points in [0, 10^18], which is 10^18+1. So ans[cur] each <= that, safe.
Now we must ensure that we use 64-bit for ans values. In C++, we can use long long. All good.
Now I'll produce final answer with code.
But wait: The input constraints: l_i, r_i up to 10^18, so r_i+1 could be 10^18+1, but we store as long long, okay.
Now let's write code with comments.
Potential improvements: Use faster I/O.
Now answer.
But there is another approach: using a map to store deltas and then iterate. But our vector approach is fine.
Now write final solution.
Note: Some solutions in contests use a technique of "coordinate compression" but we already effectively do that by sorting event positions.
Now final code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
code
int n;
cin >> n;
vector<pair<ll, int>> ev;
ev.reserve(2 * n);
for (int i = 0; i < n; ++i) {
ll l, r;
cin >> l >> r;
ev.emplace_back(l, 1);
ev.emplace_back(r + 1, -1);
}
sort(ev.begin(), ev.end());
vector<ll> ans(n + 1, 0);
int cur = 0;
ll last = ev[0].first;
size_t i = 0;
while (i < ev.size()) {
ll pos = ev[i].first;
ans[cur] += pos - last;
while (i < ev.size() && ev[i].first == pos) {
cur += ev[i].second;
++i;
}
last = pos;
}
for (int k = 1; k <= n; ++k) {
cout << ans[k] << " \n"[k == n];
}
return 0;
}
We might want to ensure that cur never exceeds n, but it's fine.
Now let's test with the samples.
Sample1: Input:
3
0 3
1 3
3 8
Our code: events: (0,1),(4,-1),(1,1),(4,-1),(3,1),(9,-1). Sorted: (0,1),(1,1),(3,1),(4,-1),(4,-1),(9,-1). Process as earlier, output "6 2 1". Good.
Sample2: Input:
3
1 3
2 4
5 7
Events: (1,1),(4,-1),(2,1),(5,-1),(5,1),(8,-1). Sorted: (1,1),(2,1),(4,-1),(5,1),(5,-1),(8,-1). Process as earlier, output "5 2 0". Good.
Now check for potential error: When we do ans[cur] += pos - last, we should ensure that cur is within bounds. But cur can be 0 (covered), and ans[0] is valid. That's fine.
Now check for scenario where cur > n? If many segments start at same point, cur could be up to n, so cur <= n. So ans index safe.
Now about reading: use cin >> l >> r, but l and r are up to 1e18, so long long.
Now all set.
Now we should also consider if there is any chance that the total number of points with coverage >=1 exceeds 2^63-1? It can't because maximum distinct integer points in [0,1e18] is 1e18+1, which is less than 2^63-1. So safe.
Now final answer.
But I recall that in some problems, they use 64-bit unsigned for large counts, but long long signed is sufficient.
Now produce final solution.
Let's think about if we need to handle cases where events list is huge due to many distinct positions, but 400k is fine.
Now finalize.
But there is one more nuance: The problem expects output "cnt_1 cnt_2 ... cnt_n". In sample they have trailing space? Not important.
Now I'll output with spaces.
Now I'll provide the answer.
But wait: I'd like to re-check with a scenario where there is a segment with l=0 and r=10^18, and another segment with l=10^18 and r=10^18. In that case, the total points with coverage 1 is 10^18 (0..10^18-1) plus maybe? Actually point 10^18 is covered by both segments, so coverage 2. So cnt1 = 10^18, cnt2 = 1. Our algorithm produced ans[1] = 10^18+1? Let's recompute: events: (0,+1),(1e18+1,-1) and (1e18,+1),(1e18+1,-1). Sorted: (0,+1), (1e18,+1), (1e18+1,-1), (1e18+1,-1). Process:
last=0,cur=0.
Group1: pos=0, ans[0]+=0; cur=1; last=0.
Group2: pos=1e18, ans[1] += 1e18 - 0 = 1e18 (points 0..1e18-1). cur +=1 =>2; last=1e18.
Group3: pos=1e18+1, ans[2] += (1e18+1 - 1e18) = 1 (point 1e18). cur += -2 =>0; last=1e18+1.
So ans[1]=1e18, ans[2]=1. That's correct: points 0..1e18-1 are 1e18 points (since 0 to 999,999,999,999,999,999? Actually number of integers from 0 to 1e18-1 is 1e18). So ans[1]=1e18, not 1e18+1. Wait earlier I computed ans[1]=1e18+1 incorrectly. Because total points from 0 to 1e18 inclusive is 1e18+1. But point 1e18 is covered by both, not just one. So ans[1] should be 1e18. Yes correct. So algorithm is correct.
Now consider if there is a segment covering the entire range [0,10^18] only. Then ans[1] = 10^18+1. Our algorithm for single segment gave ans[1] = 1e18+1. Let's test: events: (0,+1),(1e18+1,-1). Process:
last=0,cur=0.
Group1: pos=0, ans[0]+=0; cur=1; last=0.
Group2: pos=1e18+1, ans[1] += 1e18+1 - 0 = 1e18+1; cur=0; last=1e18+1.
So ans[1]=1e18+1. That's correct.
Thus algorithm yields correct.
Now consider segment that is a point at 0 and a segment covering [0,10^18]: [0,0] and [0,10^18]. Then coverage: point 0 covered by both (2), points 1..10^18 covered by 1. So cnt1=10^18, cnt2=1. Events: first: (0,+1),(1,-1); second: (0,+1),(1e18+1,-1). Sorted: (0,+2),(1,-1),(1e18+1,-1). Process:
last=0,cur=0.
Group1: pos=0, ans[0]+=0; cur=2; last=0.
Group2: pos=1, ans[2] += 1-0=1 (point0). cur += -1 =>1; last=1.
Group3: pos=1e18+1, ans[1] += (1e18+1 - 1) = 1e18 (points1..1e18). cur += -1 =>0; last=1e18+1.
Result: ans[2]=1, ans[1]=1e18. Good.
Now think about cases where there are many zero delta events and they cause many groups; still fine.
Now finalize.
But there is one more nuance: The problem says "points with integer coordinates". They could be negative? But constraints say l_i >= 0, so non-negative. So we are fine.
Now, I'll write the solution with comments for clarity.
Now final answer. ```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
code
int n;
cin >> n;
vector<pair<ll, int>> events;
events.reserve(2 * n);
for (int i = 0; i < n; ++i) {
ll l, r;
cin >> l >> r;
// +1 at the start, -1 just after the end
events.emplace_back(l, 1);
events.emplace_back(r + 1, -1);
}
sort(events.begin(), events.end());
// ans[k] = number of points covered by exactly k segments
vector<ll> ans(n + 1, 0);
int cur = 0; // current number of active segments
ll last = events[0].first; // previous event coordinate
size_t i = 0;
while (i < events.size()) {
ll pos = events[i].first;
// all integer points from last to pos-1 have coverage = cur
ans[cur] += pos - last;
// apply all events at this position
while (i < events.size() && events[i].first == pos) {
cur += events[i].second;
++i;
}
last = pos;
}
for (int k = 1; k <= n; ++k) {
cout << ans[k] << (k == n ? "\n" : " ");
}
return 0;