#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
typedef long long ll;
const int N = 100005, M = 1000000000;
const int xx[4] = {0, 1, 0, -1};
const int yy[4] = {1, 0, -1, 0};
ll ans;
int n;
int X[N], Y[N];
map<pair<int, int>, int> mp;
vector<int> g[N];
bool c[N];
int d[N];
void bfs(int i)
{
for (int i = 0; i < n; ++i)
c[i] = false;
queue<int> q;
c[i] = true;
d[i] = 0;
q.push(i);
while (!q.empty())
{
int i = q.front();
q.pop();
for (int j = 0; j < g[i].size(); ++j)
{
int h = g[i][j];
if (!c[h])
{
c[h] = true;
d[h] = d[i] + 1;
q.push(h);
}
}
}
}
int px[N], py[N];
void dfs(int i, ll yans)
{
c[i] = true;
ans += yans;
for (int j = 0; j < 4; ++j)
{
int x = X[i] + xx[j];
int y = Y[i] + yy[j];
if (mp.find(m_p(x, y)) != mp.end())
{
int h = mp[m_p(x, y)];
if (c[h])
continue;
if (xx[j] == 1)
dfs(h, yans + px[X[i]] - (n - px[X[i]]));
else if (xx[j] == -1)
dfs(h, yans - px[X[i] - 1] + (n - px[X[i] - 1]));
else if (yy[j] == 1)
dfs(h, yans + py[Y[i]] - (n - py[Y[i]]));
else
dfs(h, yans - py[Y[i] - 1] + (n - py[Y[i] - 1]));
}
}
}
int DistanceSum(int N, int *X_, int *Y_)
{
n = N;
for (int i = 0; i < n; ++i)
{
X[i] = X_[i];
Y[i] = Y_[i];
}
int minx = X[0];
int miny = Y[0];
for (int i = 0; i < n; ++i)
{
minx = min(minx, X[i]);
miny = min(miny, Y[i]);
}
for (int i = 0; i < n; ++i)
{
X[i] -= (minx - 1);
Y[i] -= (miny - 1);
}
for (int i = 0; i < n; ++i)
{
mp[m_p(X[i], Y[i])] = i;
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < 4; ++j)
{
int x = X[i] + xx[j];
int y = Y[i] + yy[j];
if (mp.find(m_p(x, y)) != mp.end())
g[i].push_back(mp[m_p(x, y)]);
}
}
if (n <= 2000)
{
for (int i = 0; i < n; ++i)
{
bfs(i);
for (int j = i + 1; j < n; ++j)
ans = (ans + d[j]);
}
return ans % M;
}
else
{
for (int i = 0; i < n; ++i)
{
px[X[i]]++;
py[Y[i]]++;
}
for (int i = 1; i <= n; ++i)
{
px[i] += px[i - 1];
py[i] += py[i - 1];
}
bfs(0);
ll yans = 0;
for (int i = 0; i < n; ++i)
yans += d[i];
memset(c, false, sizeof c);
dfs(0, yans);
ans /= 2;
return ans % M;
}
}
Compilation message
city.cpp: In function 'void bfs(int)':
city.cpp:35:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < g[i].size(); ++j)
~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
3 ms |
2688 KB |
Output is correct |
7 |
Correct |
3 ms |
2688 KB |
Output is correct |
8 |
Correct |
3 ms |
2688 KB |
Output is correct |
9 |
Correct |
3 ms |
2688 KB |
Output is correct |
10 |
Correct |
3 ms |
2688 KB |
Output is correct |
11 |
Correct |
3 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
2816 KB |
Output is correct |
2 |
Correct |
29 ms |
2816 KB |
Output is correct |
3 |
Correct |
67 ms |
2924 KB |
Output is correct |
4 |
Correct |
72 ms |
2816 KB |
Output is correct |
5 |
Correct |
116 ms |
3064 KB |
Output is correct |
6 |
Correct |
109 ms |
2944 KB |
Output is correct |
7 |
Correct |
120 ms |
2944 KB |
Output is correct |
8 |
Correct |
109 ms |
2988 KB |
Output is correct |
9 |
Correct |
104 ms |
2944 KB |
Output is correct |
10 |
Correct |
101 ms |
3064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
7544 KB |
Output is correct |
2 |
Correct |
54 ms |
7672 KB |
Output is correct |
3 |
Correct |
148 ms |
14712 KB |
Output is correct |
4 |
Correct |
147 ms |
14712 KB |
Output is correct |
5 |
Correct |
367 ms |
26616 KB |
Output is correct |
6 |
Correct |
376 ms |
26616 KB |
Output is correct |
7 |
Correct |
348 ms |
25848 KB |
Output is correct |
8 |
Correct |
354 ms |
26616 KB |
Output is correct |
9 |
Correct |
336 ms |
26616 KB |
Output is correct |
10 |
Correct |
307 ms |
26360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
47 ms |
5496 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |