思路:这道题求的是a+b+c+d=0的数目,就是a+b=-(c+d)
预处理c+d,定义数组f[(i-1)*n+j]=c[i]+d[j]; 再将f排序
而lower_bound(f,f+n*n,k)可以返回数列中第一个大于等于k的项的地址,upper_bound(f,f+n*n,k)可以返回第一个大于k的数的地址。
于是代码便简单多了。
#include<bits/stdc++.h>
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define ll long long
#define ld long double
#define mem(ar,num) memset(ar,num,sizeof(ar))
#define me(ar) memset(ar,0,sizeof(ar))
#define lowbit(x) (x&(-x))
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define lcm(a,b) ((a)*(b)/(__gcd((a),(b))))
#define Max 4010
#define mod 1000000007
using namespace std;
int n;
ll a[Max], b[Max], c[Max], d[Max], ab[4010][4010], cd[16000010], ans;
int main() {
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
}
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++)
cd[(i - 1)*n + j] = c[i] + d[j];
sort(cd + 1, cd + n * n + 1);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= n; j++) {
ll k = -(a[i] + b[j]);
ans += upper_bound(cd + 1, cd + n * n + 1, k) - lower_bound(cd + 1, cd + n * n + 1, k);
}
cout << ans;
return 0;
}