UVa 688

From Algorithmist
Jump to navigation Jump to search

688 - Mobile Phone Coverage[edit]


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 solution that works by cleverly breaking up the plane into discrete grid. There is also a Sweep Line solution that runs in .

Discrete Grid Explanation[edit]

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 iff there is some tower whose range covers the rectangle with x and y coordinates in range and . otherwise . Then to compute the coverage, we simply look all of the 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 time per square, for a total cost of .

Sweep Line Explanation[edit]

Sweep a vertical line from . 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 to . 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 events, and each event can be handled in time for the deque, for the insertion or deletion to T, and for the length of the union query, the total time is .

Sweep Line Notes[edit]

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)


4.0 4.0 3.0
5.0 6.0 3.0
5.5 4.5 1.0
3.0 3.0 3.0
1.5 1.5 1.0


1 52.00
2 36.00