제출 #122231

#제출 시각아이디문제언어결과실행 시간메모리
122231Osama_Alkhodairy이상적인 도시 (IOI12_city)C++17
32 / 100
39 ms14448 KiB
#include <bits/stdc++.h> //~ #include "grader.cpp" using namespace std; #define ll long long const ll N = 100001; ll n, wr[N], wc[N], mod = 1e9 + 7; set <ll> r[N], c[N]; vector <pair <ll, ll> > a; map <pair <ll, ll>, ll> mpr, mpc; ll cntr, cntc, ans; void addr(ll x, ll y){ r[x].insert(y); r[y].insert(x); } void addc(ll x, ll y){ c[x].insert(y); c[y].insert(x); } void setr(ll row, ll col1, ll col2){ ll id = cntr++; wr[id] = col2 - col1 + 1; for(ll col = col1 ; col <= col2 ; col++){ if(mpr.find(make_pair(row - 1, col)) != mpr.end()){ addr(id, mpr[make_pair(row - 1, col)]); } mpr[make_pair(row, col)] = id; } } void setc(ll col, ll row1, ll row2){ ll id = cntc++; wc[id] = row2 - row1 + 1; for(ll row = row1 ; row <= row2 ; row++){ if(mpc.find(make_pair(row, col - 1)) != mpc.end()){ addc(id, mpc[make_pair(row, col - 1)]); } mpc[make_pair(row, col)] = id; } } void gor(ll l, ll r){ for(ll i = l ; i < r ; i++){ ll j = i; while(j + 1 < r && a[j + 1].second == a[j].second + 1) j++; setr(a[i].first, a[i].second, a[j].second); i = j; } } void goc(ll l, ll r){ for(ll i = l ; i < r ; i++){ ll j = i; while(j + 1 < r && a[j + 1].first == a[j].first + 1) j++; setc(a[i].second, a[i].first, a[j].first); i = j; } } void dfsr(ll node, ll pnode){ for(auto &i : r[node]){ if(i == pnode) continue; dfsr(i, node); ans = (ans + 1LL * wr[i] * (n - wr[i])) % mod; wr[node] += wr[i]; } } void dfsc(ll node, ll pnode){ for(auto &i : c[node]){ if(i == pnode) continue; dfsc(i, node); ans = (ans + 1LL * wc[i] * (n - wc[i])) % mod; wc[node] += wc[i]; } } int DistanceSum(int N, int *X, int *Y) { n = N; for(ll i = 0 ; i < n ; i++){ a.push_back(make_pair(X[i], Y[i])); } sort(a.begin(), a.end()); for(ll i = 0 ; i < n ; i++){ ll j = i; while(j < n && a[j].first == a[i].first) j++; gor(i, j); i = j - 1; } sort(a.begin(), a.end(), [&](pair <ll, ll> l, pair <ll, ll> r){ return make_pair(l.second, l.first) < make_pair(r.second, r.first); }); for(ll i = 0 ; i < n ; i++){ ll j = i; while(j < n && a[j].second == a[j].second) j++; goc(i, j); i = j - 1; } dfsr(0, -1); dfsc(0, -1); return ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...