POJ 2785 4 Values whose Sum is 0
折半搜索,\(O(n)\)型枚举,二分查找相反数个数,时间复杂度(\(O(n^2logn)\))
const int N=4010;
int a[N],b[N],c[N],d[N];
int ab[N*N];
int n;
int main()
{
cin>>n;
for(int i=0;i<n;i++) cin>>a[i]>>b[i]>>c[i]>>d[i];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
ab[i*n+j]=a[i]+b[j];
sort(ab,ab+n*n);
int ans=0;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
int cd=c[i]+d[j];
ans+=upper_bound(ab,ab+n*n,-cd)-lower_bound(ab,ab+n*n,-cd);
}
cout<<ans<<endl;
//system("pause");
}