# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
122230 | Osama_Alkhodairy | Ideal city (IOI12_city) | C++17 | 0 ms | 0 KiB |
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>
//~ #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(ll N, ll *X, ll *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;
}