Summary: Meet halfway. Compare map, quick row + binary search, hash efficiency.
n is the level of 4000, the direct O (n^4) is definitely super, so the Midway encounter method, O (n^2) time enumeration where two and, O (n^2) time enumerates the other two and the opposite number, and then O (LOGN) The time the query exists.
First try the map, decisive tle
//TLE#include <cstdio>#include<algorithm>#include<map>using namespacestd;Const intMAXN =4001;intdata[4][maxn];map<int,int>CNT;intMain () {intR scanf"%d",&T); int*a = data[0], *b = data[1], *c = data[2],*d = data[3]; Map<int,int>:: Iterator it; while(t--){ intN scanf"%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 + +) {Cnt[a[i]+b[j]]++; } intAns =0; for(inti =0; I < n; i++) for(intj =0; J < N; J + +){ intTMP =-c[i]-D[j]; It=Cnt.find (TMP); if(It!=cnt.end ()) ans + = it->second; } printf ("%d\n", ans); if(T) Putchar ('\ n'); } return 0;}
Map,tle
And then changed to a fast line + binary search, 4920ms
//Runtime 4920ms#include <cstdio>#include<algorithm>using namespacestd;Const intMAXN =4001;intdata[4][MAXN];intvec[maxn*MAXN];int_lower_bound (int*a,intLintRintv) { intm; while(l<R) {m= (l+r) >>1; if(A[M]>=V) R =m; ElseL = m+1; } returnL;}int_upper_bound (int*a,intLintRintv) { intm; while(l<R) {m= (l+r) >>1; if(A[M]>V) R =m; ElseL = m+1; } returnL;}intMain () {intT scanf"%d",&T); int*a = data[0], *b = data[1], *c = data[2],*d = data[3]; while(t--){ intN scanf"%d",&N); for(inti =0; I < n; i++) {scanf ("%d%d%d%d", a+i,b+i,c+i,d+i); } intSZ =0; for(inti =0; I < n; i++) for(intj =0; J < N; J + +) {Vec[sz+ +] = a[i]+B[j]; } sort (Vec,vec+sz); intAns =0; for(inti =0; I < n; i++) for(intj =0; J < N; J + +){ intTMP =-(c[i]+D[j]); Ans+ = _upper_bound (VEC,0, sz,tmp)-_lower_bound (VEC,0, sz,tmp); } printf ("%d\n", ans); if(T) Putchar ('\ n'); } return 0;}
Quick Shoot + two points, 4920ms
A little better approach to fast-row + count, 2832ms
#include <cstdio>#include<algorithm>#include<map>using namespaceStd;typedef pair<int,int>PII;#defineFi first#defineSe SecondConst intMAXN =4001;intdata[4][MAXN];p II CNT1[MAXN*MAXN];p II CNT2[MAXN*MAXN];intvec[maxn*MAXN];intMain () {intR scanf"%d",&T); int*a = data[0], *b = data[1], *c = data[2],*d = data[3]; while(t--){ intN scanf"%d",&N); for(inti =0; I < n; i++) {scanf ("%d%d%d%d", a+i,b+i,c+i,d+i); } intSZ =0; for(inti =0; I < n; i++) for(intj =0; J < N; J + +) {Vec[sz+ +] = a[i]+B[j]; } sort (Vec,vec+sz); intLen1 =0; CNT1[LEN1]= PII (Vec[len1],1); for(inti =1; I < sz; i++){ if(Vec[i] = = cnt1[len1].fi) cnt1[len1].se++; Else{cnt1[++len1].fi = Vec[i]; cnt1[len1].se =1; } } SZ=0; for(inti =0; I < n; i++) for(intj =0; J < N; J + +) {Vec[sz+ +] =-c[i]-E[j]; } sort (Vec,vec+sz); intLen2 =0; CNT2[LEN2]= PII (Vec[len2],1); for(inti =1; I < sz; i++){ if(Vec[i] = = cnt2[len2].fi) cnt2[len2].se++; Else{cnt2[++len2].fi = Vec[i]; cnt2[len2].se =1; } } intp =0, q =0, ans =0; while(p<=len1&&q<=len2) { if(cnt1[p].fi = =cnt2[q].fi) {ans+ = cnt1[p++].se*cnt2[q++].se; }Else if(cnt1[p].fi>cnt2[q].fi) q++; Elsep++; } printf ("%d\n", ans); if(T) Putchar ('\ n'); } return 0;}
There is a hash table, write hung, to be mended ...
UVA 1152 4 values whose Sum is Zero and 4 value of 0