# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
122227 | 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;
const int N = 100001;
int n, wr[N], wc[N], mod = 1e9 + 7;
set <int> r[N], c[N];
vector <pair <int, int> > a;
map <pair <int, int>, int> mpr, mpc;
int cntr, cntc, ans;
void addr(int x, int y){
r[x].insert(y);
r[y].insert(x);
}
void addc(int x, int y){
c[x].insert(y);
c[y].insert(x);
}
void setr(int row, int col1, int col2){
int id = cntr++;
wr[id] = col2 - col1 + 1;
for(int 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(int col, int row1, int row2){
int id = cntc++;
wc[id] = row2 - row1 + 1;
for(int 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(int l, int r){
for(int i = l ; i < r ; i++){
int 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(int l, int r){
for(int i = l ; i < r ; i++){
int 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(int node, int 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(int node, int 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(int i = 0 ; i < n ; i++){
a.push_back(make_pair(X[i], Y[i]));
}
sort(a.begin(), a.end());
for(int i = 0 ; i < n ; i++){
int j = i;
while(j < n && a[j].first == a[i].first) j++;
gor(i, j);
i = j - 1;
}
sort(a.begin(), a.end(), [&](pair <int, int> l, pair <int, int> r){
return make_pair(l.second, l.first) < make_pair(r.second, r.first);
});
for(int i = 0 ; i < n ; i++){
int 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;
}