双向搜索,分组计算ab,与cd,然后二分查找即可。
lower_bound - upper_bound == the number of the items that equal to x.
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int a[4005], b[4005], c[4005], d[4005];
int cd[4005*4005];
int main() {
int n;
while (scanf("%d", &n) != EOF) {
for (int i = 0; i < n; i++) {
scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cd[i * n + j] = c[i] + d[j];
}
}
sort(cd, cd + n * n);
long long res = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
int x = -(a[i] + b[j]);
res += upper_bound(cd, cd + n * n, x) - lower_bound(cd, cd + n * n, x);
}
}
printf("%lld\n", res);
}
}