이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
#define ff first
#define ss second
const int mod = 1e9;
int N;
pii P[100005];
vector<pii> vec[100005];
vector<int> edge[100005];
int sz[100005];
int dfs(int x, int p) {
int ret = 0;
for (auto i : edge[x]) {
if (i == p) continue;
ret += dfs(i, x);
ret %= mod;
sz[x] += sz[i];
}
return (ret + 1ll * (N - sz[x]) * sz[x] % mod) % mod;
}
int solve() {
sort(P, P + N);
int x0 = P[0].ff;
for (int i = 0, j = 0; i < N; i = ++j) {
while (j + 1 < N && P[j + 1].ff == P[i].ff && P[j + 1].ss == P[j].ss + 1) j++;
vec[P[i].ff - x0].emplace_back(P[i].ss, P[j].ss);
}
for (int i = 0; i <= P[N - 1].ff - x0; ++i) sort(vec[i].begin(), vec[i].end());
int cnt = 0;
for (int i = 0; i < P[N - 1].ff - x0; ++i) {
for (int j = 0, k = 0; j < vec[i].size(); ++j) {
sz[cnt + j] = vec[i][j].ss - vec[i][j].ff + 1;
while (k + 1 < vec[i + 1].size() && vec[i + 1][k + 1].ff <= vec[i][j].ss) {
if (vec[i][j].ff <= vec[i + 1][k].ss) {
edge[cnt + j].push_back(cnt + vec[i].size() + k);
edge[cnt + vec[i].size() + k].push_back(cnt + j);
}
k++;
}
if (vec[i + 1][k].ff <= vec[i][j].ss && vec[i][j].ff <= vec[i + 1][k].ss) {
edge[cnt + j].push_back(cnt + vec[i].size() + k);
edge[cnt + vec[i].size() + k].push_back(cnt + j);
}
}
cnt += vec[i].size();
}
for (auto i : vec[P[N - 1].ff - x0]) sz[cnt++] = i.ss - i.ff + 1;
for (int i = 0; i <= P[N - 1].ff - x0; ++i) vec[i].clear();
int t = dfs(0, 0);
for (int i = 0; i < cnt; ++i) edge[i].clear();
return t;
}
int DistanceSum(int NN, int *X, int *Y) {
N = NN;
for (int i = 0; i < N; ++i) P[i] = {X[i], Y[i]};
int ans = solve();
for (int i = 0; i < N; ++i) swap(P[i].ff, P[i].ss);
return ans = (ans + solve()) % mod;
}
컴파일 시 표준 에러 (stderr) 메시지
city.cpp: In function 'int solve()':
city.cpp:36:28: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int j = 0, k = 0; j < vec[i].size(); ++j) {
~~^~~~~~~~~~~~~~~
city.cpp:38:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (k + 1 < vec[i + 1].size() && vec[i + 1][k + 1].ff <= vec[i][j].ss) {
~~~~~~^~~~~~~~~~~~~~~~~~~
# | 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... |