# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
735633 | myrcella | 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.
//by szh
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000000
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}
const int maxn = 1e5+10;
pii city[maxn];
int ans = 0;
int n;
vector <int> edge[maxn];
int sz[maxn];
map <pii,int> id;
int vis[maxn];
int dis[maxn][maxn];
void dfs(int u) {
vis[u] = 1;
for (int v:edge[u]) {
if (vis[v]==1) continue;
dfs(v);
sz[u] += sz[v];
}
// debug(sz[u]);
inc(ans,1ll*sz[u]*(n-sz[u])%mod);
}
void solve() {
memset(vis,0,sizeof(vis));
rep(i,0,maxn) while (!edge[i].empty()) edge[i].pop_back();
id.clear();
sort(city,city+n);
int tot = 0,cnt=1;
id[city[0]] = 0;
rep(i,1,n) {
if (city[i].fi == city[i-1].fi and city[i].se-1 == city[i-1].se) cnt++;
else sz[tot++] = cnt,cnt = 1;
id[city[i]] = tot;
if (id.find(MP(city[i].fi-1,city[i].se))!=id.end()) {
int tmp = id[MP(city[i].fi-1,city[i].se)];
edge[tmp].pb(tot);
edge[tot].pb(tmp);
}
}
sz[tot++] = cnt;
dfs(0);
}
int DistanceSum(int N, int *X, int *Y) {
n = N;
rep(i,0,n) city[i] = {X[i],Y[i]};
solve();
// debug(ans);
rep(i,0,n) swap(city[i].fi,city[i].se);
solve();
return ans;
}