HDU6127 Hard challenge[计算几何]

Hard challenge

HDU - 6127

#pragma comment(linker, "/STACK:102400000,102400000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef long long ll;
const int N=5e4+5;
ll sum[5][N];
vector<int>vc[5];
int nx[]={0,4,1,2,3};
int fan[]={0,3,4,1,2};
struct point
{
int x,y,val;
point (int xx=0,int yy=0,int v=0):x(xx),y(yy),val(v){}
}p[N];
ll cross(const point &p1,const point &p2,const point &q1,const point &q2)
{
return (ll)(q2.y-q1.y)*(p2.x-p1.x)-(ll)(q2.x-q1.x)*(p2.y-p1.y);
}
bool cmp(const point &a,const point &b)
{
if (!a.y && !b.y && (ll)a.x*b.x<=0)
return a.x>b.x;
if (!a.y && a.x>=0 && b.y)
return 1;
if (!b.y && b.x>=0 && a.y)
return 0;
if ((ll)b.y*a.y<=0)
return a.y>b.y;
point one;
one.y=one.x=0;
return cross(one,a,one,b)>0 || (!cross(one,a,one,b) && a.x<b.x);
}
bool check(const point &a,const point &b)
{
point np(-a.x,-a.y);
return !cmp(np,b);
}
void push(int id,int i,int val)
{
vc[id].push_back(i);
sum[id][vc[id].size()]=sum[id][vc[id].size()-1]+val;
}
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
int n;
scanf("%d",&n);
for (int i=0 ; i<n ; ++i)
scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].val);
sort(p,p+n,cmp);
for (int i=0 ; i<=4 ; ++i)
{
vc[i].clear();
for (int j=0 ; j<=n ; ++j)
sum[i][j]=0;
}
ll s=0;
for (int i=0 ; i<n ; ++i)
{
if (p[i].x>=0 && p[i].y>=0)
push(1,i,p[i].val);
else if (p[i].x<0 && p[i].y>=0)
push(2,i,p[i].val);
else if (p[i].x<0 && p[i].y<0)
push(3,i,p[i].val);
else
push(4,i,p[i].val);
s+=p[i].val;
}
ll mx=0;
for (int i=1 ; i<=4 ; ++i)
{
if (!vc[i].size())
continue;
for (int j=0,k=0 ; j<vc[i].size() ; ++j)
{
ll tmp=sum[nx[i]][vc[nx[i]].size()]+sum[i][j+1];
while (k<vc[fan[i]].size() && check(p[vc[i][j]],p[vc[fan[i]][k]]))
++k;
tmp+=sum[fan[i]][vc[fan[i]].size()]-sum[fan[i]][k];
mx=max(tmp*(s-tmp),mx);
}
}
printf("%lld\n",mx);
}
return 0;
}
/*
10
4
-5 2 100
-2 -3 100
100000000 1000000000 10
100000000 999999999 9
2
1 1 1
1 -1 1
3
1 1 1
1 -1 10
-1 0 100
4
2 1 10
-4 -1 20
-3 -4 30
-2 -5 40
3
-1 2 10
3 -1 20
1 -5 30
*/