题解:AT_abc472_f [ABC472F] Centroid of a Slice
maxiaomeng
·
·
题解
题意
给定一堆点按逆时针方向组成多边形。每次给定两个点 x 和 y,求从 x 到 y 逆时针途经的所有点组成的多边形重心坐标。多边形为均匀薄板。
做法
任意多边形重心
先考虑如何求任意多边形的重心。
先上结论,设 n 个点的坐标用向量表示为 a_{i},且 a_{n+1}=a_1,则:
- 定义叉乘(返回数字)u\times v=x_uy_v-x_vy_u。
- 则多边形面积 S=\dfrac{|\sum_{i=1}^n (a_i\times a_{i+1})|}{2}。
- 则多边形重心坐标 c=\dfrac{\sum_{i=1}^n(a_i+a_{i+1})(a_i\times a_{i+1})}{6S}。
证明(可跳过)
接下来进行证明,因为严谨证明须用二重积分等复杂知识,为方便理解这里仅为粗略证明。
步骤一:叉乘算面积
注意到叉乘 u\times v=\dfrac{1}{2}|u||v|\sin \theta 就是原点、u 和 v 围成的三角形的有向面积。则叉乘算面积公式相当于用原点和每条边组成三角形,将有向面积加起来:绿色为正面积,红色为负面积。这样绕一圈,多边形外的部分都被抵消了,剩下的面积的绝对值正好就是原多边形的面积。
步骤二:重心公式
首先我们有三角形重心公式:c=\dfrac{a_1+a_2+a_3}{3},这可以使用高中的向量知识来证明,这里不再赘述。
接下来发现均匀薄板多边形重心可粗略理解为多边形所有点坐标(向量)的平均值。
可以继续使用叉乘算面积的思路,把多边形剖分为 n 个三角形。每个三角形再用三角形重心公式计算重心。由于三角形的面积可能不一样大,重心对多边形的贡献也因此不一样多,因此多边形的重心应为每个三角形以面积为权,重心坐标(向量)的加权平均值:
## 回到原题
会了这个公式,这道题其实已经做完了。
破环成链,则询问的多边形即为原多边形的一个区间加上起点到终点的边。用前缀和即可。
:::success[code]
```cpp
#include<bits/stdc++.h>
#define int long long
#define float long double
#define endl '\n'
using namespace std;
const int N=1000005;
int n,q;
const float eps=1e-9;
struct point{ // 向量(点)模板,* 为叉乘
float x,y;
point(){
x=y=0;
}
point(float a,float b){
x=a;
y=b;
}
point(const complex<float>&b){
x=b.real();
y=b.imag();
}
operator complex<float>()const{
return complex<float>(x,y);
}
point operator+(const point&b)const{
return point(x+b.x,y+b.y);
}
point operator-(const point&b)const{
return point(x-b.x,y-b.y);
}
point operator-()const{
return point(-x,-y);
}
float operator*(const point&b)const{ // Cross Product
return x*b.y-y*b.x;
}
point operator*(const float&b)const{
return point(x*b,y*b);
}
float operator/(const point&b)const{ // Dot Product
return x*b.x+y*b.y;
}
float len()const{
return hypot(x,y);
}
point&operator+=(const point&b){
return *this=*this+b;
}
point&operator-=(const point&b){
return *this=*this-b;
}
point&operator*=(const float&b){
return *this=*this*b;
}
point operator/(const float&b)const{
return point(x/b,y/b);
}
point&operator/=(const float&b){
return *this=*this/b;
}
bool operator==(const point&b)const{
return x-b.x<=eps&&x-b.x>=-eps&&y-b.y<=eps&&y-b.y>=-eps;
}
bool operator!=(const point&b)const{
return !((*this)==b);
}
operator bool()const{
return ((*this)!=point());
}
}a[N];
float p[N],sp[N],px[N],spx[N],py[N],spy[N],x[N],y[N];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>q;
for(int i=1;i<=n;i++) cin>>a[i].x>>a[i].y,a[n+i]=a[i],x[n+i]=x[i]=a[i].x,y[n+i]=y[i]=a[i].y;
for(int i=1;i<=n+n;i++)p[i]=a[i]*a[i+1],sp[i]=sp[i-1]+p[i]; // 每条边对应的叉乘,及其前缀和
for(int i=1;i<=n+n;i++)px[i]=p[i]*(x[i]+x[i+1]),spx[i]=spx[i-1]+px[i]; // 同理
for(int i=1;i<=n+n;i++)py[i]=p[i]*(y[i]+y[i+1]),spy[i]=spy[i-1]+py[i]; // 同理
while(q--){
int l,r;
cin>>l>>r;
if(l>r)r+=n;
float S=(sp[r-1]-sp[l-1]+(a[r]*a[l]))/2; // 算面积
if(S<0)S=-S;
float cx=(spx[r-1]-spx[l-1]+(x[r]+x[l])*(a[r]*a[l]))/S/6; // 使用重心公式
float cy=(spy[r-1]-spy[l-1]+(y[r]+y[l])*(a[r]*a[l]))/S/6;
cout<<fixed<<setprecision(20)<<cx<<' ';
cout<<fixed<<setprecision(20)<<cy<<'\n';
}
return 0;
}
```
:::