# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
163141 | mhy908 | Ideal city (IOI12_city) | C++14 | 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>
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const LL llinf=987654321987654321;
const int inf=987654321;
int a, tx, ty, tmp, t, x[100010], y[100010];
LL sum, mod=1e9, sz[100010];
vector<int> grid[100002], edge[100010];
struct data{
int num, s, e;
};
vector<data> T[100010];
void dfs(int r, int par){
for(int k=0; k<edge[r].size(); k++){
if(par==edge[r][k])continue;
dfs(edge[r][k], r);
sz[r]+=sz[edge[r][k]];
}
sum+=sz[r]*(LL)(a-sz[r]);
}
void func()
{
t=0;
for(i=0; i<=a; i++)grid[i].clear(), T[i].clear(), edge[i].clear();
for(i=1; i<=a; i++)grid[x[i]].push_back(y[i]);
for(i=0; grid[i].size(); i++)sort(grid[i].begin(), grid[i].end());
for(i=0; grid[i].size(); i++){
tmp=grid[i][0];
for(j=0; j<grid[i].size(); j++){
if(j==grid[i].size()-1||grid[i][j]+1!=grid[i][j+1]){
T[i].push_back({t++, tmp, grid[i][j]});
sz[t-1]=grid[i][j]-tmp+1;
if(j!=grid[i].size()-1)tmp=grid[i][j+1];
}
}
}
for(i=1; T[i].size(); i++){
int p=0, q=0;
while(p<T[i-1].size()&&q<T[i].size()){
if(!(T[i-1][p].e<T[i][q].s||T[i][q].e<T[i-1][p].s)){
edge[T[i-1][p].num].push_back(T[i][q].num);
edge[T[i][q].num].push_back(T[i-1][p].num);
}
if(T[i-1][p].e<T[i][q].e)p++;
else q++;
}
}
dfs(0, -1);
}
int DistanceSum(int N, int *X, int *Y)
{
a=N;
tx=inf;
ty=inf;
for(i=1; i<=a; i++)x[i]=X[i-1], y[i]=Y[i-1];
for(i=1; i<=a; i++)tx=min(tx,x[i]), ty=min(ty,y[i]);
for(i=1; i<=a; i++)x[i]-=tx, y[i]-=ty;
func();
for(i=1; i<=a; i++)t=x[i], x[i]=y[i], y[i]=t;
func();
return sum%mod;
}