2019.02.21 bzoj5317: [Jsoi2018]部落战争(凸包+Minkowski和)

传送门
题意:qqq次询问把一个凸包整体加一个向量(x,y)(x,y)(x,y)之后是否与另外一个凸包相交。


思路:转化一下发现只要会求A+B={v⃗=a⃗+b⃗∣a⃗∈A,b⃗∈B}A+B=\{\vec v=\vec a+\vec b|\vec a\in A,\vec b\in B\}A+B={v=a+baA,bB}即可,这个要用到一个叫做MinkowskiMinkowskiMinkowski和的东西。
不会的可以画个图,发现最后的向量集组成的凸包每条边都是由A,BA,BA,B中的边拼成的,于是我们提出A,BA,BA,B的所有边然后归并一下即可。
代码:

#include<bits/stdc++.h>
#define int long long
#define ri register int
using namespace std;
inline int read(){
    int ans=0;
    bool f=1;
    char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f^=1;ch=getchar();}
    while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
    return f?ans:-ans;
}
typedef long long ll;
const int N=1e5+5;
struct pot{
    ll x,y;
    pot(ll _x=0,ll _y=0):x(_x),y(_y){};
    friend inline pot operator+(const pot&a,const pot&b){return pot(a.x+b.x,a.y+b.y);}
    friend inline pot operator-(const pot&a,const pot&b){return pot(a.x-b.x,a.y-b.y);}
    friend inline ll operator^(const pot&a,const pot&b){return a.x*b.y-a.y*b.x;}
    friend inline bool operator<(const pot&a,const pot&b){return a.x==b.x?a.y<b.y:a.x<b.x;}
}A[N<<1],a1[N],a2[N];
inline void graham(pot a[],int&n){
    static int q[N],top;
    static pot b[N];
    sort(a+1,a+n+1);
    q[top=1]=1;
    for(ri i=2;i<=n;++i){
        while(top>1&&((a[i]-a[q[top-1]])^(a[q[top]]-a[q[top-1]]))<=0)--top;
        q[++top]=i;
    }
    for(ri len=top,i=n-1;i;--i){
        while(top>len&&((a[i]-a[q[top-1]])^(a[q[top]]-a[q[top-1]]))<=0)--top;
        q[++top]=i;
    }
    n=top;
    for(ri i=1;i<=n;++i)b[i]=a[q[i]];
    memcpy(a,b,sizeof(b));
}
inline void Minkowski(pot a[],int&tot,pot x[],int n,pot y[],int m){
    static int pa,pb;
    a[tot=1]=x[pa=1]+y[pb=1];
    while(pa<n&&pb<m){
        pot ta=x[pa+1]-x[pa],tb=y[pb+1]-y[pb];
        ++tot;
        if((ta^tb)<=0)a[tot]=a[tot-1]+ta,++pa;
        else a[tot]=a[tot-1]+tb,++pb;
    }
    while(pa<n)++tot,a[tot]=a[tot-1]+x[pa+1]-x[pa],++pa;
    while(pb<m)++tot,a[tot]=a[tot-1]+y[pb+1]-y[pb],++pb;
    --tot;
    graham(a,tot);
    --tot;
}
int n,m,len,q;
inline bool check(const pot&p){
    if(((p-A[1])^(A[len]-A[1]))>0)return 0;
    if(((p-A[1])^(A[2]-A[1]))<0)return 0;
    int l=2,r=len-1,ans=2;
    while(l<=r){
        int mid=l+r>>1;
        if(((p-A[1])^(A[mid]-A[1]))>=0)l=mid+1,ans=mid;
        else r=mid-1;
    }
    return ((p-A[ans])^(A[ans+1]-A[ans]))>=0;
}
signed main(){
    n=read(),m=read(),q=read();
    for(ri i=1;i<=n;++i)a1[i].x=read(),a1[i].y=read();
    for(ri i=1;i<=m;++i)a2[i].x=-read(),a2[i].y=-read();
    graham(a1,n),graham(a2,m);
    Minkowski(A,len,a1,n,a2,m);
    for(ri x,y;q;--q)x=read(),y=read(),cout<<check(pot(x,y))<<'\n';
    return 0;
}


转载于:https://www.cnblogs.com/ldxcaicai/p/10582401.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值