Problem:


Problem C:Mobile Phone Coverage

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 &minus; ri, xi + ri] &times; [yi &minus; ri, yi + ri]. In other words, the distance between two points (xp, yp) and (xq, yq) is measured by the max norm max{ |xp &minus; xq|, |yp &minus; yq|}, or, the L&infin; norm, in this city Maxnorm instead of the ordinary Euclidean norm &radic; {(xp &minus; xq)2 + (yp &minus; 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.

Sample 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 for the Sample Input

1 52.00
2 36.00



Sample 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


Sample output:
1 52.00
2 36.00

Submission status: Wrong Answer

Solution code:
import sys

def is_contained(a, area):
    rx1, ry1, rx2, ry2 = a
    for r in area:
        x1, y1, x2, y2 = r
        if x1 <= rx1 <= x2 and x1 <= rx2 <= x2 and \
           y1 <= ry1 <= y2 and y1 <= ry2 <= y2:
            return True
    return False

def is_inside(x, y, rect):
    rx1, ry1, rx2, ry2 = rect
    return (rx1 < x < rx2) and (ry1 < y < ry2)

def add_area(a, area):
    if is_contained(a, area):
        return area
    xs, ys, xe, ye = a
    if xs >= xe or ys >= ye:
        return area
    ret = area
    if area == []:
        return [a]
    rr = []
    for r in area:
        rxs, rys, rxe, rye = r
        if is_inside(rxs, rys, a) and xe < rxe and ye < rye:
            rr = [[xs, ys, xe, rys], [xs, rys, rxs, ye]]
        elif is_inside(rxs, rye, a) and rys < ys and xe < rxe:
            rr = [[xs, ys, rxs, rye], [xs, rye, xe, ye]]
        elif is_inside(rxe, rys, a) and rxs < xs and ye < rye:
            rr = [[xs, ys, rxe, rys], [rxe, ys, xe, ye]]
        elif is_inside(rxe, rye, a) and rxs < xs and rys < ys:
            rr = [[xs, rye, rxe, ye], [rxe, ys, xe, ye]]
        elif is_inside(rxs, rys, a) and is_inside(rxe, rys, a) and ye < rye:
            rr = [[xs, ys, xe, rys], [xs, rys, rxs, ye], [rxe, rys, xe, ye]]
        elif is_inside(rxs, rye, a) and is_inside(rxe, rye, a) and rys < ys:
            rr = [[xs, rye, xe, ye], [xs, ys, rxs, rye], [rxe, ys, xe, rye]]
        elif is_inside(rxs, rys, a) and is_inside(rxs, rye, a) and xe < rxe:
            rr = [[xs, ys, rxs, ye], [rxs, ys, xe, rys], [rxs, rye, xe, ye]]
        elif is_inside(rxe, rys, a) and is_inside(rxe, rye, a) and rxs < xs:
            rr = [[rxe, ys, xe, ye], [xs, ys, rxe, rys], [xs, rye, rxe, ye]]
        elif rxs < xs and xe < rxe and ys < rys < ye:
            rr = [[xs, ys, xe, rys]]
        elif rys < ys and ye < rye and xs < rxs < xe:
            rr = [[xs, ys, rxs, ye]]
        elif rxs < xs and xe < rxe and ys < rye < ye:
            rr = [[xs, rye, xe, ye]]
        elif rys < ys and ye < rye and xs < rxe < xe:
            rr = [[rxe, ys, xe, ye]]
        if rr != []:
            for q in rr:
                ret = add_area(q, ret)
            break
    if rr == []:
        ret.append(a)
    return ret

def calc_area(area):
    s = 0.0
    for r in area:
        s += (r[2]- r[0]) * (r[3] - r[1])
    return s

n = 0
c = 0
area = []
for line in sys.stdin:
    if n == 0:
        n = int(line)
        c += 1
        area = []
        if n == 0:
            break
    else:
        ant = list(map(float, line.strip().split()))
        r = [ant[0] - ant[2], ant[1] - ant[2], ant[0] + ant[2], ant[1] + ant[2]]
        area = add_area(r, area)
        n -= 1
        if n == 0:
            print("%d %.2f" % (c, calc_area(area)))

Submission status: Wrong Answer

Solution code:
import sys

def is_contained(a, area):
    rx1, ry1, rx2, ry2 = a
    for r in area:
        x1, y1, x2, y2 = r
        if x1 <= rx1 <= x2 and x1 <= rx2 <= x2 and \
           y1 <= ry1 <= y2 and y1 <= ry2 <= y2:
            return True
    return False

def is_inside(x, y, rect):
    rx1, ry1, rx2, ry2 = rect
    return (rx1 <= x <= rx2) and (ry1 <= y <= ry2)

def add_area(a, area):
    if is_contained(a, area):
        return area
    xs, ys, xe, ye = a
    if xs >= xe or ys >= ye:
        return area
    ret = area
    if area == []:
        return [a]
    rr = []
    for r in area:
        rxs, rys, rxe, rye = r
        if xs <= rxs < xe and ys <= rys < ye and xe <= rxe and ye <= rye:
            rr = [[xs, ys, xe, rys], [xs, rys, rxs, ye]]
        elif xs <= rxs < xe and ys < rye <= ye and rys <= ys and xe <= rxe:
            rr = [[xs, ys, rxs, rye], [xs, rye, xe, ye]]
        elif xs < rxe <= xe and ys <= rys < ye and rxs <= xs and ye <= rye:
            rr = [[xs, ys, rxe, rys], [rxe, ys, xe, ye]]
        elif xs < rxe <= xe and ys < rye <= ye and rxs <= xs and rys <= ys:
            rr = [[xs, rye, rxe, ye], [rxe, ys, xe, ye]]
        elif xs <= rxs and ys <= rys < ye and rxe <= xe and ye <= rye:
            rr = [[xs, ys, xe, rys], [xs, rys, rxs, ye], [rxe, rys, xe, ye]]
        elif xs <= rxs and ys < rye <= ye and rxe <= xe and rys <= ys:
            rr = [[xs, rye, xe, ye], [xs, ys, rxs, rye], [rxe, ys, xe, rye]]
        elif xs <= rxs < xe and ys <= rys and rye <= ye and xe <= rxe:
            rr = [[xs, ys, rxs, ye], [rxs, ys, xe, rys], [rxs, rye, xe, ye]]
        elif xs < rxe <= xe and ys <= rys and rye <= ye and rxs <= xs:
            rr = [[rxe, ys, xe, ye], [xs, ys, rxe, rys], [xs, rye, rxe, ye]]
        elif rxs <= xs and xe <= rxe and ys <= rys < ye:
            rr = [[xs, ys, xe, rys]]
        elif rys <= ys and ye <= rye and xs <= rxs < xe:
            rr = [[xs, ys, rxs, ye]]
        elif rxs <= xs and xe <= rxe and ys < rye <= ye:
            rr = [[xs, rye, xe, ye]]
        elif rys <= ys and ye <= rye and xs < rxe <= xe:
            rr = [[rxe, ys, xe, ye]]
        if rr != []:
            for q in rr:
                ret = add_area(q, ret)
            break
    if rr == []:
        ret.append(a)
    return ret

def calc_area(area):
    s = 0.0
    for r in area:
        s += (r[2]- r[0]) * (r[3] - r[1])
    return s

n = 0
c = 0
area = []
for line in sys.stdin:
    if n == 0:
        n = int(line)
        c += 1
        area = []
        if n == 0:
            break
    else:
        ant = list(map(float, line.strip().split()))
        r = [ant[0] - ant[2], ant[1] - ant[2], ant[0] + ant[2], ant[1] + ant[2]]
        area = add_area(r, area)
        n -= 1
        if n == 0:
            print("%d %.2f" % (c, calc_area(area)))

Submission status: Accepted

Solution code:
import sys

def is_contained(a, area):
    rx1, ry1, rx2, ry2 = a
    for r in area:
        x1, y1, x2, y2 = r
        if x1 <= rx1 <= x2 and x1 <= rx2 <= x2 and \
           y1 <= ry1 <= y2 and y1 <= ry2 <= y2:
            return True
    return False

def add_area(a, area):
    if is_contained(a, area):
        return area
    xs, ys, xe, ye = a
    if xs >= xe or ys >= ye:
        return area
    ret = area
    if area == []:
        return [a]
    rr = []
    for r in area:
        rxs, rys, rxe, rye = r
        if xs < rxs < xe and ys < rys < ye and xe <= rxe and ye <= rye:
            rr = [[xs, ys, xe, rys], [xs, rys, rxs, ye]]
        elif xs < rxs < xe and ys < rye < ye and rys <= ys and xe <= rxe:
            rr = [[xs, ys, rxs, rye], [xs, rye, xe, ye]]
        elif xs < rxe < xe and ys < rys < ye and rxs <= xs and ye <= rye:
            rr = [[xs, ys, rxe, rys], [rxe, ys, xe, ye]]
        elif xs < rxe < xe and ys < rye < ye and rxs <= xs and rys <= ys:
            rr = [[xs, rye, rxe, ye], [rxe, ys, xe, ye]]
        elif xs < rxs and ys <= rys < ye and rxe < xe and ye <= rye:
            rr = [[xs, ys, xe, rys], [xs, rys, rxs, ye], [rxe, rys, xe, ye]]
        elif xs < rxs and ys < rye <= ye and rxe < xe and rys <= ys:
            rr = [[xs, rye, xe, ye], [xs, ys, rxs, rye], [rxe, ys, xe, rye]]
        elif xs <= rxs < xe and ys < rys and rye < ye and xe <= rxe:
            rr = [[xs, ys, rxs, ye], [rxs, ys, xe, rys], [rxs, rye, xe, ye]]
        elif xs < rxe <= xe and ys < rys and rye < ye and rxs <= xs:
            rr = [[rxe, ys, xe, ye], [xs, ys, rxe, rys], [xs, rye, rxe, ye]]
        elif rxs <= xs and xe <= rxe and ys < rys < ye and ye <= rye:
            rr = [[xs, ys, xe, rys]]
        elif rys <= ys and ye <= rye and xs < rxs < xe and xe <= rxe:
            rr = [[xs, ys, rxs, ye]]
        elif rxs <= xs and xe <= rxe and ys < rye < ye and rys <= ys:
            rr = [[xs, rye, xe, ye]]
        elif rys <= ys and ye <= rye and xs < rxe < xe and rxs <= xs:
            rr = [[rxe, ys, xe, ye]]
        elif xs < rxs < xe and xs < rxe < xe and ys < rys < ye and ys < rye < ye:
            rr = [[xs, ys, rxs, rye], [xs, rye, rxe, ye], [rxe, rys, xe, ye], [rxs, ys, xe, rys]]
        elif rxs <= xs and xe <= rxe and ys < rys and rye < ye:
            rr = [[xs, ys, xe, rys], [xs, rye, xe, ye]]
        elif rys <= ys and ye <= rye and xs < rxs and rxe < xe:
            rr = [[xs, ys, rxs, ye], [rxe, ys, xe, ye]]
        if rr != []:
            for q in rr:
                ret = add_area(q, ret)
            break
    if rr == []:
        ret.append(a)
    return ret

def calc_area(area):
    s = 0.0
    for r in area:
        s += (r[2]- r[0]) * (r[3] - r[1])
    return s

n = 0
c = 0
area = []
for line in sys.stdin:
    if n == 0:
        n = int(line)
        c += 1
        area = []
        if n == 0:
            break
    else:
        ant = list(map(float, line.strip().split()))
        r = [ant[0] - ant[2], ant[1] - ant[2], ant[0] + ant[2], ant[1] + ant[2]]
        area = add_area(r, area)
        n -= 1
        if n == 0:
            print("%d %.2f" % (c, calc_area(area)))

Submission status: Accepted

Solution code:
# AOJ 1202: Mobile Phone Coverage
# Python3 2018.7.28

from bisect import bisect_left
cno = 0
while True:
	n = int(input())
	if n == 0: break
	tbl, xx, yy = [], set(), set()
	for i in range(n):
		x, y, r = map(float, input().split())
		x, y, r = int(100*x), int(100*y), int(100*r)
		x1, y1, x2, y2 = x-r, y-r, x+r, y+r
		tbl.append((x1, y1, x2, y2))
		xx.add(x1); xx.add(x2)
		yy.add(y1); yy.add(y2)
	xx = sorted(xx); yy = sorted(yy)
	arr = [[0 for c in range(201)] for r in range(201)]
	for x1, y1, x2, y2 in tbl:
		l = bisect_left(xx, x1); r = bisect_left(xx, x2)
		u = bisect_left(yy, y1); d = bisect_left(yy, y2)
		for y in range(u, d):
			for x in range(l, r): arr[y][x] = 1
	ans = 0
	for r in range(1, len(yy)):
		t = yy[r]-yy[r-1]
		for c in range(1, len(xx)):
			if arr[r-1][c-1]: ans += t * (xx[c]-xx[c-1])
	ans //= 100
	cno += 1
	print(cno, "{:d}.{:02d}".format(ans//100, ans%100))

Submission status: Runtime Error

Solution code:
import sys

def is_contained(a, area):
    rx1, ry1, rx2, ry2 = a
    for r in area:
        x1, y1, x2, y2 = r
        if x1 <= rx1 <= x2 and x1 <= rx2 <= x2 and \
           y1 <= ry1 <= y2 and y1 <= ry2 <= y2:
            return True
    return False

def is_inside(x, y, rect):
    rx1, ry1, rx2, ry2 = rect
    return (rx1 < x < rx2) and (ry1 < y < ry2)

def add_area(a, area):
    if is_contained(a, area):
        return area
    xs, ys, xe, ye = a
    if xs >= xe or ys >= ye:
        return area
    ret = area
    if area == []:
        return [a]
    rr = []
    for r in area:
        rxs, rys, rxe, rye = r
        if is_inside(rxs, rys, a) and xe < rxe and ye < rye:
            rr = [[xs, ys, xe, rys], [xs, rys, rxs, ye]]
        elif is_inside(rxs, rye, a) and rys < ys and xe < rxe:
            rr = [[xs, ys, rxs, rye], [xs, rye, xe, ye]]
        elif is_inside(rxe, rys, a) and rxs < xs and ye < rye:
            rr = [[xs, ys, rxe, rys], [rxe, ys, xe, ye]]
        elif is_inside(rxe, rye, a) and rxs < xs and rys < ys:
            rr = [[xs, rye, rxe, ye], [rxe, ys, xe, ye]]
        elif is_inside(rxs, rys, a) and is_inside(rxe, rys, a) and ye < rye:
            rr = [[xs, ys, xe, rys], [xs, rys, rxs, ye], [rxe, rys, xe, ye]]
        elif is_inside(rxs, rye, a) and is_inside(rxe, rye, a) and rys < ys:
            rr = [[xs, rye, xe, ye], [xs, ys, rxs, rye], [rxe, ys, xe, rye]]
        elif is_inside(rxs, rys, a) and is_inside(rxs, rye, a) and xe < rxe:
            rr = [[xs, ys, rxs, ye], [rxs, ys, xe, rys], [rxs, rye, xe, ye]]
        elif is_inside(rxe, rys, a) and is_inside(rxe, rye, a) and rxs < xs:
            rr = [[rxe, ys, xe, ye], [xs, ys, rxe, rys], [xs, rye, rxe, ye]]
        elif rxs < xs and xe < rxe and ys < rys < ye:
            rr = [[xs, ys, xe, rys]]
        elif rys < ys and ye < rye and xs < rxs < xe:
            rr = [[xs, ys, rxs, ye]]
        elif rxs < xs and xe < rxe and ys < rye < ye:
            rr = [[xs, rye, xe, ye]]
        elif rys < ys and ye < rye and xs < rxe < xe:
            rr = [[rxe, ys, xe, ye]]
        if rr != []:
            for q in rr:
                ret = add_area(q, ret)
            break
    if rr == []:
        ret.append(a)
    return ret

def calc_area(area):
    s = 0.0
    for r in area:
        s += (r[2]- r[0]) * (r[3] - r[1])
    return s

n = 0
c = 0
area = []
for line in sys.stdin:
    if n == 0:
        n = int(line)
        c += 1
        area = []
        if n == 0:
            break
    else:
        ant = list(map(float, line.strip().split(" ")))
        r = [ant[0] - ant[2], ant[1] - ant[2], ant[0] + ant[2], ant[1] + ant[2]]
        area = add_area(r, area)
        n -= 1
        if n == 0:
            print("%d %.2f" % (c, calc_area(area)))