Angle Beats
Given n points P1,P2,⋯,Pn on 2D plane and q queries. In i-th query, a point Ai is given, and you should determine the number of tuples (u,v) that 1≤u<v≤n and Ai,Pu,Pv form a non-degenerate right-angled triangle.
Input
The first line contains two positive integers n,q (2≤n≤2000,1≤q≤2000), denoting the number of given points and the number of queries.
Next n lines each contains two integers xi,yi (|xi|,|yi|≤109), denoting a given point Pi.
Next q lines each contains two integers xi,yi (|xi|,|yi|≤109), denoting a query point Ai.
It is guaranteed that the input n+q points are all pairwise distinct.
Output
Output q lines each contains a non-negative integer, denoting the answer to corresponding query.
Input
4 2
0 1
1 0
0 -1
-1 0
0 0
1 1
Output
4
3
题意
给出n个点,q次询问,每次询问给出一个点,问这个给出的点和原有的点可以组成多少个三角形。
题解
考虑以下两种情况:
- 每次询问给出的点为直角。
- 每次询问给出的点不为直角
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define ll long long
const int maxn = 2010;
struct point {
ll x, y;
int id;
point() {}
point(ll x, ll y, int id = 0) : x(x), y(y), id(id) {}
// 对减法符号进行重载
point friend operator-(point a, point b) {
return {a.x - b.x, a.y - b.y};
}
} p[maxn * 2], q[maxn], tmps[maxn];
// 选择的极点
point tmp;
ll ans[maxn];
// 叉积
ll X(point a, point b) {
return a.x * b.y - a.y * b.x;
}
// 点积
ll Dot(point a, point b) {
return a.x * b.x + a.y * b.y;
}
// 象限
int Quadrant(point a) {
if (a.x > 0 && a.y >= 0) return 1;
if (a.x <= 0 && a.y > 0) return 2;
if (a.x < 0 && a.y <= 0) return 3;
if (a.x >= 0 && a.y < 0) return 4;
}
bool cmp(point a, point b) {
// 象限相等
if (Quadrant(a - tmp) == Quadrant(b - tmp)) {
ll use = X(a - tmp, b - tmp);
if (use > 0) {
return true;
} else if (use == 0) {
return a.x < b.x;
} else {
return false;
}
}
// 象限不等
return Quadrant(a - tmp) < Quadrant(b - tmp);
}
int main() {
int n, m;
while (cin >> n >> m) {
memset(ans, 0, sizeof(ans));
for (int i = 1; i <= n; i++) {
cin >> p[i].x >> p[i].y;
p[i].id = -1, tmps[i] = p[i];
}
for (int i = 1; i <= m; i++) {
cin >> q[i].x >> q[i].y;
q[i].id = i;
}
for (int t = 1; t <= m; t++) {
tmp = q[t];
sort(p + 1, p + 1 + n, cmp);
int pre;
for (int i = 1, j = 1; i <= n; i++) {
if (i > 1 && X(p[i] - tmp, p[i - 1] - tmp) == 0 && Dot(p[i] - tmp, p[i - 1] - tmp) > 0) {
ans[t] += pre;
continue;
}
// 一条线
pre = 0;
// 只考虑转90度以内的情况
while (Dot(p[i] - tmp, p[j] - tmp) >= 0 && X(p[i] - tmp, p[j] - tmp) >= 0) {
if (Dot(p[i] - tmp, p[j] - tmp) == 0) {
ans[t]++, pre++;
}
j = j % n + 1;
// 转回自己
if (i == j) {
break;
}
}
}
}
for (int t = 1; t <= n; t++) {
tmp = tmps[t];
int now = m;
for (int i = 1; i <= m; i++) {
p[i] = q[i];
}
for (int i = 1; i <= n; i++) {
if (i != t) {
p[++now] = tmps[i];
}
}
sort(p + 1, p + 1 + now, cmp);
int pre;
for (int i = 1, j = 1; i <= now; i++) {
if (i > 1 && X(p[i] - tmp, p[i - 1] - tmp) == 0 && Dot(p[i] - tmp, p[i - 1] - tmp) > 0) {
if (p[i].id != -1) {
ans[p[i].id] += pre;
}
continue;
}
// 一条线
pre = 0;
// 只考虑转90度以内的情况
while (Dot(p[i] - tmp, p[j] - tmp) >= 0 && X(p[i] - tmp, p[j] - tmp) >= 0) {
if (p[j].id == -1 && Dot(p[i] - tmp, p[j] - tmp) == 0) {
pre++;
if (p[i].id != -1) {
ans[p[i].id]++;
}
}
j = j % now + 1;
// 转回自己
if (i == j) {
break;
}
}
}
for (int i = now, j = now; i >= 1; i--) {
if (i < now && X(p[i] - tmp, p[i + 1] - tmp) == 0 && Dot(p[i] - tmp, p[i + 1] - tmp) > 0) {
if (p[i].id != -1) {
ans[p[i].id] += pre;
}
continue;
}
// 一条线
pre = 0;
// 顺时针
while (Dot(p[j] - tmp, p[i] - tmp) >= 0 && X(p[j] - tmp, p[i] - tmp) >= 0) {
if (p[j].id == -1 && Dot(p[j] - tmp, p[i] - tmp) == 0) {
pre++;
if (p[i].id != -1) {
ans[p[i].id]++;
}
}
j == 1 ? j = now : (--j);
// 转回自己
if (i == j) {
break;
}
}
}
}
for (int i = 1; i <= m; i++) {
cout << ans[i] << endl;
}
}
return 0;
}