C11LOCK - Ổ khóa đặc biệt

Tác giả: happyboy99x

Ngôn ngữ: C++

#include<cstdio>
#include<climits>
#include<algorithm>
using namespace std;

const int N = 500+5;
int a[5][N], n, t[N*N], nx, ny;
pair<int, int> x[N*N], y[N*N];
long long k;

void make(const int &r1, const int &r2, pair<int, int> * x, int &c) {
	int n2 = 0;
	for(int i = 0; i < n; ++i)
		for(int j = 0; j < n; ++j)
			t[n2++] = a[r1][i] + a[r2][j];
	sort(t, t+n2); t[n2] = INT_MAX; c = 0;
	for(int s = 0, i = 0; i <= n2; ++i) if(t[i] != t[s])
		x[c++] = pair<int, int>(t[s], s-i), s = i;
}

int main() {
	scanf("%d%lld", &n, &k); for(int i = 0; i < 5; ++i) for(int j = 0; j < n; ++j) scanf("%d", &a[i][j]);
	make(0, 1, x, nx); make(2, 3, y, ny);
	long long res = 0;
	for(int i = 0; i < n; ++i) {
		int s = k - a[4][i];
		for(int p = 0, q = ny - 1; p < nx && q >= 0; ) {
			if(x[p].first + y[q].first == s) res += (long long) x[p++].second * y[q--].second;
			else if(x[p].first + y[q].first < s) ++p;
			else --q;
		}
	}
	printf("%lld\n", res);
	return 0;
}

Download