中途相遇法(二分法)
这种做法比较简单
这种方法主要注意这一行(这个技巧还是很常用的)
(哈希方法我还没看懂,看懂再补上)
cnt+=(long long)(upper_bound(pq.begin(),pq.end(),temp)-lower_bound(pq.begin(),pq.end(),temp));
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int ccase;
cin>>ccase;
while(ccase--)
{
long long int cnt=0;
int n;
int a[4010],b[4010],c[4010],d[4010];
cin>>n;
for(int i=0;i<n;i++)
{
scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]);
}
vector<int>pq;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
pq.push_back(a[i]+b[j]);
}
}
sort(pq.begin(),pq.end());
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
int temp=-c[i]-d[j];
cnt+=(long long)(upper_bound(pq.begin(),pq.end(),temp)-lower_bound(pq.begin(),pq.end(),temp));
}
}
cout<<cnt<<endl;
if(ccase)
cout<<endl;
}
return 0;
}