Submission #31121

# Submission time Handle Problem Language Result Execution time Memory
31121 2017-08-10T06:05:42 Z h0ngjun7 Ideal city (IOI12_city) C++14
100 / 100
436 ms 19068 KB
#include <vector>
#include <map>
#include <set>
#include <algorithm>
using namespace std;

const int mod = 1e9;
const int MAXN = 1e5 + 5;

typedef long long ll;

int N;
struct XY {
	int x, y;
	XY() {}
	XY(int _x, int _y) { x = _x, y = _y; }
	bool operator < (const XY &a) const {
		if (x != a.x) return x < a.x;
		return y < a.y;
	}
};
struct PT {
	int x, y, k;
	PT() {}
	PT(int _x, int _y, int _k) { x = _x, y = _y; k = _k; }
} p[MAXN];

struct SEG {
	int s, e;
};

int u[MAXN], us[MAXN];
int f(int x) {
	if (x == u[x]) return x;
	return (u[x] = f(u[x]));
}
ll ans = 0;
int sub[MAXN];
vector <int> G[MAXN];
set <pair<int, int>> S;
void dfs(int x, int par) {
	for (auto &y : G[x]) {
		if (y == par) continue;
		dfs(y, x);
		sub[x] += sub[y];
	}
	sub[x] += us[x];
	ans += 1LL * sub[x] * (N - sub[x]);
	ans %= mod;
}
void solve() {
	map <XY, int> mp;
	sort(p, p + N, [&](PT a, PT b) { return (a.x != b.x) ? a.x < b.x : a.y < b.y; });
	for (int i = 1; i <= N; i++) {
		u[i] = i, us[i] = 1;
		G[i].clear(); sub[i] = 0;
	}
	S.clear();
	for (int i = 0; i < N; i++) mp[XY(p[i].x, p[i].y)] = p[i].k;
	for (int i = 0; i < N; i++) {
		int j = i;
		vector <int> Y;
		while (j < N && p[i].x == p[j].x) Y.push_back(p[j++].y);
		SEG L; L.s = L.e = Y[0];
		for (int j = 1; j < Y.size(); j++) {
			if (L.e + 1 == Y[j]) {
				L.e++;
				int A = f(mp[XY(p[i].x, Y[j] - 1)]);
				int B = f(mp[XY(p[i].x, Y[j])]);
				if (A != B) {
					u[A] = B;
					us[B] += us[A];
				}
			}
			else L.s = L.e = Y[j];
		}
		for (int k = i; k < j; k++) {
			if (mp.count(XY(p[i].x - 1, p[k].y))) {
				int A = f(mp[XY(p[i].x - 1, p[k].y)]);
				int B = f(p[k].k);
				if (A != B && !S.count({ A, B })) {
					G[A].push_back(B);
					G[B].push_back(A);
					S.insert({ A, B });
					S.insert({ B, A });
				}
			}
		}
		i = j - 1;
	}
	for (int i = 1; i <= N; i++) {
		if (f(i) == i) {
			dfs(i, -1);
			break;
		}
	}
}

int DistanceSum(int N, int *X, int *Y) {
	::N = N;
	ans = 0;
	int mx = X[0], my = Y[0];
	for (int i = 0; i < N; i++) {
		mx = min(mx, X[i]);
		my = min(my, Y[i]);
	}
	for (int i = 0; i < N; i++) p[i] = PT(X[i] - mx, Y[i] - my, i + 1);
	solve();
	for (int i = 0; i < N; i++) swap(p[i].x, p[i].y);
	solve();
	return (int)(ans);
}

Compilation message

city.cpp: In function 'void solve()':
city.cpp:65:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 1; j < Y.size(); j++) {
                     ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 6008 KB Output is correct
2 Correct 0 ms 6008 KB Output is correct
3 Correct 0 ms 6008 KB Output is correct
4 Correct 0 ms 6008 KB Output is correct
5 Correct 0 ms 6008 KB Output is correct
6 Correct 0 ms 6008 KB Output is correct
7 Correct 0 ms 6008 KB Output is correct
8 Correct 0 ms 6008 KB Output is correct
9 Correct 0 ms 6008 KB Output is correct
10 Correct 0 ms 6008 KB Output is correct
11 Correct 0 ms 6008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 6140 KB Output is correct
2 Correct 3 ms 6140 KB Output is correct
3 Correct 3 ms 6140 KB Output is correct
4 Correct 3 ms 6140 KB Output is correct
5 Correct 3 ms 6272 KB Output is correct
6 Correct 3 ms 6140 KB Output is correct
7 Correct 3 ms 6272 KB Output is correct
8 Correct 3 ms 6140 KB Output is correct
9 Correct 3 ms 6140 KB Output is correct
10 Correct 3 ms 6140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 46 ms 7472 KB Output is correct
2 Correct 43 ms 7472 KB Output is correct
3 Correct 119 ms 9568 KB Output is correct
4 Correct 116 ms 9700 KB Output is correct
5 Correct 276 ms 13128 KB Output is correct
6 Correct 259 ms 13256 KB Output is correct
7 Correct 283 ms 13384 KB Output is correct
8 Correct 256 ms 13128 KB Output is correct
9 Correct 299 ms 13544 KB Output is correct
10 Correct 336 ms 18388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 8656 KB Output is correct
2 Correct 49 ms 8132 KB Output is correct
3 Correct 186 ms 12468 KB Output is correct
4 Correct 159 ms 11280 KB Output is correct
5 Correct 389 ms 18932 KB Output is correct
6 Correct 323 ms 15284 KB Output is correct
7 Correct 436 ms 19068 KB Output is correct
8 Correct 349 ms 15492 KB Output is correct
9 Correct 326 ms 14960 KB Output is correct
10 Correct 333 ms 14700 KB Output is correct