# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
118444 |
2019-06-19T03:45:14 Z |
E869120 |
Ideal city (IOI12_city) |
C++14 |
|
1000 ms |
26772 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++;
int rems = get<2>(vec[i]);
while (true) {
col[rems] = cnts; cnt[cnts]++;
ey++;
rems = Map[make_pair(ex, ey)] - 1;
if (rems == -1) 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 |
6 ms |
2816 KB |
Output is correct |
5 |
Correct |
4 ms |
2688 KB |
Output is correct |
6 |
Correct |
5 ms |
2816 KB |
Output is correct |
7 |
Correct |
4 ms |
2688 KB |
Output is correct |
8 |
Correct |
5 ms |
2688 KB |
Output is correct |
9 |
Correct |
5 ms |
2688 KB |
Output is correct |
10 |
Correct |
4 ms |
2688 KB |
Output is correct |
11 |
Correct |
4 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2944 KB |
Output is correct |
2 |
Correct |
6 ms |
2944 KB |
Output is correct |
3 |
Correct |
7 ms |
3072 KB |
Output is correct |
4 |
Correct |
7 ms |
3200 KB |
Output is correct |
5 |
Correct |
9 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 |
7 ms |
3072 KB |
Output is correct |
10 |
Correct |
7 ms |
3072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
5576 KB |
Output is correct |
2 |
Correct |
55 ms |
5832 KB |
Output is correct |
3 |
Correct |
178 ms |
9388 KB |
Output is correct |
4 |
Correct |
175 ms |
9312 KB |
Output is correct |
5 |
Correct |
568 ms |
16516 KB |
Output is correct |
6 |
Correct |
548 ms |
16268 KB |
Output is correct |
7 |
Correct |
558 ms |
16556 KB |
Output is correct |
8 |
Correct |
500 ms |
15248 KB |
Output is correct |
9 |
Correct |
598 ms |
15892 KB |
Output is correct |
10 |
Correct |
646 ms |
23304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
7620 KB |
Output is correct |
2 |
Correct |
89 ms |
6640 KB |
Output is correct |
3 |
Correct |
400 ms |
14920 KB |
Output is correct |
4 |
Correct |
305 ms |
12384 KB |
Output is correct |
5 |
Execution timed out |
1067 ms |
26772 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |