Submission #923173

#TimeUsernameProblemLanguageResultExecution timeMemory
923173DzadzoIdeal city (IOI12_city)C++17
32 / 100
26 ms8144 KiB
#include <bits/stdc++.h> #define ll long long #define pb push_back #define S second #define F first #define pii pair<ll,ll> #define vi vector <ll> #define vvi vector <vi> #define vvvi vector <vvi> #define vp vector <pii> #define vvp vector <vp> #define vb vector <bool> #define vvb vector <vb>; #define INF LLONG_MAX #define MOD 1000000007 #define MAXN 100000 using namespace std; vvi adj(MAXN+1); vi f(MAXN+1),g(MAXN+1); vi a(MAXN+1); ll ans=0; void dfs(ll v,ll p){ g[v]=a[v]; for (ll to:adj[v]){ if (to==p)continue; dfs(to,v); g[v]+=g[to]; f[v]+=f[to]+g[to]; } } void reroot(ll v,ll p){ ans+=f[v]*a[v]; for (ll to :adj[v]){ if (to==p)continue; ll fto=f[to],fv=f[v],gv=g[v],gto=g[to]; g[v]-=g[to]; f[v]-=f[to]+g[to]; f[to]+=g[v]+f[v]; g[to]+=g[v]; reroot(to,v); f[to]=fto; f[v]=fv; g[v]=gv; g[to]=gto; } } int DistanceSum(int N, int X[], int Y[]){ vp arr; map <pii,ll> m; for (ll i=0;i<N;i++)arr.pb({1ll*X[i],1ll*Y[i]}); sort(arr.begin(),arr.end()); ll cnt=0; ll sz=0; for (auto &[x,y]:arr){ if (!m[{x,y-1}]){ a[cnt]=sz; cnt++; sz=1; if (m[{x-1,y}]){ adj[cnt].pb(m[{x-1,y}]); adj[m[{x-1,y}]].pb(cnt); } }else{ sz++; if (m[{x-1,y}] && !m[{x-1,y-1}]){ adj[cnt].pb(m[{x-1,y}]); adj[m[{x-1,y}]].pb(cnt); } } m[{x,y}]=cnt; } a[cnt]=sz; dfs(1,0); reroot(1,0); arr.clear(); m.clear(); cnt=sz=0; adj.assign(MAXN+1,{}); f.assign(MAXN+1,0); g.assign(MAXN+1,0); a.assign(MAXN+1,0); for (ll i=0;i<N;i++)arr.pb({1ll*Y[i],1ll*X[i]}); sort(arr.begin(),arr.end()); for (auto &[y,x]:arr){ if (!m[{x-1,y}]){ a[cnt]=sz; cnt++; sz=1; if (m[{x,y-1}]){ adj[cnt].pb(m[{x,y-1}]); adj[m[{x,y-1}]].pb(cnt); } }else{ sz++; if (m[{x,y-1}] && !m[{x-1,y-1}]){ adj[cnt].pb(m[{x,y-1}]); adj[m[{x,y-1}]].pb(cnt); } } m[{x,y}]=cnt; } a[cnt]=sz; dfs(1,0); reroot(1,0); return ans/2; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...