核心结论:在任意三点不共线的条件下,对于每对符合要求的三角形,有且仅有两条直线,使得该直线把两个三角形分到两个半平面内,且该直线与两个三角形都相交。
感性理解比较简单,直接任取能分开两个三角形的一条直线,然后分别逆时针或顺时针旋转。
于是我们枚举两个点作为切点,把两个半平面内的点的颜色个数算出来就可以了,这一步极角排序一下然后容易双指针找到一个半平面。注意对于两个点分别属于哪个半平面要讨论一下。
#include <bits/stdc++.h>
#define ll long long
using namespace std;
inline ll Read() {
int sig = 1; ll num = 0; char c = getchar();
while(!isdigit(c)) { if(c == '-') sig = -1; c = getchar(); }
while(isdigit(c)) num = (num << 3) + (num << 1) + (c ^ 48), c = getchar();
return num * sig;
}
void Write(ll x) {
if(x < 0) putchar('-'), x = -x;
if(x > 9) Write(x / 10);
putchar((x % 10) ^ 48);
}
const int N = 3005;
int n;
struct tNode {
int x, y, c; double d;
friend bool operator <(tNode a, tNode b) { return a.d < b.d; }
}a[N], b[N << 1];
bool Check(tNode x, tNode y) { return 1ll * x.x * y.y - 1ll * x.y * y.x >= 0; }
int main() {
int i, j, k = 0, tot[3] = {0, 0, 0}; ll ans = 0; n = Read();
for(i = 1; i <= n; i++) a[i].x = Read(), a[i].y = Read(), a[i].c = Read(), tot[a[i].c]++;
for(i = 1; i <= n; i++) {
int t = 0, cnt[3] = {0, 0, 0}; k = 0, cnt[a[i].c] = 1;
for(j = 1; j <= n; j++) if(j != i) b[++k] = {a[j].x - a[i].x, a[j].y - a[i].y, a[j].c, atan2(a[j].y - a[i].y, a[j].x - a[i].x)};
sort(b + 1, b + k + 1); for(j = 1; j <= k; j++) b[j + k] = b[j];
for(j = 1; j <= k; j++) {
while(t < k + j - 1 && Check(b[j], b[t + 1])) cnt[b[++t].c]++;
ll res = 1ll; cnt[b[j].c]--;
if(a[i].c == 0) res = cnt[1] * cnt[2];
else if(a[i].c == 1) res = cnt[0] * cnt[2];
else res = cnt[0] * cnt[1];
if(b[j].c == 0) res *= (tot[1] - cnt[1]) * (tot[2] - cnt[2]);
else if(b[j].c == 1) res *= (tot[0] - cnt[0]) * (tot[2] - cnt[2]);
else res *= (tot[0] - cnt[0]) * (tot[1] - cnt[1]);
ans += res, res = 1ll, cnt[a[i].c]--, cnt[b[j].c]++;
if(b[j].c == 0) res = cnt[1] * cnt[2];
else if(b[j].c == 1) res = cnt[0] * cnt[2];
else res = cnt[0] * cnt[1];
if(a[i].c == 0) res *= (tot[1] - cnt[1]) * (tot[2] - cnt[2]);
else if(a[i].c == 1) res *= (tot[0] - cnt[0]) * (tot[2] - cnt[2]);
else res *= (tot[0] - cnt[0]) * (tot[1] - cnt[1]);
ans += res, cnt[a[i].c]++, cnt[b[j].c]--;
}
}
Write(ans / 4);
}