UVALive - 7281 Saint John Festival logn判断点是否在凸多边形内

题目链接点这里

对于判断点是否在凸多边形内有logn的算法,,可以看这一篇博客

HLG 1429 凸多边形【快速判断点在凸多边形内】



#include<iostream>
#include<cstdio>
#include<math.h>
#include<algorithm>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<queue>
#include<string.h>
#include<cstring>
#include<vector>
#include<time.h>
#include<stdlib.h>
using namespace std;
#define INF 0x3f3f3f3f
#define INFLL 0x3f3f3f3f3f3f3f3f
#define FIN freopen("input.txt","r",stdin);
#define sf(x,y) scanf("x",y)
#define pf(x,y) printf("x",y)
#define mem(x,y) memset(x,y,sizeof(x))
typedef unsigned long long ULL;
typedef long long LL;
#define fuck(x) cout<<"["<<x<<"]"<<endl;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef pair<pair<LL,LL>,LL> PIII;
typedef pair<LL,LL> PII;
const int MX=1e4+10;
double eps=1e-8;
int L,S;
struct Point {
    double x, y;
    Point() {}
    Point(double x,double y):x(x),y(y) {}
    bool operator<(const Point&b) const {
        if(x != b.x) return x < b.x;
        return y < b.y;
    }
    Point operator-(const Point &P) const {
        return Point(x - P.x, y - P.y);;
    }
    double operator^(const Point &P) const { //叉积
        return x * P.y - y * P.x;//如果改成整形记得加LL
    }
    double operator*(const Point &P) const { //点积
        return x * P.x + y * P.y;//如果改成整形记得加LL
    }
};
typedef Point Vector;

int sign(double x) {
    if(fabs(x)<eps)return 0;
    return x<0?-1:1;
}
int dcmp(double a,double b) {
    if(fabs(a-b)<eps)return 0;
    else if(a>b)return 1;
    else return -1;
}
Point P[MX], R[MX];
int convex(int n) {
    int m = 0, k;
    sort(P, P + n);
    for(int i = 0; i < n; i++) {
        while(m > 1 && sign((R[m - 1]-R[m-2])^(P[i]-R[m - 2]))<= 0) m--;
        R[m++] = P[i];
    }
    k = m;
    for(int i = n - 2; i >= 0; i--) {
        while(m > k && sign((R[m - 1]-R[m-2])^(P[i]-R[m - 2]))<= 0) m--;
        R[m++] = P[i];
    }
    if(n > 1) m--;
    return m;
}
//nlogn计算x是否在凸多边形内
int is_in(Point P,Point *R,int n) { //-1不在,0在边上,1在内部
    int l=1, r=n-2,mid;
    while(l<=r) {
        mid = (l+r) >> 1;
        int t1 = sign((R[mid]-R[0])^(P-R[0]));
        int t2 = sign((R[mid+1]-R[0])^(P-R[0]));
        if(t1>=0 &&t2<=0) {
            int t3=sign((P-R[mid])^(P-R[mid+1]));
            if(t3 < 0) return -1;
            else if(t1||t2||t3)return 0;
            return 1;
        } else if(t1 < 0)r = mid -1;
        else l = mid+1;
    }
    return -1;
}
int main() {
    FIN;
    while(~scanf("%d",&L)) {
        for(int i=0; i<L; i++)scanf("%lf%lf",&P[i].x,&P[i].y);
        L=convex(L);
        scanf("%d",&S);
        int ans=0;
        for(int i=0; i<S; i++) {
            double x,y;
            scanf("%lf%lf",&x,&y);
            if(is_in(Point(x,y),R,L)>=0)ans++;
        }
        cout<<ans<<endl;
    }
    return 0;
}


评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值