codeforces 13D. Triangles(计算几何 dp)

D. Triangles
time limit per test
2 seconds
memory limit per test
64 megabytes
input
standard input
output
standard output

Little Petya likes to draw. He drew N red and M blue points on the plane in such a way that no three points lie on the same line. Now he wonders what is the number of distinct triangles with vertices in red points which do not contain any blue point inside.

Input

The first line contains two non-negative integer numbers N and M (0 ≤ N ≤ 5000 ≤ M ≤ 500) — the number of red and blue points respectively. The following N lines contain two integer numbers each — coordinates of red points. The following M lines contain two integer numbers each — coordinates of blue points. All coordinates do not exceed 109 by absolute value.

Output

Output one integer — the number of distinct triangles with vertices in red points which do not contain any blue point inside.

Examples
input
4 1
0 0
10 0
10 10
5 4
2 1
output
2
input
5 5
5 10
6 1
8 6
-6 -7
7 -1
5 -1
10 -4
-10 -8
-10 5
-2 -8
output
7

昨晚刚向大神求助的

题意:

二维坐标系中有 nn个红点,m 个蓝点,任意三个点不在同一直线,问这 nn个红点能组成几个三角形(组成的三角形内部没有蓝点)。

思路:

暴力枚举 3个红点,O(1)数内部的蓝点个数。
后面这个 O(1)可以这么做,选一个红点 O 作为原点,预处理count(A,B) 表示三角形 OAB 内的蓝点个数。那么 ABC内的点数可以通过预处理好的 count(A,B),count(B,C),count(C,A) 加加减减得到。
这样做是 O(n^3 + n^2 m) 的。

用叉积判断蓝点是不是在三角形中,不太懂的可以看这个,点击打开链接


#include 
using namespace std;
#define ll __int64
struct node {
	ll x, y;
	node() {}
	node(int xi, int yi):x(xi), y(yi) {}
	node operator - (const node &t)const {
		return node(x-t.x, y-t.y);
	}
	ll operator * (const node &t)const {
		return x*t.y-y*t.x;
	}
}r[550], b[550], o;
int dp[550][550];

int main() {
	int n, m;
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; i++) {
		scanf("%I64d%I64d", &r[i].x, &r[i].y);
	}
	for(int i = 1; i <= m; i++) {
		scanf("%I64d%I64d", &b[i].x, &b[i].y);
	}
	o.x = 1e9 + 1, o.y = 1e9 + 1;
	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= n; j++) {
			if((r[i]-o)*(r[j]-o) <= 0) continue;
			for(int k = 1; k <= m; k++) {
				if((r[i]-o)*(b[k]-o)>=0 && (r[j]-r[i])*(b[k]-r[i])>0 && (o-r[j])*(b[k]-r[j])>0) 
					dp[i][j]++;
			}
			dp[j][i] = -dp[i][j];
		}
	}
	ll ans = 0;
	for(int i = 1; i <= n; i++) {
		for(int j = i+1; j <= n; j++) {
			for(int k = j+1; k <= n; k++) {
				ans += (dp[i][j]+dp[j][k]+dp[k][i] == 0);
			}
		}
	}
	printf("%I64d\n", ans);
	return 0;
} 





你可能感兴趣的:(codeforces 13D. Triangles(计算几何 dp))