이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
//~ #include "grader.cpp"
using namespace std;
#define ll long long
const ll N = 100001;
ll n, wr[N], wc[N], mod = 1e9 + 7;
set <ll> r[N], c[N];
vector <pair <ll, ll> > a;
map <pair <ll, ll>, ll> mpr, mpc;
ll cntr, cntc, ans;
void addr(ll x, ll y){
r[x].insert(y);
r[y].insert(x);
}
void addc(ll x, ll y){
c[x].insert(y);
c[y].insert(x);
}
void setr(ll row, ll col1, ll col2){
ll id = cntr++;
wr[id] = col2 - col1 + 1;
for(ll col = col1 ; col <= col2 ; col++){
if(mpr.find(make_pair(row - 1, col)) != mpr.end()){
addr(id, mpr[make_pair(row - 1, col)]);
}
mpr[make_pair(row, col)] = id;
}
}
void setc(ll col, ll row1, ll row2){
ll id = cntc++;
wc[id] = row2 - row1 + 1;
for(ll row = row1 ; row <= row2 ; row++){
if(mpc.find(make_pair(row, col - 1)) != mpc.end()){
addc(id, mpc[make_pair(row, col - 1)]);
}
mpc[make_pair(row, col)] = id;
}
}
void gor(ll l, ll r){
for(ll i = l ; i < r ; i++){
ll j = i;
while(j + 1 < r && a[j + 1].second == a[j].second + 1) j++;
setr(a[i].first, a[i].second, a[j].second);
i = j;
}
}
void goc(ll l, ll r){
for(ll i = l ; i < r ; i++){
ll j = i;
while(j + 1 < r && a[j + 1].first == a[j].first + 1) j++;
setc(a[i].second, a[i].first, a[j].first);
i = j;
}
}
void dfsr(ll node, ll pnode){
for(auto &i : r[node]){
if(i == pnode) continue;
dfsr(i, node);
ans = (ans + 1LL * wr[i] * (n - wr[i])) % mod;
wr[node] += wr[i];
}
}
void dfsc(ll node, ll pnode){
for(auto &i : c[node]){
if(i == pnode) continue;
dfsc(i, node);
ans = (ans + 1LL * wc[i] * (n - wc[i])) % mod;
wc[node] += wc[i];
}
}
int DistanceSum(int N, int *X, int *Y) {
n = N;
for(ll i = 0 ; i < n ; i++){
a.push_back(make_pair(X[i], Y[i]));
}
sort(a.begin(), a.end());
for(ll i = 0 ; i < n ; i++){
ll j = i;
while(j < n && a[j].first == a[i].first) j++;
gor(i, j);
i = j - 1;
}
sort(a.begin(), a.end(), [&](pair <ll, ll> l, pair <ll, ll> r){
return make_pair(l.second, l.first) < make_pair(r.second, r.first);
});
for(ll i = 0 ; i < n ; i++){
ll j = i;
while(j < n && a[j].second == a[j].second) j++;
goc(i, j);
i = j - 1;
}
dfsr(0, -1);
dfsc(0, -1);
return ans;
}
# | 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... |