# UVa 688

## Summary

We have to find the area of n axis-aligned squares with arbitrary positions and sizes. This is a nice problem with two fairly different solutions. There is a $O(n^{3})$ solution that works by cleverly breaking up the plane into discrete grid. There is also a Sweep Line solution that runs in $O(n^{2})$ .

## Discrete Grid Explanation

Given n squares, there are really only 2n "interesting" points on the x-axis and likewise 2n interesting points on the y-axis. The points that are important are those that are on the corners of the square. So we make two arrays, each of size 2n, storing the corners of the squares in sorted order both horizontally and vertically. Call the vertical segments sorted by x-coordinate, h, and the horizontal segments sorted by y-coordinate, v. We then make a 2n by 2n boolean grid. We then fill up according to the rule $grid_{i,j}=1$ iff there is some tower whose range covers the rectangle with x and y coordinates in range $h_{i} and $v_{i} . otherwise $grid_{i,j}=0$ . Then to compute the coverage, we simply look all of the $O(n^{2})$ elements in the grid, and add its area to the total area if it is marked. For each of the n squares, we can mark the squares it contains in $O(n^{2})$ time per square, for a total cost of $O(n^{3})$ .

## Sweep Line Explanation

Sweep a vertical line from $(-\infty ,\infty )$ . Note that at any vertical line, the intersection with line with the tower coverage squares is a set of vertical line segments, which may contain intersecting segments, or may be empty. Our status, which is the intersection of the squares with the Sweep Line in a binary search tree T will be represented as follows. Consider the intersection of the sweep line with the squares from $y=-\infty$ to $\infty$ . At the beginning of each segment, keep a + sign, and at the end of each segment, keep a -. Then for at every point on the sweep line, there is a segment on that line iff there are more + than - below the point.

Our events are similiar to those in the explanation of UVa 105. We maintain the events in a priority queue Q.

• insertion events - keyed by the left end of a coverage
• insert the bottom of coverage into T with a + sign, and the top of the coverage with a - sign.
• insert a removal event for the right end of the square into Q.
• removal events - keyed by the right end of a coverage
• remove both corresponding endpoints of the coverage from T.

After we handle each event, we know both how far horizontally the sweep line has moved, dx, and the length of the union of the active segments, l by querying T. The coverage area swept by is then dx * l, and the total area is the sum of the area swept passed in each event. Since there are $2n=O(n)$ events, and each event can be handled in $O(\log n)$ time for the deque, $O(\log n)$ for the insertion or deletion to T, and $O(n)$ for the length of the union query, the total time is $O(n^{2})$ .

## Sweep Line Notes

Rather than maintaining the status as a binary search tree, a simple array or linked list will work at no additional complexity. We would have to both be able to add, remove, and find the length of the union of the line segments in faster than linear time in order to improve the overall complexity of the algorithm. An interval tree is the ideal data structure to use to support these operations. Actually, by using a segment tree structure, insertion, deleting and union query would be O(lgN), and the problem can be solved in O(NlgN)

## Input

3
4.0 4.0 3.0
5.0 6.0 3.0
5.5 4.5 1.0
2
3.0 3.0 3.0
1.5 1.5 1.0
0


## Output

1 52.00
2 36.00