【CodeForces - 144B 】Meeting (暴力枚举,水题,计算几何)

题干:

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 xayaxbyb (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.

Examples

Input

2 5 4 2
3
3 1 2
5 3 1
1 3 2

Output

4

Input

5 2 6 3
2
6 2 2
6 5 3

Output

0

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.

题目大意:

    一张长方形桌子的四个顶点全部在整数点位置,桌子的四条边上每个整数点(x,y) (x,y都是整数)位置有一把椅子,代表有一个人,当使用加热器在桌子周围加热时,问有几个人不在加热范围之内。

    输入时每组数据首先给出两个点的坐标,代表一张桌子的对角线的端点,桌子的边与坐标轴平行,然后输入一个n,代表有n个加热器,接下来n行每行输入三个数x,y,r,代表这个加热器的坐标和加热半径。

解题报告:

刚开始还以为是在整个矩形内部都有点,但是看了样例发现是在矩阵的边上、、、所以要结合样例理解题目啊

    一个加热器可以确定一个圆,判断每个人是否在圆内或圆上;输出不在圆内或圆上的人的个数即可。遍历这个矩形的边长跑一边就好了,时间复杂度O(4*n^2)大概。

AC代码:

#include
#include
#include
using namespace std;
int x1,x2,y1,y2;
int n,ans;
int x[1005],y[1005],r[1005];
int main()
{
	
	cin>>x1>>y1>>x2>>y2;//x1,y1,左下 
	cin>>n;
	for(int i = 1; i<=n; i++) {
		scanf("%d%d%d",x+i,y+i,r+i);
	}
	
	if(x1 > x2) swap(x1,x2);
	if(y1 > y2) swap(y1,y2);
	int j=y1;
	for(int i = x1; i<=x2; i++) {
		for(int k = 1; k<=n; k++) {
			if((i-x[k])*(i-x[k]) + (j-y[k])*(j-y[k]) <= r[k]*r[k]) {
				ans++;break;
			}
		}
	}
	j=y2;
	for(int i = x1; i<=x2; i++) {
		for(int k = 1; k<=n; k++) {
			if((i-x[k])*(i-x[k]) + (j-y[k])*(j-y[k]) <= r[k]*r[k]) {
				ans++;break;
			}
		}
	}	
	j = x1;
	for(int i = y1 + 1; i<=y2-1; i++) {
		for(int k = 1; k<=n; k++) {
			if((i-y[k])*(i-y[k]) + (j-x[k])*(j-x[k]) <= r[k]*r[k]) {
				ans++;break;
			}
		}
	}
	j = x2;
	for(int i = y1 + 1; i<=y2-1; i++) {
		for(int k = 1; k<=n; k++) {
			if((i-y[k])*(i-y[k]) + (j-x[k])*(j-x[k]) <= r[k]*r[k]) {
				ans++;break;
			}
		}
	}	
//	cout << ans << endl;
	printf("%d\n", (x2-x1+1)*2 + (y2-y1-1)*2 - ans);
	return 0 ;
}

 

你可能感兴趣的:(水题纪念,Codeforce~,计算几何)