CF 87E 题解 Mogohu-Rea Idol

看了题解敲出来的,还wa了那么多发,吸取了一个教训,判断点在多边形内还有多边形上,要保证凸包任意三点不共线,否则会出错,

题解也很简单。。就是对三个凸包求Minkowski和,然后判断点是否在凸多边形内即可

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
#define eps 1e-8
int sig(double x) {return (x>eps)-(x<-eps);}
const double pi=acos(-1.0);
struct P
{
    double x,y,ang;
    P(double a=0,double b=0):x(a),y(b){}
    P operator +(const P &a)const{
        return P(x+a.x,y+a.y);
    }
    P operator -(const P &a)const{
        return P(x-a.x,y-a.y);
    }
    P operator *(const double a)const{
        return P(x*a,y*a);
    }
    P operator /(const double a)const{
        return P(x/a,y/a);
    }
    double operator *(const P &a)const{
        return x*a.x+y*a.y;
    }
    double operator ^(const P &a)const{
        return x*a.y-y*a.x;
    }
    bool operator ==(const P &a)const{
        return sig(x-a.x)==0&&sig(y-a.y)==0;
    }
    bool on(P a,P b)
    {
        if(a==*this||b==*this) return 1;
        P v1=a-*this,v2=b-*this;
        return !sig(v1^v2)&&sig(v1*v2)<=0;
    }
};
double across(P a,P b,P c)
{
    return (b-a)^(c-a);
}
int cmp1(P a,P b)
{
    if(sig(a.x-b.x)!=0) return a.x<b.x;
    else return a.y<b.y;
}
int Graham(P *p,int n,P *q)
{
    sort(p,p+n,cmp1);
    int m=0;
    for(int i=0;i<n;i++)
    {
        while(m>1&&sig((q[m-1]-q[m-2])^(p[i]-q[m-2]))<=0) m--;
        q[m++]=p[i];
    }
    int k=m;
    for(int i=n-2;i>=0;i--)
    {
        while(m>k&&sig((q[m-1]-q[m-2])^(p[i]-q[m-2]))<=0) m--;
        q[m++]=p[i];
    }
    if(n>1) m--;
    return m;
}
int inside(P a,P b,P c,P p)
{
    double s=fabs(across(a,b,c));
    double sa=fabs(across(a,b,p));
    double sb=fabs(across(a,c,p));
    double sc=fabs(across(b,c,p));
    return (s==(sa+sb+sc));
}
bool inpoly(P a,P *p,int n)
{
    if(across(p[0],p[1],a)<0||across(p[0],p[n-1],a)>0) return 0;
    int r=n-1,l=1;
    while(r-l>1)
    {
        int m=(l+r)/2;
        if(across(p[0],p[m],a)>=0) l=m;
        else r=m;
    }
    return inside(p[0],p[l],p[r],a);
}
P p1[50010],p2[50010],p3[50010],p[150010],pp[150010],q[150010];
int cnt;
P input(int n,P *ch)
{
    P a;
    for(int i=0;i<n;i++)
        {
            scanf("%lf%lf",&ch[i].x,&ch[i].y);
            if(i==0) a=ch[i];
            else
            {
                if(a.y>ch[i].y) a=ch[i];
                else if(sig(a.y-ch[i].y)==0)
                {
                    if(ch[i].x<a.x) a=ch[i];
                }
            }
        }
        ch[n]=ch[0];
    for(int i=1;i<=n;i++)
    {
        pp[cnt]=ch[i]-ch[i-1];
        pp[cnt].ang=atan2(pp[cnt].y,pp[cnt].x);
        if(sig(pp[cnt].ang)<0) pp[cnt].ang+=2*pi;
        cnt++;
    }
    return a;
}
int cmp(P a,P b)
{
    return a.ang<b.ang;
}
int main()
{
    int n,m;
    //freopen("out.txt","w",stdout);
    while(scanf("%d",&n)!=EOF)
    {
        cnt=0;
        P a=input(n,p1);
        scanf("%d",&n);
        a=a+input(n,p2);
        scanf("%d",&n);
        a=a+input(n,p3);
        p[0]=a;
        sort(pp,pp+cnt,cmp);
        for(int i=1;i<=cnt;i++)
        {
            p[i]=p[i-1]+pp[i-1];
        }
        cnt=Graham(p,cnt,q);
        //for(int i=0;i<cnt;i++)
            //cout<<q[i].x<<" "<<q[i].y<<endl;
        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            P b;
            scanf("%lf%lf",&b.x,&b.y);
            b=b*3;
            //printf("%lf %lf\n",b.x,b.y);
            if(inpoly(b,q,cnt)==1)
                printf("YES\n");
            else
                printf("NO\n");
        }
    }
    return 0;
}
/*
4
3 3
6 3
6 6
3 6

4
-3 3
-3 6
-6 6
-6 3

4
3 -3
3 -6
6 -6
6 -3

36
-1 -1
-1 0
-1 1
-1 2
-1 3
-1 4
0 -1
0 0
0 1
0 2
0 3
0 4
1 -1
1 0
1 1
1 2
1 3
1 4
2 -1
2 0
2 1
2 2
2 3
2 4
3 -1
3 0
3 1
3 2
3 3
3 4
4 -1
4 0
4 1
4 2
4 3
4 4
*/


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值