【题目链接】
【思路要点】
- 考虑枚举 A , D A,D A,D ,那么显然选择 B , C B,C B,C 和 E , F E,F E,F 的方案数是独立的。
- 枚举 D D D ,将剩余点对 D D D 极角排序,按序枚举 A A A ,则选择 E , F E,F E,F 的方案数可以通过用哈希表维护一定角度内所有点到 D D D 距离的平方来实现。
- 考虑选择 B , C B,C B,C 的方案数,我们要求 B C ⊥ A D BC\bot AD BC⊥AD 且 A D AD AD 过 B C BC BC 中点。
- 因此,一对点 ( B , C ) (B,C) (B,C) 可以通过其中点 M i d Mid Mid 和一个方向 D i r Dir Dir 来描述,对于一对 A , D A,D A,D ,我们只需找到与其方向 D i r Dir Dir 一致的,在线段 A D AD AD 上的 M i d Mid Mid 的个数即可,可以二分找到对应的区间长度。
- 时间复杂度 O ( N 2 L o g N ) O(N^2LogN) O(N2LogN) 。
- 由于坐标值域较大,需避免使用实数运算。
【代码】
#include<bits/stdc++.h> using namespace std; const int MAXN = 1e3 + 5; const int MAXM = 1e6 + 5; typedef long long ll; template <typename T> void chkmax(T &x, T y) {x = max(x, y); } template <typename T> void chkmin(T &x, T y) {x = min(x, y); } template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } template <typename T> void write(T x) { if (x < 0) x = -x, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 + '0'); } template <typename T> void writeln(T x) { write(x); puts(""); } struct point {int x, y; }; struct line {point dir, mid; }; point operator + (point a, point b) {return (point) {a.x + b.x, a.y + b.y}; } point operator - (point a, point b) {return (point) {a.x - b.x, a.y - b.y}; } ll operator * (point a, point b) {return 1ll * a.x * b.y - 1ll * a.y * b.x; } ll dot(point a, point b) {return 1ll * a.x * b.x + 1ll * a.y * b.y; } ll moo(point a) {return 1ll * a.x * a.x + 1ll * a.y * a.y; } bool where(point a) {return a.y > 0 || (a.y == 0 && a.x < 0); } point normal(point a) { int g = __gcd(a.x, a.y); a.x /= g, a.y /= g; if (a.x < 0 && (a.x == 0 && a.y < 0)) { a.x *= -1; a.y *= -1; } return a; } bool operator < (point a, point b) { if (a.x == b.x) return a.y < b.y; else return a.x < b.x; } bool operator < (line a, line b) { if (a.dir < b.dir || b.dir < a.dir) return a.dir < b.dir; else if (dot(a.dir, a.mid) != dot(b.dir, b.mid)) return dot(a.dir, a.mid) < dot(b.dir, b.mid); else return a.mid < b.mid; } bool cmp(point a, point b) { if (where(a) != where(b)) return where(a) < where(b); else return a * b > 0; } ll ans; point a[MAXN]; int tot; line lines[MAXM]; int main() { int n; read(n); for (int i = 1; i <= n; i++) read(a[i].x), read(a[i].y); for (int i = 1; i <= n; i++) for (int j = i + 1; j <= n; j++) lines[++tot] = (line) {normal(a[i] - a[j]), a[i] + a[j]}; sort(lines + 1, lines + tot + 1); for (int i = 1; i <= n; i++) { static point b[MAXN * 2]; int tcnt = 0; for (int j = 1; j <= n; j++) if (j != i) b[++tcnt] = a[j] - a[i]; sort(b + 1, b + n, cmp); for (int j = 1, k = n; j <= n - 1; j++, k++) b[k] = b[j]; static unordered_map <ll, int> cnt; int sum = 0, l = 1, r = 1; cnt.clear(); for (int j = 1; j <= n - 1; j++) { while (b[j] * b[r] > 0 || (b[j] * b[r] == 0 && r <= n - 1) || dot(b[j], b[r]) < 0) sum += cnt[moo(b[r++])]++; while (l < r && dot(b[j], b[l]) >= 0) sum -= --cnt[moo(b[l++])]; if (sum) { point dir = normal((point) {b[j].y, -b[j].x}); point lp = a[i] + a[i]; point rp = (a[i] + b[j]) + (a[i] + b[j]); if (rp < lp) swap(lp, rp); int delta = lower_bound(lines + 1, lines + tot + 1, (line) {dir, rp}) - upper_bound(lines + 1, lines + tot + 1, (line) {dir, lp}); ans += 4ll * sum * delta; } } } writeln(ans); return 0; }