#include <bits/stdc++.h>
#define int long long
#define P(x) do {if(debug) cout << x << endl;} while(false)
#define H(x) P(#x << ": " << x)
#define FR(i, a, b) for(int i = (a); i < (b); ++i)
#define F(i, n) FR(i, 0, n)
#define DR(i, a, b) for(int i = (b); i --> (a);)
#define D(i, n) DR(i, 0, n)
#define S(s) (int)(s).size()
#define ALL(x) (x).begin(), (x).end()
#define MI(x, a) (x) = min(x, a)
#define MA(x, a) (x) = max(x, a)
#define V vector
#define pb push_back
#define mp make_pair
using namespace std;
const bool debug = 0;
const int inf = 1e18;
const int mod = 1e9;
vector<vector<int> > graph;
vector<int> parts;
vector<int> sd, nn;
void dfs1(int u, int par){
for(int v : graph[u]) if(v!=par){
dfs1(v, u);
nn[u] = (nn[u] + nn[v]);
sd[u] = (sd[u] + sd[v] + nn[v]);
}
}
int tot;
void dfs2(int u, int par, int sdpar, int nnpar){
H(u); H(par); H(nn[u]); H(sd[u]); H(sdpar); H(nnpar); H(sd[u] + sdpar + nnpar);
tot += parts[u]*(sd[u] + sdpar + nnpar);
sdpar+=nnpar;
for(int v : graph[u]) if(v!=par){
dfs2(v, u, sdpar+sd[u]-sd[v]-nn[v], nnpar+nn[u]-nn[v]);
}
}
int calc(int N,vector<int>& x, vector<int>& y){
map<pair<int,int>, int> mpa;
vector<pair<int,int> > v(N);
F(i, N){
v[i] = mp(x[i], y[i]);
mpa[v[i]] = i;
}
sort(v.begin(), v.end());
graph = { { } };
vector<int> part(N);
parts = {0};
int j = 0;
for(int i = 0; i<N; i++){
if(i>0 && v[i-1] != mp(v[i].first,v[i].second-1)){
j++;
graph.resize(graph.size()+1);
parts.push_back(0);
}
part[mpa[v[i]]] = j;
parts[j]++;
int a = 0;
if(mpa.count({v[i].first-1,v[i].second})){
if(!(graph[j].size()>=1 && graph[j][graph[j].size()-1] == part[mpa[mp(v[i].first-1,v[i].second)]])
&& !(graph[j].size()>=2 && graph[j][graph[j].size()-2] == part[mpa[mp(v[i].first-1,v[i].second)]]) ){
graph[j].push_back(part[mpa[mp(v[i].first-1,v[i].second)]]);
graph[part[mpa[mp(v[i].first-1,v[i].second)]]].push_back(j);
}
//a=1;
}
}
/*for(int i = 0; i<graph.size(); i++){
for(int j : graph[i]){
cout<<j<<" ";
}
cout<<endl;
}*/
sd.assign(graph.size(),0);
nn = parts;
tot = 0;
dfs1(0,0);
dfs2(0,0,0,0);
return tot/2;
}
int DistanceSum(signed N, signed *X, signed *Y) {
vector<int> x(N), y(N);
F(i,N){
x[i] = X[i];
y[i] = Y[i];
}
return (calc(N, x, y) + calc(N, y, x))%mod;
}
Compilation message
city.cpp: In function 'long long int calc(long long int, std::vector<long long int>&, std::vector<long long int>&)':
city.cpp:64:7: warning: unused variable 'a' [-Wunused-variable]
int a = 0;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
256 KB |
Output is correct |
2 |
Correct |
2 ms |
248 KB |
Output is correct |
3 |
Correct |
2 ms |
256 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
3 ms |
508 KB |
Output is correct |
3 |
Correct |
4 ms |
632 KB |
Output is correct |
4 |
Correct |
4 ms |
504 KB |
Output is correct |
5 |
Correct |
5 ms |
604 KB |
Output is correct |
6 |
Correct |
5 ms |
632 KB |
Output is correct |
7 |
Correct |
5 ms |
632 KB |
Output is correct |
8 |
Correct |
5 ms |
632 KB |
Output is correct |
9 |
Correct |
5 ms |
632 KB |
Output is correct |
10 |
Correct |
5 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
2808 KB |
Output is correct |
2 |
Correct |
36 ms |
2844 KB |
Output is correct |
3 |
Correct |
93 ms |
6284 KB |
Output is correct |
4 |
Correct |
94 ms |
6520 KB |
Output is correct |
5 |
Correct |
213 ms |
12340 KB |
Output is correct |
6 |
Correct |
209 ms |
12412 KB |
Output is correct |
7 |
Correct |
215 ms |
12752 KB |
Output is correct |
8 |
Correct |
208 ms |
12140 KB |
Output is correct |
9 |
Correct |
210 ms |
12664 KB |
Output is correct |
10 |
Correct |
241 ms |
17088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
3448 KB |
Output is correct |
2 |
Correct |
40 ms |
3320 KB |
Output is correct |
3 |
Correct |
113 ms |
8252 KB |
Output is correct |
4 |
Correct |
103 ms |
7580 KB |
Output is correct |
5 |
Correct |
250 ms |
15520 KB |
Output is correct |
6 |
Correct |
226 ms |
13668 KB |
Output is correct |
7 |
Correct |
250 ms |
15804 KB |
Output is correct |
8 |
Correct |
230 ms |
13720 KB |
Output is correct |
9 |
Correct |
217 ms |
13448 KB |
Output is correct |
10 |
Correct |
223 ms |
13244 KB |
Output is correct |