Submission #263212

# Submission time Handle Problem Language Result Execution time Memory
263212 2020-08-13T14:07:54 Z sjimed Pairs (IOI07_pairs) C++14
100 / 100
895 ms 65400 KB
#include<bits/stdc++.h>
using namespace std;

#define fast ios::sync_with_stdio(false); cin.tie(0);
#define pre(a) cout << fixed; cout.precision(a);
#define fi first
#define se second
#define em emplace
#define eb emplace_back
#define all(v) (v).begin() (v).end()
#define mp make_pair
#define mt make_tuple

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll INF = 1e18;
const int inf = 1e9;

struct point {
	int x[3];
	point() {
		x[0] = x[1] = x[2] = 0;
	}

	bool operator<(point &p) {
		return mt(x[0], x[1], x[2]) < mt(p.x[0], p.x[1], p.x[2]);
	}
};

ll b, n, d, m;
point p[100010];
ll tree[150010];
vector<int> v[1500010];
vector<pii> st[150010], ed[150010];
int cnt[80][80][80] = {};
int dp[3][80][80][80] = {};
int dp2[3][80][80][80] = {};
int dp3[3][80][80][80] = {};
int tot[80][80][80] = {};

void update(int i) {
	while(i <= 150010) {
		tree[i]++;

		i += i & -i;
	}
}

ll cal(int i) {
	ll ret = 0;
	while(i) {
		ret += tree[i];
		i -= i & -i;
	}

	return ret;
}

int dist(int i, int j) {
	int ret = 0;
	for(int k=0; k<3; k++) {
		ret += abs(p[i].x[k] - p[j].x[k]);
	}

	return ret;
}

int main() {
	fast;

	cin >> b >> n >> d >> m;

	for(int i=1; i<=n; i++) {
		for(int j=0; j<b; j++) {
			cin >> p[i].x[j];
		}
	}

	if(b == 1) {
		sort(p+1, p+n+1);

		ll ans = 0;
		int idx = 1;
		for(int i=1; i<=n; i++) {
			while(idx <= n && dist(idx, i) <= d) idx++;

			ans += idx - i - 1;
		}

		cout << ans;
		return 0;
	}
	if(b == 2) {
		for(int i=1; i<=n; i++) {
			int x = p[i].x[0];
			int y = p[i].x[1];

			v[x+y].eb(x - y);

			st[max(2LL, x + y - d)].eb(max(1-m, x-y-d), min(m-1, x-y+d));
			ed[min(2*m, x + y + d)].eb(max(1-m, x-y-d), min(m-1, x-y+d));
		}

		ll ans = 0;
		for(int i=2; i<=2*m; i++) {
			for(auto j : st[i]) {
				ans -= cal(m+j.se) - cal(m+j.fi-1);
			}

			for(auto j : v[i]) {
				update(m+j);
			}

			for(auto j : ed[i]) {
				ans += cal(m+j.se) - cal(m+j.fi-1);
			}
		}

		cout << (ans - n)/2;		
		return 0;
	}
	if(b == 3) {
		for(int i=1; i<=n; i++) {
			cnt[p[i].x[0]][p[i].x[1]][p[i].x[2]]++;
		}

		for(int i=0; i<=min(d, 3*m-3); i++) {
			for(int j=1; j<=m; j++) {
				for(int k=1; k<=m; k++) {
					for(int l=1; l<=m; l++) {
						if(i == 0) {
							dp[0][j][k][l] = cnt[j][k][l];
							continue;
						}

						dp[i%3][j][k][l] = dp[(i+2)%3][j+1][k][l];
						dp[i%3][j][k][l] += dp[(i+2)%3][j-1][k][l];

						if(i > 1 && j > 1 && j < m) dp[i%3][j][k][l] -= dp[(i+1)%3][j][k][l];
						if(i == 2) dp[i%3][j][k][l] -= cnt[j][k][l];
					}
				}
			}

			for(int j=1; j<=m; j++) {
				for(int k=1; k<=m; k++) {
					for(int l=1; l<=m; l++) {
						dp2[i%3][j][k][l] = dp[i%3][j][k][l];
						if(i == 0) continue;
						dp2[i%3][j][k][l] += dp2[(i+2)%3][j][k-1][l];
						dp2[i%3][j][k][l] += dp2[(i+2)%3][j][k+1][l];

						if(i > 1 && k > 1 && k < m) dp2[i%3][j][k][l] -= dp2[(i+1)%3][j][k][l];
						if(i > 1) dp2[i%3][j][k][l] -= dp[(i+1)%3][j][k][l];
					}
				}
			}
		
			for(int j=1; j<=m; j++) {
				for(int k=1; k<=m; k++) {
					for(int l=1; l<=m; l++) {
						dp3[i%3][j][k][l] = dp2[i%3][j][k][l];
						if(i == 0) continue;
						dp3[i%3][j][k][l] += dp3[(i+2)%3][j][k][l-1];
						dp3[i%3][j][k][l] += dp3[(i+2)%3][j][k][l+1];
						
						if(i > 1 && l > 1 && l < m) dp3[i%3][j][k][l] -= dp3[(i+1)%3][j][k][l];
						if(i > 1) dp3[i%3][j][k][l] -= dp2[(i+1)%3][j][k][l];

						tot[j][k][l] += dp3[i%3][j][k][l];
					}
				}
			}
		}

		ll ans = 0;
		for(int i=1; i<=m; i++) {
			for(int j=1; j<=m; j++) {
				for(int k=1; k<=m; k++) {
					ans += (ll) (tot[i][j][k] + cnt[i][j][k] - 1) * cnt[i][j][k];
				}
			}
		}
		cout << ans / 2;
		return 0;
	}
}
# Verdict Execution time Memory Grader output
1 Correct 28 ms 43808 KB Output is correct
2 Correct 27 ms 43776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 28 ms 43904 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 45 ms 43776 KB Output is correct
2 Correct 44 ms 44160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 50 ms 43872 KB Output is correct
2 Correct 47 ms 44672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 43776 KB Output is correct
2 Correct 58 ms 44664 KB Output is correct
3 Correct 55 ms 44672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 45056 KB Output is correct
2 Correct 35 ms 44928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 46584 KB Output is correct
2 Correct 61 ms 46096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 68 ms 46916 KB Output is correct
2 Correct 61 ms 46820 KB Output is correct
3 Correct 60 ms 46840 KB Output is correct
4 Correct 61 ms 46736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 147 ms 51320 KB Output is correct
2 Correct 147 ms 50660 KB Output is correct
3 Correct 69 ms 48116 KB Output is correct
4 Correct 71 ms 47992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 138 ms 64248 KB Output is correct
2 Correct 895 ms 64224 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 44408 KB Output is correct
2 Correct 53 ms 45176 KB Output is correct
3 Correct 50 ms 45176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 80 ms 59000 KB Output is correct
2 Correct 180 ms 59896 KB Output is correct
3 Correct 388 ms 59896 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 187 ms 64464 KB Output is correct
2 Correct 339 ms 65400 KB Output is correct
3 Correct 724 ms 65360 KB Output is correct