이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define rep(i , j , k) for (int i = j; i < (int)k; i++)
#define pb push_back
#define all(x) x.begin(),x.end()
typedef pair<int , int> pii;
typedef long long ll;
inline int bn(vector<pii> &vec, pii p) {
int res = lower_bound(all(vec) , p) - vec.begin();
if (res == vec.size() || vec[res] != p) return -1;
return res;
}
struct dsu {
vector<int> par , sz;
dsu() {}
dsu(int N) {
par.resize(N);
iota(all(par) , 0);
sz.resize(N , 1);
}
int getPar(int v) { return par[v] == v ? v : par[v] = getPar(par[v]); }
void merge(int v, int u) {
v = getPar(v), u = getPar(u);
if (v == u) return;
par[v] = u;
sz[u] += sz[v];
}
} ds;
ll MOD = 1e9;
vector<pii> vec, L, R;
map<int , int> mp;
map<int , set<int>> adj;
bitset<2 * 1000 * 1000 + 10> mark;
ll sz[2 * 1000 * 1000 + 10];
int dfs1(int v, int par = -1) {
sz[v] = mp[v];
for (auto e : adj[v])
if (e ^ par)
sz[v] += dfs1(e , v);
return sz[v] %= MOD;
}
int dfs2(int v, int par = -1, ll all = -1) {
mark[v] = true;
if (all == -1) all = sz[v];
ll res = (all - sz[v]) * sz[v];
res %= MOD;
for (auto e : adj[v])
if (e ^ par)
res += dfs2(e , v, all);
return res % MOD;
}
int solve(int N, int *X, int *Y) {
vec = vector<pii>(N);
L = vector<pii>(N);
R = vector<pii>(N);
mark.reset();
rep(i , 0 , N)
vec[i] = {X[i] , Y[i]};
sort(all(vec));
{
ds = dsu(N);
int last = 0;
auto relax = [&last](int now) {
rep(j , last , now) {
int h = bn(vec , {vec[j].first - 1 , vec[j].second});
int v = bn(vec , {vec[j].first , vec[j].second - 1});
if (h != -1)
ds.merge(j , h);
if (v != -1)
ds.merge(j , v);
}
rep(j , last , now)
L[j] = {ds.sz[ds.getPar(j)] , ds.getPar(j)};
last = now;
};
rep(i , 0 , N)
if (vec[i].first != vec[last].first) relax(i);
relax(N);
}
{
ds = dsu(N);
int last = N - 1;
auto relax = [&last](int now) {
for (int j = last; j > now; j--) {
int h = bn(vec , {vec[j].first + 1 , vec[j].second});
int v = bn(vec , {vec[j].first , vec[j].second + 1});
if (~h)
ds.merge(j , h);
if (~v)
ds.merge(j , v);
}
for (int j = last; j > now; j--)
R[j] = {ds.sz[ds.getPar(j)] , ds.getPar(j)};
last = now;
};
for (int i = N - 1; ~i; i--)
if (vec[i].first != vec[last].first) relax(i);
relax(-1);
}
ll res = 0;
rep(i , 1 , N)
if (vec[i].first != vec[i - 1].first) {
mp.clear();
adj.clear();
int l = i - 1;
while (l && vec[l - 1].first == vec[l].first) l--;
rep (j , l , i) {
mp[L[j].second] = L[j].first;
int h = bn(vec, {vec[j].first + 1 , vec[j].second});
if (~h) {
mp[R[h].second + N] = R[h].first;
adj[L[j].second].insert(R[h].second + N);
adj[R[h].second + N].insert(L[j].second);
}
}
ll local = 0;
for (auto e : mp) {
int v = e.first;
if (!mark[v]) {
dfs1(v);
local += dfs2(v);
}
}
for (auto e : mp) {
int v = e.first;
mark[v] = false;
}
res = (res + local) % MOD;
}
return res % MOD;
}
int DistanceSum(int N ,int *X , int *Y) {
return (solve(N , X , Y) + solve(N , Y , X)) % MOD;
}
컴파일 시 표준 에러 (stderr) 메시지
city.cpp: In function 'int bn(std::vector<std::pair<int, int> >&, pii)':
city.cpp:14:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (res == vec.size() || vec[res] != p) return -1;
~~~~^~~~~~~~~~~~~
# | 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... |