【BZOJ5317】【JSOI2018】—部落战争(凸包+闵可夫斯基和)

传送门

发现向量xx不合法其实就是可以从Ax=BA-x=B
也就是x=ABx=A-B
似乎叫闵可夫斯基和?
建出凸包后看点在不在凸包内就可以了

#include<bits/stdc++.h>
using namespace std;
#define ll long long
inline int read(){
    char ch=getchar();
    int res=0,f=1;
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=getchar();
    return res*f;
}
const int N=100005;
struct point{
    double x,y;
    point(double _x=0,double _y=0):x(_x),y(_y){}
    friend inline point operator +(const point &a,const point &b){
        return point(a.x+b.x,a.y+b.y);
    }
    friend inline point operator -(const point &a,const point &b){
        return point(a.x-b.x,a.y-b.y);
    }
    friend inline double operator *(const point &a,const point &b){
        return a.x*b.y-a.y*b.x;	
    }
    inline double calc(){
        return x*x+y*y;
    }
}p1[N],p2[N],q[N],t1[N],t2[N];
int n,m,k,top;
inline bool comp(const point &a,const point &b){
    double res=(a-q[1])*(b-q[1]);
    return (res==0)?((a-q[1]).calc()<(b-q[1]).calc()):(res>0);
}
int graham(point *p,int cnt)
{
    for(int i=1;i<=cnt;i++)q[i]=p[i];
    for(int i=2;i<=cnt;i++)if(q[i].x<q[1].x||q[i].x==q[1].x&&q[i].y<q[1].y)swap(q[1],q[i]);
    sort(q+2,q+cnt+1,comp);top=1;
    for(int i=2;i<=cnt;i++)
    {
        while(top>=2&&(q[top]-q[top-1])*(q[i]-q[top-1])<=0)top--;
        q[++top]=q[i];
    }
    for(int i=1;i<=top;i++)p[i]=q[i];p[top+1]=p[1];
    return top;
}
point p[N];int tot;
inline int check(point a){
    if((a-p[1])*(p[2]-p[1])>0||(a-p[1])*(p[tot]-p[1])<0)return 0;
    int l=2,r=tot,res=2;
    while(l<=r){
        int mid=((l+r)>>1);
        if((p[mid]-p[1])*(a-p[1])>=0)res=mid,l=mid+1;
        else r=mid-1;
    }
    return (p[res%tot+1]-p[res])*(a-p[res])>=0;
}
int main(){
    n=read(),m=read(),k=read();
    for(int i=1;i<=n;i++)p1[i].x=read(),p1[i].y=read();
    for(int i=1;i<=m;i++)p2[i].x=-read(),p2[i].y=-read();
    n=graham(p1,n),m=graham(p2,m);
    for(int i=1;i<=n;i++)t1[i]=p1[i+1]-p1[i];
    for(int i=1;i<=m;i++)t2[i]=p2[i+1]-p2[i];
    int f1=1,f2=1;p[tot=1]=p1[1]+p2[1];
    while(f1<=n&&f2<=m)p[++tot]=p[tot-1]+((t1[f1]*t2[f2]>=0)?t1[f1++]:t2[f2++]);
    while(f1<=n)p[++tot]=p[tot-1]+t1[f1++];
    while(f2<=m)p[++tot]=p[tot-1]+t2[f2++];
    tot=graham(p,tot);
    while(k--){
        point a;a.x=read(),a.y=read();
        cout<<check(a)<<'\n' ;
    }
}
posted @ 2019-03-08 12:15  Stargazer_cykoi  阅读(172)  评论(0编辑  收藏  举报