//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
//#include "grader.h"
#define pii pair<int, int>
#define pll pair<long long, long long>
#define vii vector<pii>
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long int ll;
const int INF = 1e9 + 7;
const int MAXN = 3e5 + 5;
vi x[MAXN], y[MAXN], adjV[MAXN], adjH[MAXN];
int vV[MAXN], vH[MAXN];
ll dp1[MAXN], dp2[MAXN];
int n, cntV = 0, cntH = 0;
void dfsH1(int u, int p) {
dp1[u] = vH[u];
for (int v: adjH[u]) {
if (v != p) {
dfsH1(v, u);
dp1[u] += dp1[v];
}
}
}
void dfsH2(int u, int p) {
if (p != -1)
dp2[u] = dp2[p] + dp1[p] - dp1[u];
else
dp2[u] = 0;
for (int v: adjH[u])
if (v != p)
dfsH2(v, u);
}
void dfsV1(int u, int p) {
dp1[u] = vV[u];
for (int v: adjV[u]) {
if (v != p) {
dfsV1(v, u);
dp1[u] += dp1[v];
}
}
}
void dfsV2(int u, int p) {
if (p != -1)
dp2[u] = dp2[p] + dp1[p] - dp1[u];
else
dp2[u] = 0;
for (int v: adjV[u])
if (v != p)
dfsV2(v, u);
}
ll DistanceSum(int N, int *X, int *Y) {
n = N;
int miX = INF, miY = INF;
for (int i = 0; i < N; i++) {
miX = min(miX, X[i]);
miY = min(miY, Y[i]);
}
for (int i = 0; i < N; i++) {
x[X[i] - miX].pb(Y[i] - miY);
y[Y[i] - miY].pb(X[i] - miX);
}
map<int,int> prevX, curX, prevY, curY;
for (int i = 0; i < N; i++) {
sort(x[i].begin(), x[i].end()); // vert
for (int j = 0; j < x[i].size(); j++) {
int k = j;
curX[x[i][j]] = cntV;
while (k < x[i].size() && x[i][j] - j == x[i][k] - k) {
curX[x[i][k]] = cntV;
if (prevX.find(x[i][k]) != prevX.end()) {
int u = prevX[x[i][k]];
adjV[cntV].pb(u);
adjV[u].pb(cntV);
}
k++;
}
vV[cntV++] = k - j;
j = k - 1;
}
swap(prevX, curX);
curX.clear();
sort(y[i].begin(), y[i].end()); // hori
for (int j = 0; j < y[i].size(); j++) {
int k = j;
while (k < y[i].size() && y[i][j] - j == y[i][k] - k) {
curY[y[i][k]] = cntH;
if (prevY.find(y[i][k]) != prevY.end()) {
int u = prevY[y[i][k]];
adjH[cntH].pb(u);
adjH[u].pb(cntH);
}
k++;
}
vH[cntH++] = k - j;
j = k - 1;
}
swap(prevY, curY);
curY.clear();
}
for (int i = 0; i < cntH; i++) {
sort(adjH[i].begin(), adjH[i].end());
adjH[i].erase(unique(adjH[i].begin(), adjH[i].end()), adjH[i].end());
}
for (int i = 0; i < cntV; i++) {
sort(adjV[i].begin(), adjV[i].end());
adjV[i].erase(unique(adjV[i].begin(), adjV[i].end()), adjV[i].end());
}
ll ret = 0;
dfsH1(0, -1);
dfsH2(0, -1);
for (int i = 0; i < cntH; i++) {
//cout << vH[i] << "\n";
ret += 1ll * (ll) dp1[i] * 1ll * (ll) dp2[i];
}
cout << "\n";
memset(dp1, 0, sizeof dp1);
memset(dp2, 0, sizeof dp2);
dfsV1(0, -1);
dfsV2(0, -1);
for (int i = 0; i < cntV; i++) {
//cout << vV[i] << "\n";
ret += 1ll * (ll) dp1[i] * 1ll * (ll) dp2[i];
}
return ret;
}
Compilation message
city.cpp: In function 'll DistanceSum(int, int*, int*)':
city.cpp:75:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < x[i].size(); j++) {
~~^~~~~~~~~~~~~
city.cpp:78:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (k < x[i].size() && x[i][j] - j == x[i][k] - k) {
~~^~~~~~~~~~~~~
city.cpp:93:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0; j < y[i].size(); j++) {
~~^~~~~~~~~~~~~
city.cpp:95:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (k < y[i].size() && y[i][j] - j == y[i][k] - k) {
~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
33272 KB |
Output is correct |
2 |
Correct |
36 ms |
33236 KB |
Output is correct |
3 |
Correct |
36 ms |
33400 KB |
Output is correct |
4 |
Correct |
33 ms |
33144 KB |
Output is correct |
5 |
Correct |
33 ms |
33152 KB |
Output is correct |
6 |
Correct |
31 ms |
33280 KB |
Output is correct |
7 |
Correct |
34 ms |
33144 KB |
Output is correct |
8 |
Correct |
34 ms |
33144 KB |
Output is correct |
9 |
Correct |
37 ms |
33272 KB |
Output is correct |
10 |
Correct |
32 ms |
33280 KB |
Output is correct |
11 |
Correct |
33 ms |
33152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
33272 KB |
Output is correct |
2 |
Correct |
33 ms |
33272 KB |
Output is correct |
3 |
Correct |
33 ms |
33280 KB |
Output is correct |
4 |
Correct |
34 ms |
33272 KB |
Output is correct |
5 |
Correct |
35 ms |
33408 KB |
Output is correct |
6 |
Correct |
31 ms |
33320 KB |
Output is correct |
7 |
Correct |
33 ms |
33268 KB |
Output is correct |
8 |
Correct |
36 ms |
33408 KB |
Output is correct |
9 |
Correct |
43 ms |
33272 KB |
Output is correct |
10 |
Correct |
33 ms |
33280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
48 ms |
34132 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
56 ms |
34372 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |