HDU 6022---MG loves set(K-D树)

时间:2022-03-17 12:52:28

题目链接

Problem Description
MG is an intelligent boy. One day he was challenged by the famous master called Douer: 
if the sum of the square of every element in a set is less than or equal to the square of the sum of all the elements, then we regard this set as ”A Harmony Set”. 
Now we give a set with n different elements, ask you how many nonempty subset is “A Harmony Set”.
MG thought it very easy and he had himself disdained to take the job. As a bystander, could you please help settle the problem and calculate the answer?
Input
The first line is an integer T which indicates the case number.(1<=T<=10)
And as for each case, there are 1 integer n in the first line which indicate the size of the set(n<=30).
Then there are n integers V in the next line, the x-th integer means the x-th element of the set(0<=|V|<=100000000).
Output
As for each case, you need to output a single line.
There should be one integer in the line which represents the number of “Harmony Set”. 
Sample Input
3
4
1 2 3 4
5
1 -1 2 3 -3
6
0 2 4 -4 -5 8
Sample Output
15
12
25
题意:
HDU  6022---MG loves set(K-D树)

思路:

看到题目数据的范围n<=30 ,枚举考虑每一个子集肯定会超时,所以这个办法不行了。怎么样降低复杂度呢?

先化简一下,设子集为{x,y,z}满足题目要求,则x*x+y*y+z*z<=(x+y+z)*(x+y+z) 即xy+yz+xz>=0  ,所以本题就是求一个集合有多少非空子集满足集合中元素两两乘积的和大于等于0;

巧妙地思想:把集合分成前后两半,设前一半集合的任意一个子集的和为Xi 两两之间乘积的和为Yi ,后一半集合的任意一个子集的和为Xj 两两之间乘积的和为Yj 可以发现(a+b)*(c+d)+ab+cd>=0是子集{a,b,c,d}满足题意的要求,那么Xi*Xj+Yi+Yj>=0就是判断当前由这两个子集合起来得到的子集是否满足题意的要求。最后用K-D树优化即可。

官方题解如下:

HDU  6022---MG loves set(K-D树)

代码如下:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long LL;
const int N=;
LL a[];
int flag[*N];
int idx; struct Node
{
LL f[];
LL mx[];
LL mn[];
int size;
bool operator<(const Node& s)const
{ return f[idx]<s.f[idx]; }
}A[N],B[N],tr[*N]; void update(int i)
{
int s=;
if(flag[i<<])
{
for(int k=;k<=;k++)
{
if(tr[i<<].mn[k]<tr[i].mn[k]) tr[i].mn[k]=tr[i<<].mn[k];
if(tr[i<<].mx[k]>tr[i].mx[k]) tr[i].mx[k]=tr[i<<].mx[k];
}
s+=tr[i<<].size;
}
if(flag[i<<|])
{
for(int k=;k<=;k++)
{
if(tr[i<<|].mn[k]<tr[i].mn[k]) tr[i].mn[k]=tr[i<<|].mn[k];
if(tr[i<<|].mx[k]>tr[i].mx[k]) tr[i].mx[k]=tr[i<<|].mx[k];
}
s+=tr[i<<|].size;
}
tr[i].size=s;
} void build(int l,int r,int i,int deep)
{
if(l>r) return;
int mid=(l+r)>>;
idx=deep;
flag[i]=;
flag[i<<]=; flag[i<<|]=; nth_element(B+l,B+mid,B+r+);
for(int k=;k<=;k++)
tr[i].mx[k]=tr[i].mn[k]=tr[i].f[k]=B[mid].f[k]; build(l,mid-,i<<,-deep);
build(mid+,r,i<<|,-deep);
update(i);
}
int check(LL x1,LL y1,LL x2,LL y2)
{
if(x1*x2+y1+y2>=) return ;
return ;
}
int count(Node p,int i)
{
int re=;
re+=check(tr[i].mn[],tr[i].mn[],p.f[],p.f[]);
re+=check(tr[i].mn[],tr[i].mx[],p.f[],p.f[]);
re+=check(tr[i].mx[],tr[i].mn[],p.f[],p.f[]);
re+=check(tr[i].mx[],tr[i].mx[],p.f[],p.f[]);
return re;
}
int query(Node p,int i)
{
if(!flag[i]) return ;
if(count(p,i)==) return tr[i].size;
int re=check(p.f[],p.f[],tr[i].f[],tr[i].f[]);
if(count(p,i<<)) re+=query(p,i<<);
if(count(p,i<<|)) re+=query(p,i<<|);
return re;
} int main()
{
int T;
cin>>T;
while(T--)
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%lld",&a[i]);
int mid=n/;
int tt=(<<mid);
int tot1=,tot2=;
for(int i=;i<=tt-;i++)
{
A[tot1].f[]=;
A[tot1].f[]=;
for(int k=;(<<(k-))<tt;k++)
{
if((<<(k-))&i)
{
A[tot1].f[]+=A[tot1].f[]*a[k];
A[tot1].f[]+=a[k];
}
}
tot1++;
}
int tt2=<<n;
for(int i=;i<tt2;i+=tt)
{
B[tot2].f[]=;
B[tot2].f[]=;
for(int k=mid+;(<<(k-))<tt2;k++)
{
if((<<(k-))&i)
{
B[tot2].f[]+=B[tot2].f[]*a[k];
B[tot2].f[]+=a[k];
}
}
tot2++;
}
/*for(int i=0;i<tot1;i++)
cout<<A[i].f[0]<<" "<<A[i].f[1]<<endl;
cout<<"-----------------"<<endl;
for(int i=0;i<tot2;i++)
cout<<B[i].f[0]<<" "<<B[i].f[1]<<endl;*/
build(,tot2-,,);
int ans=-;
for(int i=;i<tot1;i++)
{
ans+=query(A[i],);
}
printf("%d\n",ans);
}
return ;
}