답안 #105411

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
105411 2019-04-12T02:35:49 Z luciocf 이상적인 도시 (IOI12_city) C++14
100 / 100
329 ms 19864 KB
#include <bits/stdc++.h>

#define ff first
#define ss second

using namespace std;

const int maxn = 1e5+10;
const int mod = 1e9;

typedef pair<int, int> pii;

int n, ans;
pii pt[maxn];

// ----- TREES STUFF ------
int vert[maxn], A[maxn];

vector<int> grafo[maxn];

map<pii, int> ind, conecta;
// ------------------------

// -------- DFS STUFF ---------
int S[maxn], W[maxn];
// ----------------------------

bool comp(pii a, pii b)
{
	if (a.ss == b.ss) return a.ff < b.ff;
	return a.ss < b.ss; 
}

void init()
{
	ind.clear(); conecta.clear();
	memset(W, 0, sizeof W); memset(S, 0, sizeof S);
	
	for (int i = 1; i <= n; i++)
		grafo[i].clear(), A[i] = 0;
}

void make_tree1(void)
{
	sort(pt+1, pt+n+1);
	for (int i = 1; i <= n; i++)
		ind[pt[i]] = i;

	int atual = 1;
	vert[1] = 1, A[1] = 1;

	for (int i = 2; i <= n; i++)
	{
		if (pt[i].ff > pt[i-1].ff || pt[i].ss-pt[i-1].ss > 1)
		{
			vert[i] = ++atual;
			A[atual]++;
			continue;
		}

		vert[i] = atual;
		A[atual]++;
	}

	for (int i = 1; i <= n; i++)
	{
		if (ind.find({pt[i].ff-1, pt[i].ss}) != ind.end())
		{
			int u = i, v = ind[{pt[i].ff-1, pt[i].ss}];
			u = vert[u], v = vert[v];

			if (!conecta[{u, v}])
			{
				conecta[{u, v}] = conecta[{v, u}] = 1;
				grafo[u].push_back(v), grafo[v].push_back(u);
			}
		}
	}
}

void make_tree2(void)
{
	sort(pt+1, pt+n+1, comp);
	for (int i = 1; i <= n; i++)
		ind[pt[i]] = i;

	int atual = 1;
	vert[1] = 1, A[1] = 1; 

	for (int i = 2; i <= n; i++)
	{
		if (pt[i].ss > pt[i-1].ss || pt[i].ff-pt[i-1].ff > 1)
		{
			vert[i] = ++atual;
			A[atual]++;
			continue;
		}

		vert[i] = atual;
		A[atual]++;
	}

	for (int i = 1; i <= n; i++)
	{
		if (ind.find({pt[i].ff, pt[i].ss-1}) != ind.end())
		{
			int u = i, v = ind[{pt[i].ff, pt[i].ss-1}];
			u = vert[u], v = vert[v];

			if (!conecta[{u, v}])
			{
				conecta[{u, v}] = conecta[{v, u}] = 1;
				grafo[u].push_back(v), grafo[v].push_back(u);
			}
		}
	}
}

void dfs(int u, int p)
{
	for (auto v: grafo[u])
	{
		if (v == p) continue;

		dfs(v, u);

		int sub = (W[v]+S[v])%mod;
		long long mult = (1ll*W[u]*S[v] + 1ll*sub*S[u])%mod;

		ans = (ans+(1ll*sub*A[u])%mod)%mod;
		ans = (ans+mult)%mod;

		W[u] = (W[u]+sub)%mod;
		S[u] = (S[u]+S[v])%mod;
	}

	S[u] = (S[u]+A[u])%mod;
}

int DistanceSum(int N, int *X, int *Y)
{
	n = N;
	for (int i = 1; i <= n; i++)
		pt[i] = {X[i-1], Y[i-1]};

	make_tree1();
	dfs(1, 0);

	init();

	make_tree2();
	dfs(1, 0);

	return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 3456 KB Output is correct
2 Correct 5 ms 3456 KB Output is correct
3 Correct 6 ms 3456 KB Output is correct
4 Correct 6 ms 3456 KB Output is correct
5 Correct 6 ms 3456 KB Output is correct
6 Correct 6 ms 3584 KB Output is correct
7 Correct 5 ms 3456 KB Output is correct
8 Correct 5 ms 3456 KB Output is correct
9 Correct 6 ms 3456 KB Output is correct
10 Correct 5 ms 3456 KB Output is correct
11 Correct 6 ms 3584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 3584 KB Output is correct
2 Correct 8 ms 3584 KB Output is correct
3 Correct 7 ms 3712 KB Output is correct
4 Correct 7 ms 3712 KB Output is correct
5 Correct 10 ms 3712 KB Output is correct
6 Correct 9 ms 3712 KB Output is correct
7 Correct 8 ms 3840 KB Output is correct
8 Correct 9 ms 3712 KB Output is correct
9 Correct 9 ms 3840 KB Output is correct
10 Correct 8 ms 3712 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 31 ms 5376 KB Output is correct
2 Correct 32 ms 5476 KB Output is correct
3 Correct 99 ms 8056 KB Output is correct
4 Correct 87 ms 8184 KB Output is correct
5 Correct 217 ms 12536 KB Output is correct
6 Correct 160 ms 12536 KB Output is correct
7 Correct 178 ms 12908 KB Output is correct
8 Correct 172 ms 12252 KB Output is correct
9 Correct 170 ms 13092 KB Output is correct
10 Correct 180 ms 19864 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 60 ms 6776 KB Output is correct
2 Correct 40 ms 6324 KB Output is correct
3 Correct 140 ms 11384 KB Output is correct
4 Correct 111 ms 10332 KB Output is correct
5 Correct 282 ms 19380 KB Output is correct
6 Correct 216 ms 15480 KB Output is correct
7 Correct 329 ms 19552 KB Output is correct
8 Correct 241 ms 15792 KB Output is correct
9 Correct 230 ms 14968 KB Output is correct
10 Correct 249 ms 14840 KB Output is correct