BZOJ 2618 CQOI2006 凸多边形 半平面交

本文详细介绍了如何通过半平面交集算法解决给定多个凸多边形交集面积的问题,并提供了代码实现。通过优化操作减少重复计算,提高效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目大意:给定n个凸多边形,求交集的面积

时隔多年我终于把完整的半平面交搞出来了……真尼玛艰辛……

曾经写了一发 RE到死 于是就搁置0.0 今天写一发又是WA到死的节奏……

不多说直接上代码 其实刘汝佳同学写麻烦了 每次插入一个半平面之后不用两端都删的 只删一端 最后再处理两端的部分就行

300题留念……切了道模板题也不错

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define M 510
#define EPS 1e-7
using namespace std;
struct point{
	double x,y;
	point(){}
	point(double _,double __):
		x(_),y(__){}
	point operator + (const point &p) const
	{
		return point(x+p.x,y+p.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;
	}
	point operator * (const double &rate) const
	{
		return point(x*rate,y*rate);
	}
	void Read()
	{
		scanf("%lf%lf",&x,&y);
	}
}points[M];
struct line{
	point p,v;
	double alpha;
	line(){}
	line(const point &p1,const point &p2):p(p1),v(p2-p1)
	{
		alpha=atan2(v.y,v.x);
	}
	bool operator < (const line &l) const
	{
		return alpha < l.alpha;
	}
}lines[M];
int n,m,tot;
line *q[M];
int r,h;
double ans;
bool Onleft(const point &p,const line &l)
{
	return (l.p-p)*l.v>=0;
}
point Get_Intersection(const line &l1,const line &l2)
{
	point u=l1.p-l2.p;
	double temp=(l2.v*u)/(l1.v*l2.v);
	return l1.p+l1.v*temp;
}
void Get_Half_Plane_Intersection()
{
	int i;
	for(i=1;i<=tot;i++)
	{
		while( r-h>=2 && !Onleft(Get_Intersection(*q[r],*q[r-1]),lines[i]) )
			q[r--]=0x0;
		if( r-h>=1 && fabs(lines[i].v*q[r]->v)<=0 )
			q[r]=Onleft(lines[i].p,*q[r])?&lines[i]:q[r];
		else q[++r]=&lines[i];
	}
	while(1)
	{
		if( r-h>=2 && !Onleft(Get_Intersection(*q[h+1],*q[h+2]),*q[r]) )
			q[++h]=0x0;
		else if( r-h>=2 && !Onleft(Get_Intersection(*q[r],*q[r-1]),*q[h+1]) )
			q[r--]=0x0;
		else
			break;
	}
}
int main()
{
	int i,j;
	cin>>n;
	for(i=1;i<=n;i++)
	{
		point first,p1,p2;
		scanf("%d",&m);
		first.Read();p2=first;
		for(j=2;j<=m;j++)
		{
			p1=p2;p2.Read();
			lines[++tot]=line(p1,p2);
		}
		lines[++tot]=line(p2,first);
	}
	sort(lines+1,lines+tot+1);
	Get_Half_Plane_Intersection();
	if(r-h<=2)
		return puts("0.000"),0;
	tot=0;
	for(i=h+2;i<=r;i++)
		points[++tot]=Get_Intersection(*q[i],*q[i-1]);
	points[++tot]=Get_Intersection(*q[r],*q[h+1]);
	for(i=2;i<=tot;i++)
		ans+=points[i-1]*points[i];
	ans+=points[tot]*points[1];
	printf("%.3lf\n",ans/2);
	return 0;
}




根据引用所述,交错序列是一个仅由0和1构成的序列,其中没有相邻的1(可以有相邻的0)。特征值定义为x^ay^b,其中x和y分别表示0和1出现的次数。长度为n的交错序列可能有多个。问题要求计算所有长度为n的交错序列特征值的和除以m的余数。 根据引用所述,输入文件包含一个行,该行包含三个整数n、a、b和m。其中,1≤n≤10000000,0≤a、b≤45,m<100000000。 为了解决这个问题,可以使用动态规划和矩阵快速幂优化的方法,具体实现可以参考引用提到的相关算法。算法的思路是通过计算长度为n的交错序列的特征值,然后将所有特征值求和并对m取余数。 具体步骤如下: 1. 使用动态规划计算长度为n的所有交错序列的特征值,将结果保存在一个矩阵中。 2. 使用矩阵快速幂优化,将动态规划的过程进行优化。 3. 对优化后的结果进行求和,并对m取余数。 4. 输出结果。 参考引用给出的博客中的代码实现,可以帮助你更好地理解和实现该算法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [BZOJ5298 CQOI2018 交错序列 【DP+矩阵快速幂优化】*](https://blog.youkuaiyun.com/weixin_30892987/article/details/99470493)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值