凸包模板 / 优化

const double eps = 1e-9;

struct Point  									//保存点
{
    int x, y;
    Point(){}
    Point(int x, int y):x(x),y(y){}
    Point operator - (Point &b)					//重构减法
    {
    	return Point(x-b.x, y-b.y);
    }
    bool operator < (Point p)
    {
    	if(y!=p.y) return y<p.y;
    	return x<p.x;
    }
};

int sgn(int d)				//判断d的符号函数,d大于0返回1,小于0返回-1,等于0返回0 
{
    if(d<eps) return -1;
    return d>eps;
}

int dcmp(double x,double y)                   //两者想等返回0,x > y返回 1,x < y返回 0 
{
	if(fabs(x - y) < eps) return 0;
	if(x > y) return 1;
	return -1; 
} 

//向量叉乘
double Cross(Point A, Point B) return A.x*B.y-A.y*B.x;

//向量点乘 
double Dot(Point A, Point B) return A.x*B.x + A.y*B.y;

//判断p点是否在线段a1a2上
bool OnSegment(Point p, Point a1, Point a2)              //三点共线原理判定 
{
    return dcmp(Cross(a1-p, a2-p)) == 0 && dcmp(Dot(a1-p, a2-p)) < 0;
}

//多边形有向面积,可以计算任意形状多边形 
double PolygonArea(vector<Point> p)          //p为端点集合,n为端点个数
{
	int n = p.size();
    double s = 0;
    for(int i = 1; i < n-1; ++i)
        s += Cross(p[i]-p[0], p[i+1]-p[0]);
    return s;
}

//判断点是否在多边形内,若点在多边形内返回1,在多边形外部返回0,在多边形上返回-1
int isPointInPolygon(Point p,vector<Point> con) 	// 二分效率高
{
	int k = con.size();
    if(k<3) return 0;
    if(Cross(p-con[0],con[1]-con[0])>-eps) return 0;
    if(Cross(p-con[0],con[k-1]-con[0])<eps) return 0;
 	int i=2,j=k-1;
    int line=-1;
    while(i<=j)
    {
        int mid=(i+j)>>1;
        if(Cross(p-con[0],con[mid]-con[0])>-eps)
        {
            line=mid;
            j=mid-1;
        }
        else i=mid+1;
    }
    if(line>0)
    	return Cross(p-con[line-1],con[line]-con[line-1])<-eps;
    return 0;
}

vector<Point> ConvexHull(vector<Point> p)				//求凸包
{
	t = p.size(); if(t<=1) return p;
	sort(p.begin(), p.end());
	vector<Point> q(t*2); int k = 0;
	for(register int i=0; i<t; q[k++]=p[i++])
		while(k > 1 && sgn(Cross(q[k-1]-q[k-2], p[i]-q[k-1])) <= 0) --k;
	for(register int i=t-2, j=k; i>=0; q[k++]=p[i--])
		while(k > j && sgn(Cross(q[k-1]-q[k-2], p[i]-q[k-1])) <= 0) --k;
	q.resize(k-1);
	return q;
}

Gym - 100941D
VJudge

给你n个嵌套的凸多边形,再给你m个点坐标,求m个点所在封闭区域的面积和。 (精度1e-6)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<iomanip>
#include<vector>
#define llong long long
#define For(i,a,n) for(register llong i=a;i<=n;++i)
#define RF(i,a,n) for(register llong i=a;i>=n;--i)
#define maxn 100005
#pragma GCC optimize(2)
using namespace std;
const double eps = 1e-7;
inline void in(llong &x){
    llong y=1; char c=getchar(); x=0;
    while(c<'0'||c>'9'){
        if(c=='-') y=-1;
        c=getchar();
    }
    while(c<='9'&&c>='0') x=(x<<1)+(x<<3)+c-'0',c=getchar();
    x*=y;
}

struct Point{  //保存点
    llong x, y;
    Point(){}
    Point(llong x, llong y):x(x),y(y){}
    Point operator - (Point &b){  //重构减法
    	return Point(x-b.x, y-b.y);
    }
    bool operator < (Point p){
    	if(y!=p.y) return y<p.y;
    	return x<p.x;
    }
};
struct node{  //存面积以及序号
	double mj;
	int id;
	bool operator < (const node &b)const{
		return mj < b.mj;
	}
}a[maxn];

llong n, pnum, m;
llong ans;
int flag[maxn];
vector<Point> poly[maxn];

inline llong sgn(llong d){
    if(d<eps) return -1;
    return d>eps;
}

inline llong Cross(Point A, Point B) { return A.x*B.y-A.y*B.x;} // ×乘

inline llong PolygonArea(vector<Point> p){  //求凸包面积(多边形面积)
	llong n = p.size();  //很重要!  不能将n当参数传进来
    llong ans = 0;
    for(llong i = 1; i < n-1; ++i)
        ans += Cross(p[i]-p[0], p[i+1]-p[0]);
    return ans;
}

inline bool isPointInPolygon(Point p,vector<Point> con)//二分判断一个点是否在凸包内
{
	int k = con.size();
    if(k<3) return 0;
    if(Cross(p-con[0],con[1]-con[0])>-eps) return 0;
    if(Cross(p-con[0],con[k-1]-con[0])<eps) return 0;
 	int i=2,j=k-1;
    int line=-1;
 
    while(i<=j)
    {
        int mid=(i+j)>>1;
        if(Cross(p-con[0],con[mid]-con[0])>-eps)
        {
            line=mid;
            j=mid-1;
        }
        else i=mid+1;
    }
    if(line>0)
    	return Cross(p-con[line-1],con[line]-con[line-1])<-eps;
    return 0;
}

inline vector<Point> ConvexHull(vector<Point> p, llong t){//凸包优化?
	if(t<=1) return p;
	sort(p.begin(), p.end());
	vector<Point> q(t*2);
	llong k = 0;
	for(register llong i=0; i<t; q[k++]=p[i++])
		while(k>1&&sgn(Cross(q[k-1]-q[k-2], p[i]-q[k-1]))<=0) --k;
	for(register llong i=t-2, j=k; i>=0; q[k++]=p[i--])
		while(k>j&&sgn(Cross(q[k-1]-q[k-2], p[i]-q[k-1]))<=0) --k;
	q.resize(k-1);
	return q;
}

int main(){
	//freopen("castle.in","r",stdin);
	//freopen("castle.out","w",stdout);
	in(n);
	For(i,1,n){
		in(pnum);
		poly[i].resize(pnum);//预留空间  数据大时比push_back快
		For(j,0,pnum-1){
			in(poly[i][j].x); in(poly[i][j].y);
		}
		poly[i] = ConvexHull(poly[i], pnum);//返回的poly的size可能发生变化
		a[i].mj = PolygonArea(poly[i]);//求面积
		a[i].id = i;
	}
	sort(a+1, a+n+1);
	in(m);
	while(m--){
		Point t;
		in(t.x); in(t.y);
		int l = 1, r = n+1;//二分查找点在哪
		while(l<r){
			int mid = (l+r)>>1;
			if(mid == n+1) continue;
			if(isPointInPolygon(t, poly[a[mid].id])){
				r = mid;
			}
			else {
				l = mid+1;
			}
		}
		//注意不要重复计算面积
		if(l != n+1 && !flag[l]) flag[l] = 1, ans+=a[l].mj-a[l-1].mj;
	}
	printf("%0.6f\n", ans/2.0);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值