#include <bits/stdc++.h>
using namespace std;
typedef int INT;
#define pb push_back
#define int long long
#define fst first
#define snd second
#define FOR(i,l,r) for(int i = (l); i < (r); i++)
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> ii;
typedef vector<ii> vii;
int N;
vii points;
vvi adj;
map<ii, int> idx;
vvi rowgr, colgr;
vi rowsz, colsz;
vi row, col;
vi vis;
void flood(int u, int c, bool co){
vis[u] = 1;
if(co) col[u] = c, colsz[c] ++;
else row[u] = c, rowsz[c] ++;
for(int i = co; i < 4; i+= 2) if(adj[u][i] != -1 and !vis[adj[u][i]]) flood(adj[u][i], c, co);
for(int i = 1-co; i < 4; i+= 2) if(adj[u][i] != -1 and vis[adj[u][i]]) {
int v = adj[u][i];
if(v == -1) continue;
if(co) colgr[c].pb(col[v]), colgr[col[v]].pb(c);
else rowgr[c].pb(row[v]), rowgr[row[v]].pb(c);
}
}
map<ii, int> dp[2];
int dfs(int u, int p, bool col){
int &res = dp[col][{u, p}];
if(res != 0) return res;
res = col ? colsz[u] : rowsz[u];
for(int v : (col ? colgr[u] : rowgr[u]) ) if(v != p) res += dfs(v, u, col);
return res;
}
int gettot(int u, int tot){
//cout << u << " " << tot << endl;
int res = tot;
vis[u] = 1;
FOR(i, 0, 4){
int v = adj[u][i];
if(v == -1 || vis[v]) continue;
if(i % 2 == 0) { res += gettot(v, tot - 2*dfs(col[v], col[u], 1) + N); }
else res += gettot(v, tot - 2*dfs(row[v], row[u], 0) + N);
}
return res;
}
int bfs(int u){
vis.assign(N, 0);
int total = 0;
queue<ii> q;
q.push({u, 0});
vis[u] = 1;
while(!q.empty()){
ii p = q.front(); q.pop();
total += p.snd;
for(int v : adj[p.fst])if(v !=-1 and !vis[v]){
q.push({v,p.snd+1});
vis[v] = 1;
}
}
vis.assign(N, 0);
return total;
}
INT DistanceSum(INT n, INT *X, INT *Y) {
N = n;
adj.assign(N, vi(4, -1));
FOR(i, 0, N) {points.pb({X[i], Y[i]}); idx[{X[i], Y[i]}] = i+1;}
FOR(i, 0, N){
int x = X[i], y = Y[i];
int neigh;
if(neigh = idx[{x+1, y}]) adj[i][0] = neigh-1;
if(neigh = idx[{x, y-1}]) adj[i][1] = neigh-1;
if(neigh = idx[{x-1, y}]) adj[i][2] = neigh-1;
if(neigh = idx[{x, y+1}]) adj[i][3] = neigh-1;
}
idx.clear();
int k = 0;
row.assign(N, -1);
col.assign(N, -1);
vis.assign(N, 0);
FOR(i, 0, N) if(!vis[i]){
rowgr.pb(vi(0));
rowsz.pb(0);
flood(i, k++,0);
}
for(auto &v : rowgr){
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
}
k = 0;
vis.assign(N, 0);
FOR(i, 0, N) if(!vis[i]){
colgr.pb(vi(0));
colsz.pb(0);
flood(i, k++,1);
}
vis.assign(N, 0);
for(auto &v : colgr){
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
}
/*
for(auto &v : adj) {
for(int k : v) cout << k << " ";
cout<<endl;
}
for(auto &v : colgr) {
for(int k : v) cout << k << " ";
cout<<endl;
}
cout<<endl;
for(auto &v : rowgr) {
for(int k : v) cout << k << " ";
cout<<endl;
}
for(int k : col) cout << k << " ";
cout<<endl;
for(int k : row) cout << k << " ";
cout<<endl;
cout << dfs(1, 0, 0) << endl;
cout << bfs(0) <<endl;
*/
return (gettot(0, bfs(0))/2) % 1000000000;
}
Compilation message
city.cpp: In function 'INT DistanceSum(INT, INT*, INT*)':
city.cpp:89:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
if(neigh = idx[{x+1, y}]) adj[i][0] = neigh-1;
city.cpp:90:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
if(neigh = idx[{x, y-1}]) adj[i][1] = neigh-1;
city.cpp:91:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
if(neigh = idx[{x-1, y}]) adj[i][2] = neigh-1;
city.cpp:92:12: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
if(neigh = idx[{x, y+1}]) adj[i][3] = neigh-1;
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
376 KB |
Output is correct |
2 |
Correct |
4 ms |
604 KB |
Output is correct |
3 |
Correct |
3 ms |
604 KB |
Output is correct |
4 |
Correct |
3 ms |
604 KB |
Output is correct |
5 |
Correct |
3 ms |
604 KB |
Output is correct |
6 |
Correct |
4 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
4 ms |
604 KB |
Output is correct |
9 |
Correct |
3 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
876 KB |
Output is correct |
2 |
Correct |
4 ms |
876 KB |
Output is correct |
3 |
Correct |
6 ms |
1004 KB |
Output is correct |
4 |
Correct |
7 ms |
1004 KB |
Output is correct |
5 |
Correct |
9 ms |
1132 KB |
Output is correct |
6 |
Correct |
8 ms |
1132 KB |
Output is correct |
7 |
Correct |
8 ms |
1132 KB |
Output is correct |
8 |
Correct |
8 ms |
1132 KB |
Output is correct |
9 |
Correct |
7 ms |
1132 KB |
Output is correct |
10 |
Correct |
7 ms |
1132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
5220 KB |
Output is correct |
2 |
Correct |
54 ms |
5220 KB |
Output is correct |
3 |
Correct |
259 ms |
11876 KB |
Output is correct |
4 |
Correct |
225 ms |
11876 KB |
Output is correct |
5 |
Correct |
582 ms |
23676 KB |
Output is correct |
6 |
Correct |
648 ms |
23676 KB |
Output is correct |
7 |
Correct |
606 ms |
23676 KB |
Output is correct |
8 |
Correct |
598 ms |
24156 KB |
Output is correct |
9 |
Correct |
575 ms |
24156 KB |
Output is correct |
10 |
Correct |
662 ms |
25856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
102 ms |
25856 KB |
Output is correct |
2 |
Correct |
126 ms |
25856 KB |
Output is correct |
3 |
Correct |
374 ms |
25856 KB |
Output is correct |
4 |
Correct |
323 ms |
25856 KB |
Output is correct |
5 |
Execution timed out |
1055 ms |
26732 KB |
Time limit exceeded |
6 |
Halted |
0 ms |
0 KB |
- |