用map效率有点低啊,tle了。
#include <set>
#include <map>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <string>
#include <vector>
#include <cctype>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define SF(a) scanf("%d", &a)
#define PF(a) printf("%d\n", a)
#define SFF(a, b) scanf("%d%d", &a, &b)
#define SFFF(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define LL long long
#define maxn 4005
#define maxm 205
#define mod 1000000007
#define INF 10000007
#define eps 1e-4
using namespace std;
//-------------------------CHC------------------------------//
int a[maxn], b[maxn], c[maxn], d[maxn];
int sum[maxn*maxn];
int main() {
int t;
SF(t);
while (t--) {
int n;
SF(n);
FOR(i, 0, n) SFFF(a[i], b[i], c[i]), SF(d[i]);
int ns = 0;
FOR(i, 0, n) FOR(j, 0, n) sum[ns++] = a[i] + b[j];
LL ans = 0;
sort(sum, sum + ns);
FOR(i, 0, n) FOR(j, 0, n)
ans += upper_bound(sum, sum + ns, -c[i] - d[j]) - lower_bound(sum, sum + ns, -c[i] - d[j]);
printf("%lld\n", ans);
if (t) puts("");
}
return 0;
}
hash实现 1.01s
#include <set>
#include <map>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <deque>
#include <cstdio>
#include <string>
#include <vector>
#include <cctype>
#include <sstream>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define SF(a) scanf("%d", &a)
#define PF(a) printf("%d\n", a)
#define SFF(a, b) scanf("%d%d", &a, &b)
#define SFFF(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define IN() freopen("in.txt", "r", stdin)
#define OUT() freopen("out.txt", "w", stdout)
#define FOR(i, a, b) for(int i = a; i < b; ++i)
#define LL long long
#define maxn 4005
#define maxm 205
#define mod 1000000007
#define INF 10000007
#define eps 1e-4
using namespace std;
//-------------------------CHC------------------------------//
struct hash_map {
static const int mask = 0x7ffffff;
int q[mask + 1], p[mask + 1];
void clear() { CLEAR(q, 0); }
int& operator[](int k) {
int i;
for (i = k & mask; q[i] && p[i] != k; i = (i + 1) & mask);
p[i] = k; return q[i];
}
}Hash;
int a[maxn], b[maxn], c[maxn], d[maxn];
int main() {
int t;
SF(t);
while (t--) {
Hash.clear();
int n;
SF(n);
FOR(i, 0, n) SFFF(a[i], b[i], c[i]), SF(d[i]);
FOR(i, 0, n) FOR(j, 0, n) ++Hash[a[i] + b[j]];
LL ans = 0;
FOR(i, 0, n) FOR(j, 0, n) ans += Hash[-c[i] - d[j]];
printf("%lld\n", ans);
if (t) puts("");
}
return 0;
}