Links: http://vjudge.net/problem/36014
Analysis: First enumerate A and B, put all a+b records in an ordered array, and then enumerate C and D, check-c-d how many methods are written in the form of a+b (binary search).
1#include <cstdio>2#include <algorithm>3 using namespacestd;4 5 Const intMAXN =4000+5;6 7 intA[MAXN], B[MAXN], C[MAXN], D[MAXN];8 intAB[MAXN * MAXN], CD[MAXN *MAXN];9 Ten intMain () { One intT; Ascanf"%d", &T); - while(t--) { - intN; thescanf"%d", &n); - for(inti =0; I < n; i++) scanf ("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]); - for(inti =0; I < n; i++) - for(intj =0; J < N; J + +) Ab[i * n + j] = A[i] +B[j]; + for(inti =0; I < n; i++) - for(intj =0; J < N; J + +) Cd[i * n + j] =-(C[i] +d[j]); +Sort (CD, CD + n *n); A Long LongAns =0; at for(inti =0; I < n * N; i++) -Ans + = Upper_bound (CD, CD + N * N, Ab[i])-Lower_bound (CD, CD + n *N, Ab[i]); -printf"%lld\n", ans); - if(T) printf ("\ n"); - } - return 0; in}
UVa1152 4 Values whose Sum is 0 (Midway encounter method)