Submission #430522

#TimeUsernameProblemLanguageResultExecution timeMemory
430522wiwihoIdeal city (IOI12_city)C++14
100 / 100
195 ms27712 KiB
#include <bits/stdc++.h> #include <bits/extc++.h> #define StarBurstStream ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define iter(a) a.begin(), a.end() #define riter(a) a.rbegin(), a.rend() #define lsort(a) sort(iter(a)) #define gsort(a) sort(riter(a)) #define pb(a) push_back(a) #define eb(a) emplace_back(a) #define pf(a) push_front(a) #define ef(a) emplace_front(a) #define pob pop_back() #define pof pop_front() #define mp(a, b) make_pair(a, b) #define F first #define S second #define mt make_tuple #define gt(t, i) get<i>(t) #define tomax(a, b) ((a) = max((a), (b))) #define tomin(a, b) ((a) = min((a), (b))) #define topos(a) ((a) = (((a) % MOD + MOD) % MOD)) #define uni(a) a.resize(unique(iter(a)) - a.begin()) #define printv(a, b) {bool pvaspace=false; \ for(auto pva : a){ \ if(pvaspace) b << " "; pvaspace=true;\ b << pva;\ }\ b << "\n";} using namespace std; using namespace __gnu_pbds; typedef long long ll; typedef unsigned long long ull; typedef long double ld; using pii = pair<int, int>; using pll = pair<ll, ll>; using pdd = pair<ld, ld>; using tiii = tuple<int, int, int>; const ll MOD = 1000000007; const ll MAX = 2147483647; template<typename A, typename B> ostream& operator<<(ostream& o, pair<A, B> p){ return o << '(' << p.F << ',' << p.S << ')'; } ll ifloor(ll a, ll b){ if(b < 0) a *= -1, b *= -1; if(a < 0) return (a - b + 1) / b; else return a / b; } ll iceil(ll a, ll b){ if(b < 0) a *= -1, b *= -1; if(a > 0) return (a + b - 1) / b; else return a / b; } struct Tree{ vector<set<int>> g; vector<ll> w; ll total = 0; ll sum = 0; explicit Tree(int n){ g.resize(n); w.resize(n); } void addedge(int u, int v){ g[u].insert(v); g[v].insert(u); } void addweight(int v){ w[v]++; total++; } ll dfs(int now, int p){ ll sz = w[now]; for(int i : g[now]){ if(i == p) continue; sz += dfs(i, now); } sum += sz * (total - sz); return sz; } ll calc(){ dfs(0, 0); return sum; } }; int DistanceSum(int n, int *X, int *Y) { map<int, vector<int>> vc, hc; for(int i = 0; i < n; i++){ vc[X[i]].eb(Y[i]); hc[Y[i]].eb(X[i]); } map<pii, int> vid, hid; int vsz = -1, hsz = -1; for(auto& i : vc){ int lst = -87; lsort(i.S); for(int j : i.S){ if(j != lst + 1) vsz++; vid[mp(i.F, j)] = vsz; lst = j; } } for(auto& i : hc){ int lst = -87; lsort(i.S); for(int j : i.S){ if(j != lst + 1) hsz++; hid[mp(j, i.F)] = hsz; lst = j; } } // for(int i = 0; i < n; i++){ // cerr << "city " << X[i] << " " << Y[i] << " " << vid[mp(X[i], Y[i])] << " " << hid[mp(X[i], Y[i])] << "\n"; // } vsz++; hsz++; Tree vt(vsz), ht(hsz); for(auto i : vid){ int x, y, id = i.S; tie(x, y) = i.F; vt.addweight(id); if(vid.find(mp(x + 1, y)) != vid.end()){ vt.addedge(id, vid[mp(x + 1, y)]); // cerr << "v " << id << " " << vid[mp(x + 1, y)] << "\n"; } } for(auto i : hid){ int x, y, id = i.S; tie(x, y) = i.F; ht.addweight(id); if(hid.find(mp(x, y + 1)) != hid.end()){ ht.addedge(id, hid[mp(x, y + 1)]); // cerr << "h " << id << " " << hid[mp(x, y + 1)] << "\n"; } } ll ans = vt.calc() + ht.calc(); return ans % 1000000000; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...