bzoj 2564 集合的面积

时间:2021-08-16 15:12:26

Description

  对于一个平面上点的集合P={(xi,yi )},定义集合P的面积F(P)为点集P的凸包的面积。
  对于两个点集A和B,定义集合的和为:
  A+B={(xiA+xjB,yiA+yjB ):(xiA,yiA )∈A,(xjB,yjB )∈B}
  现在给定一个N个点的集合A和一个M个点的集合B,求2F(A+B)。
 

Input

 第一行包含用空格隔开的两个整数,分别为N和M;
  第二行包含N个不同的数对,表示A集合中的N个点的坐标;
  第三行包含M个不同的数对,表示B集合中的M个点的坐标。

Output

 一共输出一行一个整数,2F(A+B)。

Sample Input

4 5
0 0 2 1 0 1 2 0
0 0 1 0 0 2 1 2 0 1

Sample Output

18
数据规模和约定
  对于30%的数据满足N ≤ 200,M ≤ 200;
  对于100%的数据满足N ≤ 10^5,M ≤ 10^5,|xi|, |yi| ≤ 10^8。

分别求出两个点集的凸包,然后贪心地加点就行。

A和B凸包第一个点肯定在答案里

然后贪心,如果A到了i,B到了j

显然如果A[i+1]+B[j]比A[i]+B[j+1]更凸,也就是在右边,那么就i+1,否则j+1

这样构造出的新凸包即为答案

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long lol;
struct point
{
lol x,y;
}p[],s[][],sta[];
int n,m,C,top;
lol ans;
lol cross(point a,point b)
{
return a.x*b.y-a.y*b.x;
}
point operator -(point a,point b)
{
return (point){a.x-b.x,a.y-b.y};
}
point operator +(point a,point b)
{
return (point){a.x+b.x,a.y+b.y};
}
bool cmp(point a,point b)
{
return (a.y<b.y)||(a.y==b.y&&a.x<b.x);
}
lol dist(point a)
{
return a.x*a.x+a.y*a.y;
}
bool cmp2(point a,point b)
{
lol t=cross((p[]-a),(p[]-b));
if (t==) return dist(p[]-a)<dist(p[]-b);
return t>;
}
int graham(int N,int c)
{int i;
int C=c;
sort(p+,p+N+,cmp);
sort(p+,p+N+,cmp2);
top=;
s[c][++top]=p[];s[c][++top]=p[];
for (i=;i<=N;i++)
{
while (top>&&cross(p[i]-s[c][top-],s[c][top]-s[c][top-])>=) top--;
++top;
s[c][top]=p[i];
}
return top;
}
int main()
{int i,j;
cin>>n>>m;
for (i=;i<=n;i++)
{
scanf("%lld%lld",&p[i].x,&p[i].y);
}
n=graham(n,);
for (i=;i<=m;i++)
{
scanf("%lld%lld",&p[i].x,&p[i].y);
}
m=graham(m,);
sta[top=]=s[][]+s[][];
for (i=,j=;i<=n||j<=m;)
{
point x=s[][(i-)%n+]+s[][j%m+],y=s[][i%n+]+s[][(j-)%m+];
if (cross(x-sta[top],y-sta[top])>=)
sta[++top]=x,j++;
else sta[++top]=y,i++;
}
for (i=;i<top;i++)
ans+=cross(sta[i]-sta[],sta[i+]-sta[]);
printf("%lld",ans);
}