Subject address: Ultraviolet A 1152
First, enumerate the sum of set a and Set B, and then enumerate the sum of set C and set D to see how many values are opposite to each other. Question. You can use binary to find the stored value.
The Code is as follows:
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;int a[5000], b[5000], c[6000], d[6000], p[17000000];int main(){ int t, n, m, ans, i, j, k; scanf("%d",&t); while(t--) { scanf("%d",&n); m=0; for(i=0;i<n;i++) { scanf("%d%d%d%d",&a[i],&b[i],&c[i],&d[i]); } for(i=0;i<n;i++) { for(j=0;j<n;j++) { p[m++]=a[i]+b[j]; } } sort(p,p+m); int low, high, mid; ans=0; for(i=0;i<n;i++) { for(j=0;j<n;j++) { int x=-c[i]-d[j]; low=0, high=m-1; while(low<=high) { mid=low+high>>1; if(p[mid]>x) high=mid-1; else if(p[mid]<x) low=mid+1; else { for(k=mid;k>=0;k--) { if(p[k]==x) { ans++; } else break; } for(k=mid+1;k<m;k++) { if(p[k]==x) { ans++; } else break; } break; } } } } printf("%d\n",ans); if(t) puts(""); } return 0;}
(White Book Training Program) fig 1152 4 values whose sum is 0 (midway encounter ..)