计算几何——凸包+闵可夫斯基和+二分

· · 题解

能来这的同志大概都是会打凸包的,而关于具体闵可夫斯基和的原理大佬都有讲述,博客上的讲解也很清晰,应该不会有同志理解不能,所以这些就不讲了,我就具体讲讲一些细节问题,和关于我在特判和凸包上WA半天这件事

先上代码

struct node{
    int x,y;
    void read(){ scanf("%lld%lld",&x,&y); }
    void write(){ printf("\n%lld %lld",x,y); }
    void change(){ x=-x,y=-y; }
    int len(){ return x*x+y*y; }
}a[N],b[N],c[N],tmp;
node operator + (node a,node b){ return (node){ a.x+b.x,a.y+b.y }; }
node operator - (node a,node b){ return (node){ a.x-b.x,a.y-b.y }; }
int operator * (node a,node b){ return a.x*b.y-a.y*b.x; }

储存点和向量,结构体运算符重载,因为题目中所有数据均是整数,所以不需要用double

bool cmp(node a,node b){ return a.y==b.y ? a.x<b.x : a.y<b.y; }
bool check(node a,node b){
    int flag=a*b;
    return flag ? flag>0 : a.len()<b.len();
}

比较函数,凸包我用的Graham算法,共线的情况下len小优先,便于后面进行覆盖

int convex(node a[],int n){
    int t=1;
    for(int i=1;i<=n;i++) if(cmp(a[i],a[t]))t=i;
    swap(a[t],a[1]);
    for(int i=2;i<=n;i++)a[i]=a[i]-a[1];
    sort(a+2,a+n+1,check);
    for(int i=2;i<=n;i++)a[i]=a[i]+a[1];
    tot=0;
    for(int i=1;i<=n;i++){
        while(tot>1 && (a[tot]-a[tot-1])*(a[i]-a[tot-1])<=0)tot--;
        a[++tot]=a[i];
    }
    a[tot+1]=a[1];
    return tot;
}

求凸包:

  1. 因为Graham只需找最低点,所以遍历一遍即可,不需要整体排序
  2. 计算向量,按极角排序,使之逆时针旋转
  3. 因为最后需要进行点转向量求闵可夫斯基和,故a[tot+1]=a[1]便于对i=tot时进行减法
    void minkowski(){
     c[tot=1]=a[1]+b[1];
     for(int i=1;i<=n;i++)a[i]=a[i+1]-a[i];
     for(int i=1;i<=m;i++)b[i]=b[i+1]-b[i];
     int l=1,r=1;
     while(l<=n && r<=m) c[++tot]=a[l]*b[r]>=0 ? c[tot-1]+a[l++] : c[tot-1]+b[r++];
     while(l<=n) c[++tot]=c[tot-1]+a[l++];
     while(r<=m) c[++tot]=c[tot-1]+b[r++];
     tot=convex(c,tot);
     for(int i=2;i<=tot;i++)c[i]=c[i]-c[1];
    }

    求闵可夫斯基和:

  4. 可能存在共线的情况,对点集求一次凸包
  5. 将点集转换成向量集,用于判断点是否在凸包内

//另不能在tot=convex(c,tot)前进行一次tot--,蒟蒻开始想用来排除最后返回的起点,可惜的是不能这么干,因为我全WA掉了(有没有大佬出来解释解释嘤嘤嘤~),但反正求凸包的时候会把两个起点排掉一个……

bool query(node x){
    x=x-c[1];
    if(x*c[2]>0 || x*c[tot]<0)return 0;
    if(x*c[tot]==0){
        if(x.len()<=c[tot].len()) return 1;
        else return 0;
    }
        int pos=lower_bound(c+2,c+tot+1,x,check)-c-1;
        return (c[pos%tot+1]-c[pos])*(x-c[pos])>=0;
}

查询操作:

  1. lower_bound( )二分判断向量x-c[1]在凸包中的逆时针排序的哪两条向量之间
  2. 提前排除x在第一条边和最后一条边之外的情况,因为后面的判断没法排除这两种情况
  3. 判断点在边的左侧还是右侧

完整代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
int n,m,q,tot;
struct node{
    int x,y;
    void read(){ scanf("%lld%lld",&x,&y); }
    void write(){ printf("\n%lld %lld",x,y); }
    void change(){ x=-x,y=-y; }
    int len(){ return x*x+y*y; }
}a[N],b[N],c[N],tmp;
node operator + (node a,node b){ return (node){ a.x+b.x,a.y+b.y }; }
node operator - (node a,node b){ return (node){ a.x-b.x,a.y-b.y }; }
int operator * (node a,node b){ return a.x*b.y-a.y*b.x; }//大于零 a在b右

bool cmp(node a,node b){ return a.y==b.y ? a.x<b.x : a.y<b.y; }
bool check(node a,node b){
    int flag=a*b;
    return flag ? flag>0 : a.len()<b.len();
}

int convex(node a[],int n){
    int t=1;
    for(int i=1;i<=n;i++) if(cmp(a[i],a[t]))t=i;
    swap(a[t],a[1]);
    for(int i=2;i<=n;i++)a[i]=a[i]-a[1];
    sort(a+2,a+n+1,check);
    for(int i=2;i<=n;i++)a[i]=a[i]+a[1];
    tot=0;
    for(int i=1;i<=n;i++){
        while(tot>1 && (a[tot]-a[tot-1])*(a[i]-a[tot-1])<=0)tot--;
        a[++tot]=a[i];
    }
    a[tot+1]=a[1];
    return tot;
}

void minkowski(){
    c[tot=1]=a[1]+b[1];
    for(int i=1;i<=n;i++)a[i]=a[i+1]-a[i];
    for(int i=1;i<=m;i++)b[i]=b[i+1]-b[i];
    int l=1,r=1;
    while(l<=n && r<=m) c[++tot]=a[l]*b[r]>=0 ? c[tot-1]+a[l++] : c[tot-1]+b[r++];
    while(l<=n) c[++tot]=c[tot-1]+a[l++];
    while(r<=m) c[++tot]=c[tot-1]+b[r++];
    //for(int i=1;i<=tot;i++)c[i].write();
    //printf("\n");
    tot=convex(c,tot);
    for(int i=2;i<=tot;i++)c[i]=c[i]-c[1];
    //for(int i=1;i<=tot;i++) c[i].write();
    //printf("\n");
}

bool query(node x){
    x=x-c[1];
    if(x*c[2]>0 || x*c[tot]<0)return 0;
    if(x*c[tot]==0){
        if(x.len()<=c[tot].len()) return 1;
        else return 0;
    }
    int pos=lower_bound(c+2,c+tot+1,x,check)-c-1;
    return (c[pos%tot+1]-c[pos])*(x-c[pos])>=0;
}

signed main(){
    scanf("%lld%lld%lld",&n,&m,&q);
    for(int i=1;i<=n;i++)a[i].read();
    for(int i=1;i<=m;i++)b[i].read(),b[i].change();
    n=convex(a,n);m=convex(b,m);
    minkowski();
    for(int i=1;i<=q;i++){
        tmp.read();
        cout<<query(tmp)<<endl;
    }
    return 0;
}