# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
61843 | zetapi | 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>
using namespace std;
#define pb push_back
#define mp make_pair
#define ll long long
#define itr ::iterator
typedef pair<ll,ll> pii;
const ll MAX=1e6;
const ll mod=1e9;
vector<pii> vec;
set<ll> adj[MAX];
ll res,value[MAX],sum[MAX];
void dfs(int node,int par)
{
sum[node]=value[node];
for(auto A:adj[node])
{
if(A==par)
continue;
dfs(A,node);
sum[node]+=sum[A];
sum[node]%=mod;
}
return ;
}
void dfs_(int node,int par)
{
for(auto A:adj[node])
{
if(A==par)
continue;
dfs_(A,node);
res+=(sum[A]*(sum[1]-sum[A]+mod)%mod)%mod;
res%=mod;
}
return ;
}
void cal(int N,int *X,int *Y)
{
for(int A=1;A<=N;A++)
{
sum[A]=0;
value[A]=0;
adj[A].clear();
}
int ptr=0;
vector<pii> vec;
map<pii,int> mark;
for(int A=0;A<N;A++)
vec.pb(mp(X[A],Y[A]));
vec.pb(mp(0,0));
sort(vec.begin(),vec.end());
for(int A=1;A<vec.size();A++)
{
if(vec[A].first==vec[A-1].first and vec[A].second==vec[A-1].second+1)
{
value[ptr]++;
mark[mp(vec[A].first,vec[A].second)]=ptr;
}
else
{
value[++ptr]++;
mark[mp(vec[A].first,vec[A].second)]=ptr;
}
//cout<<vec[A].first<<" "<<vec[A].second<<" "<<mark[vec[A]]<<"\n";
}
for(int A=1;A<vec.size();A++)
{
if(mark[mp(vec[A].first-1,vec[A].second)])
{
adj[mark[vec[A]]].insert(mark[mp(vec[A].first-1,vec[A].second)]);
adj[mark[mp(vec[A].first-1,vec[A].second)]].insert(mark[vec[A]]);
// cout<<mark[vec[A]]<<" "<<mark[mp(vec[A].first-1,vec[A].second)]<<"lol \n";
}
}
dfs(1,0);
dfs_(1,0);
return ;
}
int DistanceSum(int N, int *X, int *Y)
{
cal(N,X,Y);
for(int A=0;A<N;A++)
swap(X[A],Y[A]);
cal(N,X,Y);
return res;
}
signed main()
{
ios_base::sync_with_stdio(false);
int X[]={2,2,3,3,4,4,4,4,5,5,5};
int Y[]={5,6,3,6,3,4,5,6,3,4,6};
cout<<DistanceSum(11,X,Y);
return 0;
}