Meeting (Codeforces 144B)
Tags: Implement, implementation

The Super Duper Secret Meeting of the Super Duper Secret Military Squad takes place in a Super Duper Secret Place. The place is an infinite plane with introduced Cartesian coordinate system. The meeting table is represented as a rectangle whose sides are parallel to the coordinate axes and whose vertexes are located at the integer points of the plane. At each integer point which belongs to the table perimeter there is a chair in which a general sits.Some points on the plane contain radiators for the generals not to freeze in winter. Each radiator is characterized by the number ri — the radius of the area this radiator can heat. That is, if the distance between some general and the given radiator is less than or equal to ri, than the general feels comfortable and warm. Here distance is defined as Euclidean distance, so the distance between points (x1, y1) and (x2, y2) is Each general who is located outside the radiators' heating area can get sick. Thus, you should bring him a warm blanket. Your task is to count the number of warm blankets you should bring to the Super Duper Secret Place.The generals who are already comfortable do not need a blanket. Also the generals never overheat, ever if they are located in the heating area of several radiators. The radiators can be located at any integer points on the plane, even inside the rectangle (under the table) or on the perimeter (directly under some general). Even in this case their radius does not change.

Input: The first input line contains coordinates of two opposite table corners xa, ya, xb, yb (xa ≠ xb, ya ≠ yb). The second line contains integer n — the number of radiators (1 ≤ n ≤ 103). Then n lines contain the heaters' coordinates as "xi yi ri", the numbers are separated by spaces. All input data numbers are integers. The absolute value of all coordinates does not exceed 1000, 1 ≤ ri ≤ 1000. Several radiators can be located at the same point.

Output: Print the only number — the number of blankets you should bring.

Note: In the first sample the generals are sitting at points: (2, 2), (2, 3), (2, 4), (2, 5), (3, 2), (3, 5), (4, 2), (4, 3), (4, 4), (4, 5). Among them, 4 generals are located outside the heating range. They are the generals at points: (2, 5), (3, 5), (4, 4), (4, 5).In the second sample the generals are sitting at points: (5, 2), (5, 3), (6, 2), (6, 3). All of them are located inside the heating range.

Sample Inputs:
2 5 4 2
3
3 1 2
5 3 1
1 3 2

5 2 6 3
2
6 2 2
6 5 3

Sample Outputs:
4

0

Solution (Python):
import math
coor = list(map(int, input().split()))
startx = min(coor[0], coor[2])
starty = min(coor[1], coor[3])
length_y = abs(coor[1] - coor[3])
length_x = abs(coor[0] - coor[2])
endx = startx + length_x
endy = starty + length_y
number_of_c = int(input())
c_data = {'x': {startx: {'arr': []}, endx: {'arr': []}}, 'y': {starty: {'arr': []}, endy: {'arr': []}}}
count = 0

def findy(coor, value, r):
    v = r ** 2 - (coor[0] - value) ** 2
    if value > coor[0] + r or v < 0:
        return False
    else:
        y = math.sqrt(v)
        ans1 = y + coor[1]
        ans2 = y * -1 + coor[1]
        small = min(ans2, ans1)
        big = max(ans2, ans1)
        return [small if small == math.ceil(small) else math.ceil(small), big if big == math.ceil(big) else math.floor(big)]

def findx(coor, value, r):
    v = r ** 2 - (coor[1] - value) ** 2
    if value > coor[1] + r or v < 0:
        return False
    else:
        x = math.sqrt(v)
        ans1 = x + coor[0]
        ans2 = x * -1 + coor[0]
        small = min(ans2, ans1)
        big = max(ans2, ans1)
        return [small if small == math.ceil(small) else math.ceil(small), big if big == math.ceil(big) else math.floor(big)]
for i in range(startx, endx + 1):
    c_data['y'][endy]['arr'].append(i)
    c_data['y'][starty]['arr'].append(i)
for i in range(starty + 1, endy):
    c_data['x'][endx]['arr'].append(i)
    c_data['x'][startx]['arr'].append(i)
for j in range(0, number_of_c):
    x = list(map(int, input().split()))
    point = [x[0], x[1]]
    for z in c_data['x'].keys():
        info = findy(point, z, x[2])
        if info == False:
            continue
        c_data['x'][z]['arr'] = [i for i in c_data['x'][z]['arr'] if i < info[0] or i > info[1]]
    for z in c_data['y'].keys():
        info = findx(point, z, x[2])
        if info == False:
            continue
        c_data['y'][z]['arr'] = [i for i in c_data['y'][z]['arr'] if i < info[0] or i > info[1]]
counter = 0
for i in ['x', 'y']:
    for j in c_data[i]:
        counter += len(c_data[i][j]['arr'])
print(counter)

Solution (Python):
def f(i, t, Y, c):
    (arr, res) = ([(t, t)], 0)
    for (x, y, r) in c:
        d = r - (y - Y) ** 2
        if d < 0:
            continue
        d = int(d ** 0.5)
        arr.append((x - d, x + d + 1))
    for (a, b) in sorted(arr):
        if i < a and i < t:
            res += a - i
        if i < b:
            i = b
    return res
(xa, ya, xb, yb) = map(int, input().split())
if xa > xb:
    (xa, xb) = (xb, xa)
if ya > yb:
    (ya, yb) = (yb, ya)
n = int(input())
arr = [map(int, input().split()) for _ in range(n)]
arr1 = [(x, y, r * r) for (x, y, r) in arr]
arr2 = [(y, x, r) for (x, y, r) in arr1]
res1 = f(xa, xb + 1, ya, arr1)
res2 = f(xa, xb + 1, yb, arr1)
res3 = f(ya + 1, yb, xa, arr2)
res4 = f(ya + 1, yb, xb, arr2)
print(res1 + res2 + res3 + res4)

Solution (Python):
def f(i, t, Y, c):
    (arr, res) = ([(t, t)], 0)
    for (x, y, r) in c:
        d = r - (y - Y) ** 2
        if d < 0:
            continue
        d = int(d ** 0.5)
        arr.append((x - d, x + d + 1))
    for (a, b) in sorted(arr):
        if i < a and i < t:
            res += a - i
        if i < b:
            i = b
    return res
(xa, ya, xb, yb) = map(int, input().split())
if xa > xb:
    (xa, xb) = (xb, xa)
if ya > yb:
    (ya, yb) = (yb, ya)
n = int(input())
arr = [map(int, input().split()) for _ in range(n)]
arr1 = [(x, y, r * r) for (x, y, r) in arr]
arr2 = [(y, x, r) for (x, y, r) in arr1]
res1 = f(xa, xb + 1, ya, arr1)
res2 = f(xa, xb + 1, yb, arr1)
res3 = f(ya + 1, yb, xa, arr2)
res4 = f(ya + 1, yb, xb, arr2)
print(res1 + res2 + res3 + res4)