我吃柠檬的闵可夫斯基和!!!
tm的,交了42发,写了两天,终于咽下这口矢了[USACO22JAN] Multiple Choice Test P
这东西猎奇到完全有必要写一篇笔记,首先就是求凸包,要是不想死就用y最小,且x对应这个y也最小的点作为开头,不然赤石去吧!(要是Andrew 算法当我没说)
然后就是合并,边全插一个堆里,再极角排序,听着很对对吧,对牛魔!全几把WA了,滚木知道为什么!
然后就是归并,暴力归并T飞(显然),还要搞个启发式合并,牛魔的.
upd 搞完这篇文章1min后:
①:我知道为什么了!因为这个写法不选择最左下的点就全假了,我当时没写…
不写了直接上代码
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<random>
#include<ctime>
#include<queue>
#include<set>
#define MN 400200
#define ll long long
using namespace std;
#define lf double
#define eps 1e-7
#include<iostream>
#include<cmath>
#define Lf long double
constexpr Lf veryRightPi = 3.141592653589793238462643383279L;
template<typename _Tp>
class Vector2{
public:
_Tp X,Y;
Vector2(){}
Vector2(_Tp _X,_Tp _Y){X=_X;Y=_Y;}
inline const Vector2<_Tp> operator - ()const{return {-X,-Y};}
inline const Vector2<_Tp> operator ~ ()const{return {X,-Y};}
inline const Vector2<_Tp> operator ! ()const{return {-X,Y};}
inline const _Tp operator / (const Vector2<_Tp>&_V)const{return Y*_V.X-X*_V.Y;}//fuck cpp, I can't find a ope that fit EP
inline const Vector2<_Tp> operator + (const Vector2<_Tp>&_V)const{return {X+_V.X,Y+_V.Y};}
inline Vector2<_Tp>&operator +=(const Vector2<_Tp>&_V){return (*this)={X+_V.X,Y+_V.Y};}
inline const Vector2<_Tp> operator * (const Vector2<_Tp>&_V)const{return {X*_V.X-Y*_V.Y,X*_V.Y+Y*_V.X};}
inline const Vector2<_Tp> operator - (const Vector2<_Tp>&_V)const{return {X-_V.X,Y-_V.Y};}
inline friend const bool operator == (const Vector2<_Tp>&_V1,const Vector2<_Tp>&_V2){
return (_V1.X==_V2.X)&&(_V1.Y==_V2.Y);
}
inline friend const bool operator != (const Vector2<_Tp>&_V1,const Vector2<_Tp>&_V2){
return (_V1.X!=_V2.X)||(_V1.Y!=_V2.Y);
}
inline Lf Angle(const Vector2<_Tp>&_C = {(_Tp)0,(_Tp)0})const{//don't use it too much or you will TLE
Lf res = atan2((Lf)(Y-_C.Y),(Lf)(X-_C.X));//std::atan2 and atan2 both right,why?
return res>=0.0?res:res+veryRightPi*2;
}
inline unsigned short Loc()const{
if(X>(_Tp)0&&Y>=(_Tp)0)return 0;
else if(X<=(_Tp)0&&Y>(_Tp)0)return 1;
else if(X<(_Tp)0&&Y<=(_Tp)0)return 2;
return 3;
}
inline _Tp Dis2(){return X*X+Y*Y;}
inline Lf Dis()const{return sqrtl((Lf)(X*X+Y*Y));}//only sqrtl not std::sqrtl,why?
friend std::istream & operator >>(std::istream&_I,Vector2<_Tp>&_V){
_I>>_V.X>>_V.Y;
return _I;
}
friend std::ostream & operator <<(std::ostream&_O,const Vector2<_Tp>&_V){
_O<<'('<<_V.X<<','<<_V.Y<<')';
return _O;
}
inline friend bool cmpAngle(const Vector2<_Tp>&_V1,const Vector2<_Tp>&_V2){
if(_V1.Loc()!=_V2.Loc())return _V1.Loc()<_V2.Loc();
return _V1/_V2<0;
}
inline friend bool sameDir(const Vector2<_Tp>&_V1,const Vector2<_Tp>&_V2){
if(_V1.Loc()!=_V2.Loc())return 0;
return _V1.X*_V2.Y==_V1.Y*_V2.X;
}
};
// #undef Lf
#define ve2 Vector2<long long>
ve2 sr[MN],st[MN];
int ct,n;
struct tb{
int siz;ve2*vs;
friend bool operator <(const tb&a,const tb&b){
return a.siz>b.siz;
}
};
priority_queue<tb> pq;
tb merge(tb _A,tb _B){
if(!_A.siz)return _B;
if(!_B.siz)return _A;
int rlen = 0;
int sa = _A.siz,sb = _B.siz;
ve2*a = _A.vs,*b = _B.vs;
ve2*res;
int l = 0,r = 0;
ve2 h = a[0]+b[0];
ve2 e = a[0]-a[sa-1];
for(int i = 0;i<sa-1;i++){
a[i]=a[i+1]-a[i];
}
a[sa-1]=e;
e = b[0]-b[sb-1];
for(int i = 0;i<sb-1;i++){
b[i]=b[i+1]-b[i];
}
b[sb-1]=e;
sr[0]=h;rlen=1;
while(l<sa&&r<sb){
// if(sameDir(a[l],b[r]))sr[rlen++]=(h+=a[l++]+b[r++]);
// else
if(cmpAngle(a[l],b[r]))sr[rlen++]=(h+=a[l++]);
else sr[rlen++]=(h+=b[r++]);
}
while(l<sa)sr[rlen++]=(h+=a[l++]);
while(r<sb)sr[rlen++]=(h+=b[r++]);
res=new ve2[rlen-1];
for(int i = 0;i<rlen-1;i++)res[i]=sr[i];
delete a;delete b;
return {rlen-1,res};
}
signed main(){
// freopen("data.in","r",stdin);freopen("data.out","w",stdout);
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n;
int m;
ll ans = 0;
tb E={0,nullptr};
for(int i = 1;i<=n;i++){
cin>>m;
int mi = 0;
for(int i = 0;i<m;i++){
cin>>sr[i];
ans=max(ans,sr[i].Dis2());
if(sr[i].Y<sr[mi].Y)mi=i;
if(sr[i].Y==sr[mi].Y)
if(sr[i].X<sr[mi].X)mi=i;
}
swap(sr[0],sr[mi]);
ve2 cb = sr[0];
sort(sr+1,sr+m,[cb](const ve2&A,const ve2&B){
if(sameDir(A-cb,B-cb)){
return (A.X==B.X)?(A.Y<B.Y):(A.X<B.X);
}
return cmpAngle(A-cb,B-cb);
});
st[ct=1]=cb;
for(int i = 1;i<m;i++){
while(ct>1){
if((sr[i]-st[ct-1])/(st[ct]-st[ct-1])<=0)ct--;
else break;
}
st[++ct]=sr[i];
}
if(ct>3){
if((st[1]-st[ct-1])/(st[ct]-st[ct-1])<=0)ct--;
}
ve2*_s = new ve2[ct];
for(int i = 1;i<=ct;i++)_s[i-1]=st[i];
pq.push({ct,_s});
// E=merge(E,{ct,_s});
}
for(int i = 1;i<n;i++){
tb A = pq.top();pq.pop();
tb B = pq.top();pq.pop();
tb C = merge(A,B);
pq.push(C);
}
E = pq.top();
for(int i = 0;i<E.siz;i++){
ans = max(ans,E.vs[i].Dis2());
}
//
// for(int i = 1;i<=cs;i++)ans = max(ans,sve[i].Dis2());
cout<<ans;
return 0;
}