#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <queue>
#include <map>
typedef long long llong;
const int MAXN = 100000 + 10;
const int MOD = 1'000'000'000;
int n;
llong ans;
int sz[MAXN];
int par[MAXN];
llong dp[MAXN];
int x[MAXN], y[MAXN];
std::pair <int,int> delta[] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
std::map <std::pair <int,int>, int> map;
std::vector <int> g[MAXN];
std::vector <int> t[MAXN];
std::queue <int> q;
void dfsDown(int node)
{
dp[node] = 0;
sz[node] = 1;
for (const int &u : t[node])
{
if (u == par[node])
{
continue;
}
dfsDown(u);
sz[node] += sz[u];
dp[node] += dp[u] + sz[u];
}
}
void dfsUp(int node)
{
std::vector <std::pair <int*, int>> rollback;
if (node != 1)
{
dp[node] = dp[par[node]] + n - 2 * sz[node];
// std::cout << "here: " << node << ' ' << par[node] << ' ' << dp[par[node]] << ' ' << sz[node] << ' ' << dp[node] << '\n';
for (const int &u : g[node])
{
if (par[u] != node)
{
rollback.push_back({&par[u], par[u]});
if (u != par[node] && par[par[u]] == par[node]) dp[node] -= sz[u];
par[u] = node;
}
}
}
for (const int &u : t[node])
{
if (u != par[node])
{
dfsUp(u);
}
}
for (auto [addr, val] : rollback)
{
*addr = val;
}
}
int DistanceSum(int N, int *X, int *Y)
{
n = N;
for (int i = 1 ; i <= n ; ++i)
{
x[i] = X[i - 1];
y[i] = Y[i - 1];
map[{x[i], y[i]}] = i;
}
for (int i = 1 ; i <= n ; ++i)
{
for (const auto &[dx, dy] : delta)
{
if (map.count({x[i] + dx, y[i] + dy}))
{
g[i].push_back(map[{x[i] + dx, y[i] + dy}]);
}
}
}
for (int root = 1 ; root <= n ; ++root)
{
q.push(root);
std::fill(par + 1, par + n + 1, -1);
par[root] = 0;
for (int i = 1 ; i <= n ; ++i)
{
t[i].clear();
}
while (!q.empty())
{
int top = q.front();
q.pop();
for (const int &i : g[top])
{
if (par[i] == -1)
{
par[i] = top;
t[top].push_back(i);
q.push(i);
}
}
}
dfsDown(root);
// std::cout << "dp: " << root << ' ' << x[root] << ' ' << y[root] << " = " << dp[root] << '\n';
ans += dp[root];
ans %= MOD;
}
return ans / 2;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
5016 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5016 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5012 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
5008 KB |
Output is correct |
10 |
Correct |
3 ms |
5008 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
5076 KB |
Output is correct |
2 |
Correct |
31 ms |
5076 KB |
Output is correct |
3 |
Correct |
82 ms |
5272 KB |
Output is correct |
4 |
Correct |
88 ms |
5280 KB |
Output is correct |
5 |
Correct |
139 ms |
5344 KB |
Output is correct |
6 |
Correct |
112 ms |
5332 KB |
Output is correct |
7 |
Correct |
145 ms |
5356 KB |
Output is correct |
8 |
Correct |
121 ms |
5364 KB |
Output is correct |
9 |
Correct |
113 ms |
5356 KB |
Output is correct |
10 |
Correct |
106 ms |
5268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1059 ms |
8268 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1061 ms |
8268 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |