#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Correct |
2 ms |
460 KB |
Output is correct |
3 |
Correct |
3 ms |
684 KB |
Output is correct |
4 |
Correct |
3 ms |
588 KB |
Output is correct |
5 |
Correct |
4 ms |
844 KB |
Output is correct |
6 |
Correct |
3 ms |
632 KB |
Output is correct |
7 |
Correct |
4 ms |
844 KB |
Output is correct |
8 |
Correct |
3 ms |
716 KB |
Output is correct |
9 |
Correct |
3 ms |
588 KB |
Output is correct |
10 |
Correct |
3 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
3516 KB |
Output is correct |
2 |
Correct |
25 ms |
3524 KB |
Output is correct |
3 |
Correct |
76 ms |
8100 KB |
Output is correct |
4 |
Correct |
72 ms |
8104 KB |
Output is correct |
5 |
Correct |
173 ms |
16004 KB |
Output is correct |
6 |
Correct |
151 ms |
16020 KB |
Output is correct |
7 |
Correct |
164 ms |
16528 KB |
Output is correct |
8 |
Correct |
153 ms |
15836 KB |
Output is correct |
9 |
Correct |
144 ms |
16188 KB |
Output is correct |
10 |
Correct |
155 ms |
23852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
5836 KB |
Output is correct |
2 |
Correct |
34 ms |
4928 KB |
Output is correct |
3 |
Correct |
97 ms |
13928 KB |
Output is correct |
4 |
Correct |
88 ms |
11688 KB |
Output is correct |
5 |
Correct |
185 ms |
27488 KB |
Output is correct |
6 |
Correct |
171 ms |
20240 KB |
Output is correct |
7 |
Correct |
195 ms |
27712 KB |
Output is correct |
8 |
Correct |
173 ms |
20616 KB |
Output is correct |
9 |
Correct |
172 ms |
19396 KB |
Output is correct |
10 |
Correct |
170 ms |
18884 KB |
Output is correct |