A mobile phone company ACMICPC (Advanced Cellular, Mobile, and Internet-Connected Phone Corporation) is planning to set up a collection of antennas for mobile phones in a city called Maxnorm. The company ACMICPC has several collections for locations of antennas as their candidate plans, and now they want to know which collection is the best choice.
for this purpose, they want to develop a computer program to find the coverage of a collection of antenna locations. Each antenna Ai has power ri, corresponding to "radius". Usually, the coverage region of the antenna may be modeled as a disk centered at the location of the antenna (xi, yi) with radius ri. However, in this city Maxnorm such a coverage region becomes the square [xi − ri, xi + ri] × [yi − ri, yi + ri]. In other words, the distance between two points (xp, yp) and (xq, yq) is measured by the max norm max{ |xp − xq|, |yp − yq|}, or, the L∞ norm, in this city Maxnorm instead of the ordinary Euclidean norm √ {(xp − xq)2 + (yp − yq)2}.
As an example, consider the following collection of 3 antennas
4.0 4.0 3.0
5.0 6.0 3.0
5.5 4.5 1.0
depicted in the following figure
where the i-th row represents xi, yi ri such that (xi, yi) is the position of the i-th antenna and ri is its power. The area of regions of points covered by at least one antenna is 52.00 in this case.
Write a program that finds the area of coverage by a given collection of antenna locations.
Input
The input contains multiple data sets, each representing a collection of antenna locations. A data set is given in the following format.
n
x1 y1 r1
x2 y2 r2
. . .
xn yn rn
The first integer n is the number of antennas, such that 2 ≤ n ≤ 100. The coordinate of the i-th antenna is given by (xi, yi), and its power is ri. xi, yi and ri are fractional numbers between 0 and 200 inclusive.
The end of the input is indicated by a data set with 0 as the value of n.
Output
For each data set, your program should output its sequence number (1 for the first data set, 2 for the second, etc.) and the area of the coverage region. The area should be printed with two digits to the right of the decimal point, after rounding it to two decimal places.
The sequence number and the area should be printed on the same line with no spaces at the beginning and end of the line. The two numbers should be separated by a space.
We are given n antennas, each covering an axis-aligned square: [xi - ri, xi + ri] × [yi - ri, yi + ri] in L∞ metric. We need to compute the total area of the union of these squares. Input coordinates and radii are fractional numbers between 0 and 200 inclusive, n up to 100. Output area rounded to two decimals, with dataset numbering.
We need to compute union area of axis-aligned squares. Simple approach: use plane sweep or inclusion-exclusion? n=100, coordinates up to 200, radii up to 200 maybe; squares can be large. But we can compute union area by using a sweep line algorithm: sort by x coordinates of edges, then for each x-interval compute the total length of union of intervals in y. Since squares are axis-aligned, we can treat them as rectangles (but squares in L∞, they are axis-aligned squares indeed). So each antenna defines a rectangle with xmin = xi - ri, xmax = xi + ri, ymin = yi - ri, ymax = yi + ri. So we have n axis-aligned rectangles. Compute area of union.
We could use line sweep: sort unique x-coordinates of all rectangle left and right edges. For each adjacent x-interval [x_i, x_{i+1}], find all rectangles that cover that vertical strip, then compute the total length of the union of their y-intervals over that strip. Since the rectangles don't change within that interval (only the set of active rectangles is constant between events), we can multiply width times union length. Complexity: O(n^2) in terms of sorting events (2n unique x coords up to 200). But need careful handling of floating points.
Because coordinates are fractional, we cannot rely on integer grid. But we can treat them as double. Sorting up to 200 unique x's. Then for each interval, we need to compute union length of intervals from active rectangles. Since active set can be up to n, we can sort the y intervals (each active rectangle contributes [ymin, ymax] constant across the strip). Compute union length by merging these intervals. That's O(n log n) per strip, and O(n) strips O(n^2 log n) worst-case: 2n x-coords => ~200 strips, n=100, so 200100 log 100 = 200100*~7 = 140k, fine.
But careful: the x-intervals width may be zero if duplicate x coordinates, then skip.
So implement: For each dataset, read n, if n==0 break. For each antenna, parse x,y,r. Compute x1=x-r, x2=x+r, y1=y-r, y2=y+r. Store rectangles.
Collect all x coordinates (x1 and x2) into list xs. Sort, unique (floating). But due to floating errors, we might have very close values. To be safe, we can sort and then merge if difference < epsilon, but given data likely exact with one decimal? But they are fractional numbers, could be like 0.333? Since they are between 0 and 200, we don't know format. Better to keep all distinct as they appear; but duplicates from same x might cause issues if we treat them as separate causing zero width intervals, we can handle width zero by skipping. To be robust, we can use set but floating points keys can cause issues. Instead, we can sort vector and then use a tolerance to treat as same? But might cause errors in area if we merge coordinates that are not exactly same. Probably simplest: store all x values as double, sort, then while scanning we consider each consecutive pair (xs[i], xs[i+1]) as an interval. If they are equal (difference < 1e-12) skip. This yields O(2n) intervals. However, if there are multiple edges at same x, the active set changes at that x, but the width of interval between that x and next x (which might be same if multiple same x) will be zero, so no area contribution. So we can just sort and keep duplicates? Actually, for sweep line typical approach: we need to know the active set between two distinct x positions. The events occur at each distinct x coordinate. So we should merge duplicate x values to get unique events. But careful: The active set may change at a particular x coordinate. If there are both left and right edges at the same x, the set after processing all events at that x (right edges remove before left edges? Actually order matters: at exactly the x coordinate, the rectangle should be considered covering that vertical line? For area computation using sweep over intervals, we treat intervals as half-open? The standard method: area = ∫ from x_min to x_max (union length at x) dx. If we have a rectangle that starts at x1 and ends at x2, it is active for x in (x1, x2) but at the boundaries the measure of a line is zero, so it doesn't matter if we include endpoints. So we can define active set for an interval (x_i, x_{i+1}) as all rectangles with x1 < x_{i+1} and x2 > x_i? Actually typical algorithm: sort all unique x coordinates from left/right edges. Then for each interval between two consecutive unique x's, we consider the mid x (or treat as constant). All rectangles whose x1 <= left_x and x2 >= right_x are active? But careful: If a rectangle starts exactly at left_x, it should be active in the interval if left_x is the left edge? Actually if we define the interval as [x_k, x_{k+1}], and we include the left boundary but not right? Let's think: The union area of rectangles can be computed as sum over k of (x_{k+1} - x_k) * length_of_union_of_y_intervals for that strip. The set of rectangles that cover the strip (x_k, x_{k+1}) is exactly those with x1 < x_{k+1} and x2 > x_k. Since the width is positive, it doesn't matter if we include equality. So we can simply for each interval, test each rectangle: if rect.x1 < x_{k+1} and rect.x2 > x_k, then it's active. That avoids needing to manage events per x coordinate. This is simpler: we generate all unique x coordinates from both x1 and x2, sort them. Then for each consecutive pair (x_left, x_right) where x_right > x_left, compute width = x_right - x_left. For that strip, we need the union length of the y-intervals of rectangles that cover the entire strip? Wait, do they cover the whole strip? A rectangle covers a point (x,y) if x1 ≤ x ≤ x2 and y1 ≤ y ≤ y2. For the strip between x_left and x_right, the rectangle covers any point with x in that interval? Actually, the rectangle covers all points (x,y) where x ∈ [x1,x2] and y ∈ [y1,y2]. So if the rectangle's x-interval overlaps the strip [x_left, x_right], then for any x in that strip, the rectangle covers the vertical segment [y1,y2] provided that the rectangle's x-interval includes that x. However, to compute the union area, we integrate over x. At each x, the union of y intervals is the union of [y1,y2] for rectangles that contain that x. But if we take the entire strip and treat the set of rectangles that overlap the strip, the union of y intervals may not be constant across the strip: because some rectangles might start or end within the strip. For example, if a rectangle's left edge is at x = x_mid inside the strip, then for x left of x_mid, that rectangle is not active; for x right of x_mid, it is active. Therefore the set of active rectangles changes within the strip if there is an edge inside it. However, our strip is defined between consecutive unique x coordinates; by construction, there is no rectangle edge strictly between x_left and x_right because all edges are at the x coordinates in the list. Since we included all left and right x coordinates, any rectangle edge occurs exactly at one of these x values. Therefore, in the open interval (x_left, x_right), there is no edge; the set of rectangles covering any x in that interval is constant. Yes, because any rectangle's x interval either starts at some x in the list or ends at some x in the list. Between two consecutive distinct x coordinates, no rectangle has its left or right boundary. So the set of rectangles whose x-interval contains a given x in (x_left, x_right) is exactly those with x1 ≤ x_left? Wait careful: If x1 equals x_left, then for x > x_left (and < x_right), the rectangle is active. For x just greater than x_left, it's active. At x = x_left exactly, it's boundary but not needed. Similarly, if x2 equals x_right, then for x < x_right, rectangle is active. So in the interior (x_left, x_right), the condition is: rectangle is active if x1 < x_right and x2 > x_left. Actually, for any x in (x_left, x_right), we need x1 ≤ x and x ≤ x2. Since x is between x_left and x_right, x1 ≤ x is equivalent to x1 < x_right (because if x1 = x_right? Not possible because x_right is greater than x_left and if x1 = x_right, then x1 would be greater than x_left, but then x1 is an edge at x_right, but then rectangle would not be active because x must be < x_right? Actually, if x1 = x_right, then rectangle starts exactly at the right boundary, so for any x < x_right, it's not active; so indeed x1 < x_right needed. Similarly, x2 > x_left needed. So the active set for strip (x_left, x_right) is rectangles with x1 < x_right and x2 > x_left. Since we know there are no edges in between, this set is constant. So we can compute union of y intervals of these rectangles, and multiply by width. Good.
Thus algorithm:
For each dataset:
Read n.
For i in 1..n: read xi, yi, ri, compute x1=xi-ri, x2=xi+ri, y1=yi-ri, y2=yi+ri.
Store rectangles as (x1,x2,y1,y2).
Collect all x coordinates: all x1 and x2. Put into vector xs.
Sort xs.
Optionally, we might want to remove duplicates? If duplicates exist, then there will be intervals of zero width. We can skip intervals where width is zero. However, to keep the active set constant, we need to ensure that there is no edge between x_left and x_right; if we leave duplicates, then consecutive duplicates produce zero width intervals, which we skip anyway. But also if we have duplicate x values, the sorted xs will have repeated values. For example, xs = [1,1,2]. Consecutive pairs: (1,1) width zero skip; (1,2) width positive, but note there is an edge at x=1; but there might be two edges at same x but that x is the left boundary. In the interval (1,2), the set of rectangles is those with x1 < 2 and x2 > 1. That's correct. However, if we had duplicates but we didn't remove them, the active set for interval (1,2) remains constant because any rectangle edge at x=1 is at the left endpoint, not inside. That's fine. The only issue is that we might have extra zero-width intervals, but they don't affect area. So we can keep duplicates; less code. But we need to ensure that for each consecutive pair we compute width > 0. If width is zero, continue.
Then area = 0.
For i from 0 to len(xs)-2:
x_left = xs[i], x_right = xs[i+1]
if x_right - x_left < 1e-12: continue (or use <=0)
width = x_right - x_left
// collect active rectangles: those with rect.x1 < x_right and rect.x2 > x_left
vector intervals; for each rectangle if (rect.x1 < x_right && rect.x2 > x_left) then add (rect.y1, rect.y2)
// compute union length of y intervals
sort intervals by y1 (or by y1 then y2)
merge overlapping intervals: initialize cur = intervals[0]; for each next, if next.y1 <= cur.y2 then cur.y2 = max(cur.y2, next.y2); else add cur length to total, set cur = next. Finally add last length.
// sum lengths = total_y_union
area += width * total_y_union
Output dataset index and area rounded to 2 decimal places.
Complexities: O(K * n log n) where K = 2n (max 200). That's fine.
Precision: We need area with two decimals. Use double, then round: printf("%d %.2f", case, area) or cout with fixed setprecision(2). That will round. However, double errors might accumulate. Given coordinates up to 200, width and lengths up to 400, area up to maybe 160000? Actually max area union maybe up to 400*400=160000. So double should be fine.
But consider that we use "rect.x1 < x_right" condition, not <=, for active. This ensures that rectangles that start exactly at x_right are not included, which is correct. Similarly, rectangles that end exactly at x_left are not included. At the endpoints, no contribution. That's fine.
Edge Cases: Rectangles with negative coordinates? Since xi,ri between 0 and 200, xi-ri could be negative, down to -200. So union area might extend beyond 0? That's allowed. So x coordinates can be negative. No constraint that squares are within positive region. So fine.
But the union area might be computed as integral over all x where there is coverage. Our xs list includes all left and right edges, so coverage outside the min and max edges? Actually if we have a rectangle with x1 = -10, x2 = 30, then xs includes -10 and 30. The leftmost x coordinate among all rectangles will be the start of the first interval? Our intervals are defined between consecutive xs values. However, the union of rectangles may not cover all x between the smallest and largest xs; but the active set computed as described will correctly compute zero length for intervals where no rectangles cover. For an interval between two edges where no rectangle covers, the active set will be empty (since no rectangle satisfies x1 < x_right and x2 > x_left? Actually if there is a gap, say two disjoint rectangles, there will be an x interval between the right edge of first and left edge of second. In that gap, xs includes both edges. For an interval (x_right_of_first, x_left_of_second), the condition: any rectangle must have x1 < x_right and x2 > x_left. For the first rectangle: x2 (right edge) is equal to x_left_of_gap? Actually the right edge of first is at some x = a. The next x in xs might be a (if only that) or maybe a is included; but because we include all edges, after a, the next coordinate is b (left edge of second). For interval (a, b): first rectangle's x2 = a, which is not > x_left = a? condition x2 > a? Since x2 = a, x2 > a is false, so not included. Second rectangle's x1 = b, condition x1 < x_right = b? x1 = b, x1 < b false, so not included. So active set empty, total_y_union = 0, area contribution zero. Good.
Thus the algorithm works.
But we need to consider if we need to merge overlapping y intervals correctly. Sorting intervals by y1, then merging.
Now, we also need to consider if there are any degenerate rectangles (ri=0)? Then x1=x2, y1=y2. That's a line or point, area zero. Our condition x1 < x_right and x2 > x_left might not hold for any positive width interval because x1=x2. If x1 equals some x coordinate, and we consider an interval where x_left < x1 < x_right, but if x1=x2, then the rectangle covers only x = exactly that value. Since width of interval is positive, no point with x exactly that line (measure zero) so no area contribution. So we can ignore. Our condition would require x1 < x_right and x2 > x_left. For a point at x0, if we have an interval (x_left, x_right) that contains x0, then x1=x0 < x_right true, x2=x0 > x_left true, so it would be considered active. But then the rectangle's y-interval is [y1, y2] where y1=y2 also maybe a point. That contributes zero length in y, so union length unaffected. So fine.
Implementation details:
Use vector with double x1,x2,y1,y2.
xs: vector xs; for each rect: xs.push_back(rect.x1); xs.push_back(rect.x2);
sort xs.
Then loop i from 0 to xs.size()-2:
double a = xs[i], b = xs[i+1];
if (b - a < 1e-12) continue; // or <= 0
width = b - a;
vector<pair<double,double>> y_intervals;
for each rect:
if (rect.x1 < b && rect.x2 > a) // careful: should we use <=? Let's reason: For x in (a,b). Since a and b are from edges, and a < b. For rect that has left edge exactly at a: rect.x1 = a. For any x in (a,b), x > a, so rect.x1 <= x holds. Condition rect.x1 < b is true (since a < b). Condition rect.x2 > a is true (since rect.x2 >= a? Actually if rect.x2 = a? That would be a rectangle of zero width? But if rect.x2 = a, then it's a line at a. For x in (a,b), x > a, so rect.x2 = a < x, so not cover. Condition rect.x2 > a? if rect.x2 = a, then rect.x2 > a is false, so not active. Good. For rect with right edge exactly at b: rect.x2 = b. For x in (a,b), x < b, condition rect.x2 > a true if b > a, which is true, and rect.x1 < b? rect.x1 < b true. So it's active. So condition works.
Then add (rect.y1, rect.y2) to list.
// Might need to treat degenerate y intervals? But they don't affect length.
Sort y_intervals by first then second.
Merge:
double total_y = 0;
if (y_intervals empty) skip.
sort(y_intervals.begin(), y_intervals.end());
double cur_l = y_intervals[0].first, cur_r = y_intervals[0].second;
for (int j=1; j< y_intervals.size(); ++j) {
double l = y_intervals[j].first, r = y_intervals[j].second;
if (l <= cur_r) { // overlap or touch? If intervals just touch at a point? Since area contribution from y length, touching at a point doesn't add area; but if they touch exactly, the union length is the same as if they are separate? Actually if two intervals are [0,1] and [1,2], the union covers [0,2] but the measure of the point 1 is zero; however, does the union length count as 2? The union of intervals [0,1] and [1,2] is [0,2] as a set; its total length is 2 (since the point at 1 doesn't affect length). So merging when l <= cur_r is appropriate: if l == cur_r, then total length from cur_l to max(cur_r, r) = (cur_r - cur_l) + (r - l) but overlapping at point yields continuous interval length = max(cur_r, r) - cur_l. That's correct. However, if we treat as separate we would get (cur_r - cur_l)+(r - l) = (cur_r - cur_l)+(r - l). If cur_r == l, that sum equals (cur_r - cur_l)+(r - l) = (cur_r - cur_l)+(r - cur_r) = r - cur_l, which equals max(cur_r,r)-cur_l. So same. So merging with <= works fine.
cur_r = max(cur_r, r);
} else {
total_y += cur_r - cur_l;
cur_l = l; cur_r = r;
}
}
total_y += cur_r - cur_l;
area += width * total_y;
Potential issues: floating errors in sorting? For intervals, we sort by y1. If two intervals have y1 exactly equal, we need consistent ordering; std::sort will work.
Precision: The area may be a double; we print with two decimals rounding. Example: 52.00. So use printf("%d %.2f\n", casenum, area); or cout << fixed << setprecision(2) << casenum << " " << area << endl;.
Now test on sample:
First dataset:
3 rectangles:
(4,4,3) => square from 1 to 7 in x, 1 to 7 in y => [1,7]x[1,7]
(5,6,3) => [2,8]x[3,9]
(5.5,4.5,1) => [4.5,6.5]x[3.5,5.5]
Union area? According to problem, 52.00. Let's compute manually: The union of first two squares: first 6x6=36, second 6x6=36, union might be area = sum - overlap. Overlap area: intersection of [1,7]x[1,7] and [2,8]x[3,9] is [2,7]x[3,7] = width5, height4 =20. So union=36+36-20=52. The third square is inside union? Actually third is [4.5,6.5]x[3.5,5.5] which is inside the union? Overlaps with both? Its area 2x2=4. But already covered? Actually union area of first two is 52, third is within that union? Let's check: [4.5,6.5] is within [1,7] and [2,8]? yes, x from 4.5 to 6.5 is subset of [1,7] and [2,8]; y from 3.5 to 5.5 is subset of [1,7] and [3,9]. So third is fully covered, union remains 52. So algorithm should compute 52.
Second dataset: two squares: (3,3,3) => [0,6]x[0,6] area 36; (1.5,1.5,1) => [0.5,2.5]x[0.5,2.5] area 4. But the small square is entirely inside the big one? Big square from 0 to 6 covers it. So union=36. Output 36. Good.
Thus algorithm works.
But what about more complex shapes? The union area computed via sweep is exact (subject to double precision) for axis-aligned rectangles.
Now, confirm constraints: n up to 100, input numbers up to 200. The number of unique x coordinates could be up to 200. So loops fine.
Edge Cases: Overlapping at edges, zero width rectangles, etc.
Now implement.
But wait: There is nuance: In the sweep method described, we need to consider if there is any change in the set of active rectangles inside the interval due to x-edges that coincide exactly with either endpoint. Our condition rect.x1 < x_right and rect.x2 > x_left ensures that if a rectangle starts at x_right, it is not included; if it ends at x_left, not included. For the rectangle that starts at x_left: condition rect.x1 = x_left. Since x_left < x_right, rect.x1 < x_right holds; rect.x2 > x_left holds as long as rect.x2 > x_left. So it's included. For rectangle that ends at x_right: rect.x2 = x_right, rect.x2 > x_left holds because x_right > x_left; rect.x1 < x_right holds, so included. That matches desired: active in the interior. Good.
Now about sorting y intervals: we need to handle potential negative y values. Sorting works.
Potential issue: If we have many xs duplicates, the number of intervals could be up to 200, but if many duplicates, the actual distinct might be fewer. But not problem.
We should also consider that for large n (100) the number of intervals (2n) is 200, and for each we loop through all rectangles (100) to find active ones, and then sort active y intervals (up to 100). So total operations ~200100 + 200100 log 100 ~ 20k+ 2001007=140k, trivial.
Implementation steps in C++:
struct Rect { double x1, x2, y1, y2; };
while(cin >> n, n!=0) {
caseNum++;
vector rects(n);
vector xs;
for i=0..n-1:
double x,y,r; cin >> x >> y >> r;
double x1 = x - r, x2 = x + r, y1 = y - r, y2 = y + r;
rects[i] = {x1,x2,y1,y2};
xs.push_back(x1); xs.push_back(x2);
sort(xs.begin(), xs.end());
double area = 0.0;
for (size_t i = 0; i+1 < xs.size(); ++i) {
double xl = xs[i], xr = xs[i+1];
if (xr - xl < 1e-12) continue;
vector<pair<double,double>> y_intervals;
for (const Rect& r : rects) {
if (r.x1 < xr && r.x2 > xl) { // active
y_intervals.emplace_back(r.y1, r.y2);
}
}
if (y_intervals.empty()) continue;
sort(y_intervals.begin(), y_intervals.end());
double total_y = 0;
double cur_l = y_intervals[0].first, cur_r = y_intervals[0].second;
for (size_t j = 1; j < y_intervals.size(); ++j) {
double l = y_intervals[j].first, r = y_intervals[j].second;
if (l <= cur_r) { // overlapping or touching
if (r > cur_r) cur_r = r;
} else {
total_y += cur_r - cur_l;
cur_l = l; cur_r = r;
}
}
total_y += cur_r - cur_l;
area += (xr - xl) * total_y;
}
printf("%d %.2f\n", caseNum, area);
}
Now, is there any risk that area might be slightly off due to floating errors in condition? Possibly, but given the numbers are not pathological, it's fine.
But we should check for large values: For extreme cases, x coordinates up to 400 (since xi+ri up to 400) and y up to 400. The total union area could be up to (400-(-200)? Actually xi between 0 and 200, ri up to 200, so min x1 = -200, max x2 = 400, range up to 600. So area up to 600^2 = 360000. So within double.
Now, what about performance? Good.
Now, are there any corner cases with overlapping intervals exactly? The sorting of y intervals: When intervals have same start, we sort by end. That's fine.
Now, the problem statement says the input contains multiple data sets, each representing a collection of antenna locations. The end is 0. Output for each dataset: sequence number and area with two decimal places. Example uses integer numbers but they have .0. So output format: "1 52.00". Good.
Potential pitfalls: The input numbers are fractional, we must read as double. There might be trailing spaces. Use cin >> double.
Our algorithm: For first dataset:
xs: from rect1: 1.0,7.0; rect2:2.0,8.0; rect3:4.5,6.5 => xs = [1,2,4.5,6.5,7,8] sorted: 1,2,4.5,6.5,7,8.
Intervals:
(1,2): width=1. Active rects: rect1 (x1=1<2, x2=7>1), rect2? x1=2? Condition x1 < 2? x1=2 not <2 false, so not active. rect3? x1=4.5 not <2 false. So only rect1. y_intervals: [1,7]. union length=6. area contribution =16=6.
(2,4.5): width=2.5. Active: rect1 (2<4.5 true, 7>2 true), rect2 (2<4.5? x1=2 <4.5 true, x2=8>2 true) => active. rect3? x1=4.5 <4.5? false, not active. So two intervals: rect1 [1,7], rect2 [3,9]? Actually rect2 y1=6-3=3, y2=6+3=9, yes. So y_intervals: [1,7] and [3,9]. sort: [1,7],[3,9]. merge: cur=[1,7]; next l=3 <=7 -> cur_r=max(7,9)=9 => cur=[1,9]; then total_y=9-1=8. area+=2.58=20.
(4.5,6.5): width=2.0. Active: rect1: x1=1<6.5, x2=7>4.5 => active. rect2: x1=2<6.5, x2=8>4.5 => active. rect3: x1=4.5<6.5 true, x2=6.5>4.5 true => active. So three intervals: rect1[1,7], rect2[3,9], rect3[3.5,5.5]. sort by y1: [1,7], [3,9], [3.5,5.5] actually after sort: [1,7],[3,9],[3.5,5.5] (since 1<3<3.5). merge: start [1,7]; next [3,9] -> l=3<=7 => cur_r=max(7,9)=9 => cur=[1,9]; next [3.5,5.5] -> l=3.5 <=9 => cur_r stays 9 => cur=[1,9]; total_y=8. area+=28=16.
(6.5,7): width=0.5. Active: rect1: x1=1<7 true, x2=7>6.5 true? x2=7 >6.5 true. rect2: x1=2<7 true, x2=8>6.5 true. rect3: x1=4.5<7 true, but x2=6.5 >6.5? x2=6.5 >6.5? Condition x2 > x_left (6.5). x2 = 6.5, x_left=6.5 => x2 > x_left is false (since not >). So rect3 not active. So intervals: rect1[1,7], rect2[3,9]. merge => [1,9] length=8. area+=0.58=4.
(7,8): width=1. Active: rect1? x1=1<8 true, x2=7 >7? Condition x2 > x_left (7). x2=7 >7? false, so rect1 not active. rect2: x1=2<8 true, x2=8>7 true => active. rect3? x2=6.5 not >7. So only rect2: [3,9] length=6. area+=1*6=6.
Total area = 6+20+16+4+6 = 52. Good.
Second dataset: rect1: [0,6]x[0,6]; rect2: [0.5,2.5]x[0.5,2.5]. xs: 0,6,0.5,2.5 sorted: 0,0.5,2.5,6? Actually sort: 0,0.5,2.5,6. Intervals:
(0,0.5): width=0.5. Active: rect1 (0<0.5 true, 6>0 true) => active; rect2: x1=0.5 <0.5? false, so not. So y_intervals: [0,6] length=6. area+=3? 0.56=3.
(0.5,2.5): width=2.0. Active: rect1 (0<2.5,6>0.5) => active; rect2 (0.5<2.5 true? x1=0.5<2.5 true, x2=2.5>0.5 true? x2=2.5 >0.5 true) => active. So y_intervals: rect1[0,6], rect2[0.5,2.5]. Merge: [0,6] length=6. area+=26=12.
(2.5,6): width=3.5. Active: rect1 (0<6,6>2.5 true) => active; rect2: x2=2.5 >2.5? false (since not >) so not active. So y_intervals: [0,6] length=6. area+=3.5*6=21.
Total =3+12+21=36. Good.
Thus works.
Now implement.
But need to consider if there are overlapping intervals where y ranges are exactly same? That's fine.
Now about rounding: The output should be two decimal places after rounding. Using printf("%.2f") does rounding. Should we use f? Yes.
Potential issue: There may be many datasets, so we should clear vectors each time.
Now a more efficient method: We could also use rectangle union area algorithm by dividing plane into cells and using inclusion-exclusion, but our method is fine.
Potential concern: The x coordinates list may include edges from rectangles that are not part of any active set because they are isolated? That doesn't matter.
Now, there is a nuance: The active set condition we used is rect.x1 < xr && rect.x2 > xl. But suppose there is a rectangle that starts exactly at xl and ends exactly at xr (i.e., x1=xl, x2=xr). Then it's active for the interior because x1 < xr true, x2 > xl true. So it's included, which is correct. Good.
Now, about the sorting of xs: We might have many xs due to duplicates. That's fine. But note: In the active set condition, if there is a rectangle with x1 = xr (i.e., left edge at the right endpoint of the interval), it's not active; but since xr is from some rectangle's edge, that rectangle's left edge is exactly at xr, and it will become active in the next interval (xr, next). So correct.
Now, we need to watch for floating point errors causing condition to fail due to tiny differences. For example, suppose xl = 1.0, xr = 1.0+epsilon, and rectangle's x1 = 1.0 exactly. Then condition x1 < xr: 1.0 < 1.0+epsilon is true if epsilon > 0. So fine. But if due to floating rounding, xr computed as 1.0 (if xs had duplicates but we kept only one?), if duplicates merged, we might lose the fact that there is an interval. Actually if we merge duplicates, we need to ensure we still have intervals between distinct x coordinates. So merging duplicates is beneficial to avoid zero width intervals. We should actually unique the xs with a tolerance? But typical solution: sort and then use unique with direct equality? But comparing doubles exactly may not identify duplicates from same computed values if they are exactly same bitwise (they likely are if they are from same arithmetic operations). Since we compute x1 and x2 as x-r and x+r. If inputs are like "4.0 4.0 3.0", we compute 4.0-3.0=1.0 exactly representable? 1.0 is exact. So duplicates should be exactly equal if same value appears from multiple rectangles (e.g., two rectangles both have left edge at 1.0). So using std::unique on sorted vector after sort (which uses ==) may treat them as equal only if they are bitwise equal? Actually std::unique uses equality comparison, which for double will compare exactly. But due to floating point errors, two values that mathematically should be equal may not be exactly equal if computed differently? For example, 4.0-3.0 = 1.0 exactly, and 5.0-4.0? Actually not relevant. All coordinates are given as decimals likely with one decimal; so all operations are exact in binary? Not all decimals are exact in binary, e.g., 0.1 is not exact. But differences like 3.0 - 2.0 = 1.0 exact; but if we have numbers like 0.1, 0.2, they might not be exact. However, duplicates from the same arithmetic expression (x - r) might produce same value for different rectangles? Possibly if different x and r yield same x-r; then they would be mathematically equal, but due to floating representation they could be slightly different if computed from different inputs that are not exactly representable. For instance, if x=0.1, r=0.0 then x1=0.1; another rectangle with x=0.2,r=0.1 yields x1=0.1 mathematically but 0.2 - 0.1 = 0.1; both might produce 0.1 if both numbers are exactly 0.1? But 0.1 is not exactly representable, so 0.1 from the first input is read as a double approximation, say a. For the second, 0.2 and 0.1 are also approximations, and the result of subtraction may not be exactly the same as a due to rounding errors. So duplicates may not be exactly equal. This could lead to many unique xs values, up to 2n (200) and they might all be distinct anyway, which is fine. So we don't need to unique; we can just keep all and skip zero-width intervals if width is essentially zero. However, if there are many duplicates (mathematically same but floating different), we might have many extremely close x coordinates, leading to very small intervals that may be considered positive width if difference > 1e-12. This could add small area contributions erroneously due to rounding errors? Possibly, but likely not a problem because the active set condition for that narrow interval would be almost the same as for a zero width, but multiplied by a tiny width could cause negligible error. However, to be safe, we could sort and then use a tolerance to combine nearly equal x's into a single value. But it's simpler to keep all and just use if (xr - xl > 1e-9) as threshold. Because the maximum area is about 360k, and rounding to 2 decimals, errors on order 1e-9*400 ~ 4e-7 are irrelevant. So it's fine.
Thus we can proceed.
Now about reading input: The input might contain multiple lines with spaces; use cin.
Edge Cases: n=0 terminates.
Now, let's confirm with a more complex example? Not needed.
But there is a nuance: The union area may be computed incorrectly if we use the condition "rect.x1 < xr && rect.x2 > xl" and then compute union of y intervals for the whole strip. However, as argued, the active set is constant within (xl, xr). But does the union of y intervals remain constant? Yes, because the y intervals of active rectangles do not change with x (they are axis-aligned rectangles, y intervals are constant across x). So it's correct.
Therefore implementation as above.
But note: In some sweep line implementations, they also need to consider that at the boundaries, the union length might change discontinuously, but our integration over intervals picks up the contributions correctly.
Now, check: For a rectangle that starts at xl, its y interval is included for the strip. For a rectangle that ends at xr, it's also included. So the strip area corresponds to the union of rectangles over the interior of the strip. The contribution from the boundaries (lines) is zero measure, so it's fine.
Thus algorithm is correct.
Now we should implement and test on sample.
One more check: The rectangles are squares, but we treat them as axis-aligned rectangles, which is correct.
Potential large dataset: n=100 with random values. The algorithm is fine.
Now I'll write final solution in C++.
But there's one more nuance: The input format says "n" then n lines each with "xi yi ri" fractional numbers. Coordinates between 0 and 200 inclusive. So we can use double.
Now, output: "1 52.00" without extra spaces. So use printf.
Now, I'd like to also consider using long double for extra precision? Not needed.
Now, memory: small.
Now, confirm that we should not include the area of overlap multiple times; the merging of y intervals accounts for that.
Now, I think this is fine.
However, we must be careful with the sorting of y intervals: if we have an empty active set, we skip.
Now, about reading until EOF: while(cin >> n && n != 0) works.
Now, I'll produce final code.
But wait: There's a potential pitfall: The number of xs can be up to 200, but we are iterating i from 0 to size-2 inclusive. That's fine.
Now, test with sample again manually maybe.
Now, think about possible floating point rounding errors in the condition rect.x1 < xr. If rect.x1 is computed as, say, 1.0000000000001 and xr is 1.0000000000000 due to rounding, then condition may incorrectly exclude a rectangle that should be active. But this is unlikely to cause large area error. To reduce such errors, we could use a small epsilon in condition: rect.x1 - xr < eps? But that might incorrectly include rectangles that should not be active. Given typical values are with at most maybe few decimals, and double precision is around 15 decimal digits, the error from subtracting numbers of magnitude up to 400 is about 1e-13 relative, so condition without epsilon is safe. But if numbers are given with many decimals, the representation may not be exact. However, typical problems like this accept solution with double and direct comparisons.
Alternatively, we can use a different approach: we can use event-based sweep line where we maintain active set by adding at left edges and removing at right edges, sorted by x, and then process intervals. That would avoid the condition check per interval. But then we need to handle multiple events at same x; but that's also fine. That approach may be more efficient and avoids checking all rectangles per interval. However, given n=100, it's okay.
But my current method checks all rectangles per interval (200 intervals * 100 rectangles = 20k checks). That's fine.
But we need to compute y intervals per interval anyway, so checking all rectangles is okay.
Now, maybe we could improve by only checking those rectangles whose x interval covers the current x strip; but that's what we do.
Thus final answer.
But let me double-check the merging of y intervals: we need to be careful that y_intervals may have negative values; sorting works.
But before final, there is one more nuance: The input may have n up to 100, but coordinates could be real numbers with maybe more than 2 decimals, but it's fine.
Now, consider potential performance: The inner loop sorts y_intervals. y_intervals size could be up to 100. Sorting each time is O(m log m). Could we avoid sorting by using a global approach? Not needed.
Now, think about rounding: We need to output with exactly two decimal digits after rounding. Using printf("%.2f") ensures that. But careful: if area is -0.00? Not possible because area non-negative.
Now, coding:
code
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;
struct Rect {
double x1, x2, y1, y2;
};
int main() {
int n;
int caseNo = 0;
while (cin >> n && n != 0) {
caseNo++;
vector<Rect> rects(n);
vector<double> xs;
for (int i = 0; i < n; i++) {
double x, y, r;
cin >> x >> y >> r;
double x1 = x - r, x2 = x + r;
double y1 = y - r, y2 = y + r;
rects[i] = {x1, x2, y1, y2};
xs.push_back(x1);
xs.push_back(x2);
}
sort(xs.begin(), xs.end());
double area = 0.0;
for (size_t i = 0; i + 1 < xs.size(); i++) {
double xl = xs[i], xr = xs[i+1];
if (xr - xl < 1e-12) continue;
vector<pair<double, double>> yint;
for (const Rect& r : rects) {
if (r.x1 < xr && r.x2 > xl) {
yint.emplace_back(r.y1, r.y2);
}
}
if (yint.empty()) continue;
sort(yint.begin(), yint.end());
double total_y = 0.0;
double cur_l = yint[0].first, cur_r = yint[0].second;
for (size_t j = 1; j < yint.size(); j++) {
double l = yint[j].first, r = yint[j].second;
if (l <= cur_r) {
if (r > cur_r) cur_r = r;
} else {
total_y += cur_r - cur_l;
cur_l = l; cur_r = r;
}
}
total_y += cur_r - cur_l;
area += (xr - xl) * total_y;
}
printf("%d %.2f\n", caseNo, area);
}
return 0;
}
Now, is there any chance that area might be negative? No.
Now, confirm with sample. It should output as expected.
One more nuance: The problem says "The area of regions of points covered by at least one antenna is 52.00 in this case." So they likely expect exactly two digits after decimal, and rounding.
Now, about the figure: irrelevant.
Now, ensure we flush output? Not needed.
Now, we can consider using set to get unique x values, but not necessary.
Potential improvement: If we want to be extra safe about floating errors in the condition, we could use a small epsilon:
if (r.x1 + eps < xr && r.x2 > xl + eps) but that may cause errors. I'd keep as is.
Now, I'd like to test edge case: n=2, both rectangles with zero radius: r=0. Then each is a point. The union area should be 0. Our algorithm: each rectangle has x1=x2, y1=y2. xs includes those points. Intervals between sorted xs: there might be intervals of positive width if the points have different x coordinates. For example, two points at (0,0) and (10,0) with r=0: rect1: [0,0]x[0,0]; rect2: [10,10]x[0,0]. xs: 0,0,10,10 sorted: 0,0,10,10. Intervals: (0,0) skip; (0,10) width=10. For interval (0,10), active rectangles? rect1: x1=0 <10 true, x2=0 >0? x2=0 >0? false, so not active. rect2: x1=10 <10 false, not active. So yint empty, skip. So area=0. Good.
If they have same x coordinate: xs: 0,0,0,0 sorted: 0,0,0,0. All intervals width zero skip. area=0.
Now, test with a rectangle that has negative x1 but xr positive, etc.
All good.
Now, finalize answer.
But wait, the problem statement says "The coordinate of the i-th antenna is given by (xi, yi), and its power is ri. xi, yi and ri are fractional numbers between 0 and 200 inclusive." So they are non-negative. But xi-ri can be negative. So fine.
Now, possible alternative: We could also compute union area using plane sweep with segment tree, but overkill.
Thus final solution as above.
Now, I'll provide final answer in required format: code with explanation? The user only asked to "Write a program that finds the area of coverage by a given collection of antenna locations." So we provide code.
But as ChatGPT, we need to output final answer (the code). The conversation history indicates we are to produce a solution. So I'll provide final answer with explanation and code.
But the instruction: "A mobile phone company ACMICPC ... Write a program that finds the area of coverage by a given collection of antenna locations." So they want a program. We'll output the code.
In the final answer, we can include a brief description and then the code.
Thus final. ```cpp
#include
#include
#include
#include
using namespace std;
struct Rect {
double x1, x2, y1, y2;
};
int main() {
int n, caseNo = 0;
while (cin >> n && n != 0) {
++caseNo;
vector rects(n);
vector xs;
code
// read rectangles, compute their bounding squares
for (int i = 0; i < n; ++i) {
double x, y, r;
cin >> x >> y >> r;
double x1 = x - r, x2 = x + r;
double y1 = y - r, y2 = y + r;
rects[i] = {x1, x2, y1, y2};
xs.push_back(x1);
xs.push_back(x2);
}
sort(xs.begin(), xs.end());
double area = 0.0;
// sweep over intervals between consecutive x‑coordinates
for (size_t i = 0; i + 1 < xs.size(); ++i) {
double xl = xs[i], xr = xs[i + 1];
if (xr - xl < 1e-12) continue; // skip zero‑width intervals
// collect y‑intervals of all rectangles covering this vertical strip
vector<pair<double, double>> yint;
for (const Rect& r : rects) {
if (r.x1 < xr && r.x2 > xl) { // active in (xl, xr)
yint.emplace_back(r.y1, r.y2);
}
}
if (yint.empty()) continue;
// merge y‑intervals to obtain their total length
sort(yint.begin(), yint.end());
double total_y = 0.0;
double cur_l = yint[0].first, cur_r = yint[0].second;
for (size_t j = 1; j < yint.size(); ++j) {
double l = yint[j].first, r = yint[j].second;
if (l <= cur_r) {
if (r > cur_r) cur_r = r; // extend current interval
} else {
total_y += cur_r - cur_l; // finish current interval
cur_l = l;
cur_r = r;
}
}
total_y += cur_r - cur_l;
area += (xr - xl) * total_y;
}
printf("%d %.2f\n", caseNo, area);
}
return 0;