This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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) {
| ~~^~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |