UVALive 3890

#define eps 1e-7
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
struct point{
    double x,y;
    int id;
    void init(double xx,double yy,int i){
        x=xx;y=yy;id=i;
    }
    bool operator<(point a){
        return x<a.x||fabs(x-a.x)<eps&&y<a.y;
    }
}p[400010],s[400010];
double cp(point a,point b,point c){
    return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
double dis(point u,point v){
    return (u.x-v.x)*(u.x-v.x)+(u.y-v.y)*(u.y-v.y);
}
void solve(int n,int& u,int& v){
    if(n==1){u=v=0;return ;}
    if(n==2){u=0,v=1;return ;}
    p[n]=p[0];
    int q=1;
    double as=0;
    for(int i=0;i<n;i++){
        while(fabs(cp(p[i],p[i+1],p[q]))<fabs(cp(p[i],p[i+1],p[(q+1)%n])))
            q=(q+1)%n;
            if(dis(p[i],p[q])>as){
                as=dis(p[i],p[q]);
                u=i;v=q;
            }
    }
}
int cmp(point a,point b){
    double l=cp(p[0],a,b);
    if(fabs(l)>eps)return l>eps;
    return dis(p[0],a)<dis(p[0],b);
}
int graham(int n){
    int i,tmp,top;
    tmp=0;top=1;
    for(i=1;i<n;i++)
        if(p[i].x<p[tmp].x||fabs(p[i].x-p[tmp].x)<eps&&p[i].y<p[tmp].y)
            tmp=i;
    swap(p[0],p[tmp]);
    sort(p+1,p+n,cmp);
    s[0]=p[0];
    s[1]=p[1];
    for(i=2;i<n;i++){
        while(cp(s[top-1],s[top],p[i])<eps&&top)
            top--;
        s[++top]=p[i];
    }
    for(i=0;i<=top;i++)
        p[i]=s[i];
    return ++top;
}
int main(){
    int cs,i,n,u,v;
    double x,y,w;
    scanf("%d",&cs);
    while(cs--){
        scanf("%d",&n);
        n*=4;
        for(i=0;i<n;i+=4){
            scanf("%lf%lf%lf",&x,&y,&w);
            p[i].init(x,y,i);
            p[i+1].init(x,y+w,i);
            p[i+2].init(x+w,y,i);
            p[i+3].init(x+w,y+w,i);
        }
        n=graham(n);
        solve(n,u,v);
        printf("%.0lf\n",dis(p[u],p[v]));
    }
    return 0;
}

你可能感兴趣的:(UVALive 3890)