https://www.luogu.com.cn/problem/P4557
首先介绍一下闵可夫斯基和
定义:两个欧几里得空间点集的和,也称作两个空间的膨胀集,点集 A A A与 B B B的闵可夫斯基和定义为 A + B = { a + b , a ∈ A , b ∈ B } A+B=\{a+b,a\in A,b\in B\} A+B={a+b,a∈A,b∈B}
那么这道题我们其实要求的是 a a a和 − b -b −b的闵可夫斯基和,只要 k → \overrightarrow{k} k终点在两个空间的膨胀集内部,或者边界,就说明会发生冲突,否则不会
接下来回到这道题,梳理一下思路,我们首先要求出 A A A和 − B -B −B两个凸集的闵可夫斯基和,这个结果是一个凸包,求闵可夫斯基和我们可以使用固定一个初始点然后进行归并,始终把外面的线加进去,这里使用向量叉积来判断线是否在外部, − B -B −B的意思就是坐标都取相反数,算出来要再跑一次 A n d r e w Andrew Andrew防止出现共线,然后进行点是否在凸包内部的二分判定,程序如下
#include
using namespace std;
#define db double
const db eps = 1e-10;
const int MAXN = 1e5 + 100;
int sgn(db x){
if(fabs(x) < eps) return 0;
return x < 0 ? -1 : 1;
}
struct Point{
db x, y;
Point(){}
Point(db x, db y): x(x), y(y){}
Point operator + (const Point &B)const{
return Point(x + B.x, y + B.y);
}
Point operator - (const Point &B)const{
return Point(x - B.x, y - B.y);
}
bool operator < (const Point &B)const{
return sgn(x - B.x) < 0 || (sgn(x - B.x) == 0 && sgn(y - B.y) < 0);
}
bool operator == (const Point &B)const{
return sgn(x - B.x) == 0 && sgn(y - B.y) == 0;
}
}s1[MAXN], s2[MAXN], ch1[MAXN], ch2[MAXN];
Point M[MAXN], ans[MAXN];
typedef Point Vector;
db Cross(Vector A, Vector B){
return A.x * B.y - A.y * B.x;
}
int Convex_hull(Point *s, Point *ch, int n){
int v = 0;
sort(s, s + n);
n = unique(s, s + n) - s;
for(int i=0;i<n;i++){
while(v > 1 && sgn(Cross(ch[v - 1] - ch[v - 2], s[i] - ch[v - 2])) <= 0){
v -= 1;
}
ch[v++] = s[i];
}
int j = v;
for(int i=n-2;i>=0;i--){
while(v > j && sgn(Cross(ch[v - 1] - ch[v - 2], s[i] - ch[v - 2])) <= 0){
v -= 1;
}
ch[v++] = s[i];
}
if(n > 1) v -= 1;
return v;
}
int check(Point A, Point *ch, int n){
int l = 0;
int r = n - 1;
while(r - l > 1){
int mid = ((r - l) >> 1) + l;
db a1 = Cross(ch[mid] - ch[0], A - ch[0]);
db a2 = Cross(ch[mid + 1] - ch[0], A - ch[0]);
if(sgn(a1) >= 0 && sgn(a2) <= 0){
if(sgn(Cross(ch[mid + 1] - ch[mid], A - ch[mid])) >= 0) return 1;
return 0;
}else if(sgn(a1) < 0){
r = mid;
}else{
l = mid;
}
}
return 0;
}
int Minkowski_sum(int n, int m){
int tot;
for(int i=1;i<n;i++){
s1[i] = ch1[i] - ch1[i - 1];
}
s1[n] = ch1[0] - ch1[n - 1];
for(int i=1;i<m;i++){
s2[i] = ch2[i] - ch2[i - 1];
}
s2[m] = ch2[0] - ch2[m - 1];
ans[tot = 0] = s1[0] + s2[0];
int pt1, pt2;
pt1 = pt2 = 1;
while(pt1 <= n && pt2 <= m){
tot += 1;
ans[tot] = ans[tot - 1] + (sgn(Cross(s1[pt1], s2[pt2])) >= 0 ? s1[pt1++] : s2[pt2++]);
}
while(pt1 <= n){
tot += 1;
ans[tot] = ans[tot - 1] + s1[pt1++];
}
while(pt2 <= m){
tot += 1;
ans[tot] = ans[tot - 1] + s2[pt2++];
}
return tot;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, q;
db x, y;
cin >> n >> m >> q;
for(int i=0;i<n;i++){
cin >> s1[i].x >> s1[i].y;
}
int a = Convex_hull(s1, ch1, n);
for(int i=0;i<m;i++){
cin >> s2[i].x >> s2[i].y;
s2[i].x = -s2[i].x;
s2[i].y = -s2[i].y;
}
int b = Convex_hull(s2, ch2, m);
int c = Minkowski_sum(a, b);
c = Convex_hull(ans, M, c);
while(q--){
cin >> x >> y;
cout << check(Point(x, y), M, c) << '\n';
}
return 0;
}