Submission #118443

# Submission time Handle Problem Language Result Execution time Memory
118443 2019-06-19T03:42:41 Z E869120 Ideal city (IOI12_city) C++14
55 / 100
1000 ms 28008 KB
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <tuple>
#include <map>
using namespace std;

long long N, X[100009], Y[100009], col[100009], cnt[100009], rem, dp[100009]; bool used[100009];
long long ans, mod = 1000000000;
vector<int>G[100009];

void dfs1(int pos, long long dep) {
	used[pos] = true;
	rem += dep * cnt[pos]; dp[pos] = cnt[pos];
	for (int i = 0; i < G[pos].size(); i++) {
		if (used[G[pos][i]] == true) continue;
		dfs1(G[pos][i], dep + 1);
		dp[pos] += dp[G[pos][i]];
	}
}

void dfs2(int pos) {
	ans += 1LL * cnt[pos] * rem; used[pos] = true;
	for (int i = 0; i < G[pos].size(); i++) {
		if (used[G[pos][i]] == true) continue;
		rem -= (dp[G[pos][i]]) - (N - dp[G[pos][i]]);
		dfs2(G[pos][i]);
		rem += (dp[G[pos][i]]) - (N - dp[G[pos][i]]);
	}
}

long long solve() {
	rem = 0; ans = 0;
	for (int i = 0; i <= N; i++) { G[i].clear(); col[i] = 0; cnt[i] = 0; dp[i] = 0; used[i] = false; }

	vector<tuple<int, int, int>>vec;
	map<pair<int, int>, int>Map, Map2;
	for (int i = 0; i < N; i++) {
		Map[make_pair(X[i], Y[i])] = i + 1;
		vec.push_back(make_tuple(X[i], Y[i], i));
	}
	sort(vec.begin(), vec.end());

	int cnts = 0;
	for (int i = 0; i < vec.size(); i++) {
		if (col[get<2>(vec[i])] >= 1) continue;
		int ex = get<0>(vec[i]), ey = get<1>(vec[i]); cnts++;
		while (true) {
			int num = Map[make_pair(1LL * ex, 1LL * ey)] - 1;
			col[num] = cnts; cnt[cnts]++;
			ey++;
			if (Map[make_pair(ex, ey)] == 0) break;
		}
	}

	for (int i = 0; i < N; i++) {
		int dx[4] = { 1, -1 }, dy[4] = { 0, 0 };
		for (int j = 0; j < 2; j++) {
			int ex = X[i] + dx[j], ey = Y[i] + dy[j];

			int D = Map[make_pair(ex, ey)]; if (D == 0) continue;
			int t1 = i, t2 = D - 1;
			t1 = col[t1]; t2 = col[t2];
			if (Map2[make_pair(t1, t2)] == 1 || t1 == t2) continue;
			G[t1].push_back(t2); Map2[make_pair(t1, t2)] = 1;
			G[t2].push_back(t1); Map2[make_pair(t2, t1)] = 1;
		}
	}

	dfs1(1, 0);
	for (int i = 1; i <= cnts; i++) used[i] = false;
	dfs2(1);
	return ans;
}

int DistanceSum(int NN, int *AX, int *AY) {
	N = NN; long long finalans = 0;
	for (int i = 0; i < N; i++) { X[i] = AX[i]; Y[i] = AY[i]; }
	for (int i = 0; i < 2; i++) {
		long long ret = solve();
		finalans += ret;
		for (int j = 0; j < N; j++) swap(X[j], Y[j]);
	}
	return (finalans / 2LL) % mod;
}

Compilation message

city.cpp: In function 'void dfs1(int, long long int)':
city.cpp:16:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < G[pos].size(); i++) {
                  ~~^~~~~~~~~~~~~~~
city.cpp: In function 'void dfs2(int)':
city.cpp:25:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < G[pos].size(); i++) {
                  ~~^~~~~~~~~~~~~~~
city.cpp: In function 'long long int solve()':
city.cpp:46:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < vec.size(); i++) {
                  ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 2688 KB Output is correct
2 Correct 4 ms 2688 KB Output is correct
3 Correct 4 ms 2688 KB Output is correct
4 Correct 4 ms 2688 KB Output is correct
5 Correct 4 ms 2688 KB Output is correct
6 Correct 4 ms 2816 KB Output is correct
7 Correct 4 ms 2688 KB Output is correct
8 Correct 4 ms 2688 KB Output is correct
9 Correct 4 ms 2688 KB Output is correct
10 Correct 4 ms 2688 KB Output is correct
11 Correct 4 ms 2816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 2944 KB Output is correct
2 Correct 6 ms 2944 KB Output is correct
3 Correct 8 ms 3072 KB Output is correct
4 Correct 7 ms 2944 KB Output is correct
5 Correct 10 ms 3200 KB Output is correct
6 Correct 8 ms 3072 KB Output is correct
7 Correct 9 ms 3200 KB Output is correct
8 Correct 8 ms 3072 KB Output is correct
9 Correct 8 ms 3072 KB Output is correct
10 Correct 8 ms 3072 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 5512 KB Output is correct
2 Correct 65 ms 5704 KB Output is correct
3 Correct 200 ms 9440 KB Output is correct
4 Correct 185 ms 9276 KB Output is correct
5 Correct 544 ms 16540 KB Output is correct
6 Correct 532 ms 16168 KB Output is correct
7 Correct 511 ms 16468 KB Output is correct
8 Correct 512 ms 15148 KB Output is correct
9 Correct 686 ms 16116 KB Output is correct
10 Correct 637 ms 23372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 113 ms 7536 KB Output is correct
2 Correct 96 ms 6724 KB Output is correct
3 Correct 385 ms 14816 KB Output is correct
4 Correct 281 ms 12512 KB Output is correct
5 Correct 931 ms 26804 KB Output is correct
6 Correct 723 ms 20300 KB Output is correct
7 Execution timed out 1020 ms 28008 KB Time limit exceeded
8 Halted 0 ms 0 KB -