#include <bits/stdc++.h>
using namespace std;
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__);
#define ll long long
const int mod = 1e9;
int add(int a, int b) {
a += b;
if(a > mod) {
a -= mod;
}
return a;
}
int mul(int a, int b) {
a = (ll)a * b % mod;
return a;
}
int sub(int a, int b) {
a -= b;
if(a < 0) {
a += mod;
}
return a;
}
vector<pair<int, int>> dir = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
vector<vector<int>> adj;
vector<int> sz;
vector<bool> vis;
int n;
int tot = 0;
int get_sz(int node) {
vis[node] = 1;
for(int x : adj[node]) {
if(vis[x]) continue;
sz[node] = add(sz[node], get_sz(x));
}
return sz[node];
}
void dfs(int node) {
vis[node] = 1;
for(int x : adj[node]) {
if(vis[x]) continue;
// dbg(x, sz[x]);
tot = add(tot, mul(sz[x], (n - sz[x])));
dfs(x);
}
}
int cal(int* X, int* Y) {
tot = 0;
map<int, vector<int>> mp;
adj.clear();
adj.resize(n);
sz.clear();
sz.resize(n);
vis.clear();
vis.resize(n);
map<pair<int, int>, int> cc;
for(int i = 0; i < n; ++i) {
mp[X[i]].push_back(Y[i]);
}
int nbr = 0;
for(auto[id, v] : mp) {
sort(v.begin(), v.end());
int c = 1;
cc[{id, v[0]}] = nbr;
for(int i = 1; i < v.size(); ++i) {
if(v[i - 1] + 1 == v[i]) { // you can expend the edge of the tree
c++;
} else {
sz[nbr++] = c;
c = 1;
}
cc[{id, v[i]}] = nbr; // edges having the same cc
}
sz[nbr++] = c; c = 0;
for(int i = 0; i < v.size(); ++i) {
auto it = cc.find({id - 1, v[i]}); // link those having adjacent y
if(it == cc.end()) continue;
int ff = it->second;
int ss = cc[{id, v[i]}];
adj[ff].push_back(ss);
adj[ss].push_back(ff);
}
}
get_sz(0);
vis.assign(n, 0);
dfs(0);
return tot;
}
int DistanceSum(int n_, int *X, int *Y) {
n = n_;
int cal_x = cal(X, Y);
int cal_y = cal(Y, X);
int ans = add(cal_x, cal_y);
return ans;
}
// int main() {
// /* Set input and output buffering */
// // char *inbuf, *outbuf;
// // inbuf = (char*) malloc(inbuf_len * sizeof(char));
// // outbuf = (char*) malloc(outbuf_len * sizeof(char));
// // tmp = setvbuf(stdin, inbuf, _IOFBF, inbuf_len);
// // assert(tmp == 0);
// // tmp = setvbuf(stdout, outbuf, _IOFBF, outbuf_len);
// // assert(tmp == 0);
// int N, i;
// cin >> N;
// int *sq_x, *sq_y;
// sq_x = (int*) malloc(N * sizeof(int));
// sq_y = (int*) malloc(N * sizeof(int));
// for (i = 0; i < N; i++) {
// cin >> sq_x[i] >> sq_y[i];
// }
// int ds = DistanceSum(N, sq_x, sq_y);
// cout << ds;
// return 0;
// }
Compilation message
city.cpp: In function 'int cal(int*, int*)':
city.cpp:74:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for(int i = 1; i < v.size(); ++i) {
| ~~^~~~~~~~~~
city.cpp:84:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(int i = 0; i < v.size(); ++i) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
304 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
308 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
304 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
436 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
440 KB |
Output is correct |
5 |
Correct |
2 ms |
572 KB |
Output is correct |
6 |
Correct |
2 ms |
564 KB |
Output is correct |
7 |
Correct |
2 ms |
576 KB |
Output is correct |
8 |
Correct |
3 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
3 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
2836 KB |
Output is correct |
2 |
Correct |
15 ms |
2900 KB |
Output is correct |
3 |
Correct |
38 ms |
6484 KB |
Output is correct |
4 |
Correct |
41 ms |
6468 KB |
Output is correct |
5 |
Correct |
84 ms |
12952 KB |
Output is correct |
6 |
Correct |
82 ms |
12848 KB |
Output is correct |
7 |
Correct |
83 ms |
12876 KB |
Output is correct |
8 |
Correct |
82 ms |
12876 KB |
Output is correct |
9 |
Correct |
80 ms |
12620 KB |
Output is correct |
10 |
Correct |
103 ms |
17116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
2968 KB |
Output is correct |
2 |
Correct |
19 ms |
2956 KB |
Output is correct |
3 |
Correct |
42 ms |
6760 KB |
Output is correct |
4 |
Correct |
41 ms |
6640 KB |
Output is correct |
5 |
Correct |
99 ms |
13040 KB |
Output is correct |
6 |
Correct |
100 ms |
12976 KB |
Output is correct |
7 |
Correct |
87 ms |
13180 KB |
Output is correct |
8 |
Correct |
86 ms |
12876 KB |
Output is correct |
9 |
Correct |
84 ms |
12652 KB |
Output is correct |
10 |
Correct |
87 ms |
12788 KB |
Output is correct |