#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
using namespace std;
// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
typedef vector<int> vi;
#define f first
#define s second
// END NO SAD
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<vector<ll>> matrix;
vector<int> edges[100005];
int weight[100005];
ll dfs(int sz, int curr, int par) {
ll ret = 0;
for(int out: edges[curr]) {
if(out == par) continue;
ret += dfs(sz, out, curr);
ret += weight[out] * (ll)(sz - weight[out]);
weight[curr] += weight[out];
}
return ret;
}
ll solve(vector<pii>& v) {
sort(all(v));
int n = sz(v);
for(int i = 0; i < n; i++) edges[i].clear();
memset(weight, 0, sizeof(weight));
vector<array<int, 3>> intervals; // x, yl, yr
for(int i = 0; i < n;) {
int j = i+1;
while(j < n && v[i].f == v[j].f && v[j].s - v[i].s == j - i) j++;
weight[sz(intervals)] = j-i;
intervals.pb({v[i].f, v[i].s, v[j-1].s});
i = j;
}
for(int a = 0; a < sz(intervals);) {
int b = a+1;
while(b < sz(intervals) && intervals[a][0] == intervals[b][0]) b++;
if(b == sz(intervals)) break;
// [a, b)
int c = b+1;
while(c < sz(intervals) && intervals[b][0] == intervals[c][0]) c++;
int lhs = a;
int rhs = b;
while(lhs < b && rhs < c) {
if(intervals[lhs][2] <= intervals[rhs][2]) {
if(intervals[rhs][1] <= intervals[lhs][2]) {
edges[lhs].pb(rhs);
edges[rhs].pb(lhs);
}
lhs++;
}
else {
if(intervals[lhs][1] <= intervals[rhs][2]) {
edges[lhs].pb(rhs);
edges[rhs].pb(lhs);
}
rhs++;
}
}
a = b;
}
return dfs(n, 0, -1);
}
int DistanceSum(int n, int *X, int *Y) {
vector<pii> v(n);
for(int i = 0; i < n; i++) v[i] = {X[i], Y[i]};
ll ret = solve(v);
for(auto& x: v) swap(x.f, x.s);
ret += solve(v);
return ret%1000000000;
}
/*
void solve() {
int n;
cin >> n;
vector<pii> v(n);
for(auto& x: v) cin >> x.f >> x.s;
ll ret = solve(v);
for(auto& x: v) swap(x.f, x.s);
ret += solve(v);
cout << ret%1000000000 << "\n";
}
// what would chika do
// are there edge cases (N=1?)
// are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// integer overflow?
// DS reset properly between test cases
// are you doing geometry in floating points
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
solve();
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3020 KB |
Output is correct |
2 |
Correct |
2 ms |
3020 KB |
Output is correct |
3 |
Correct |
2 ms |
3020 KB |
Output is correct |
4 |
Correct |
2 ms |
3040 KB |
Output is correct |
5 |
Correct |
2 ms |
3020 KB |
Output is correct |
6 |
Correct |
2 ms |
3020 KB |
Output is correct |
7 |
Correct |
3 ms |
3032 KB |
Output is correct |
8 |
Correct |
2 ms |
3020 KB |
Output is correct |
9 |
Correct |
2 ms |
3020 KB |
Output is correct |
10 |
Correct |
2 ms |
3020 KB |
Output is correct |
11 |
Correct |
2 ms |
3020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
3020 KB |
Output is correct |
2 |
Correct |
2 ms |
3020 KB |
Output is correct |
3 |
Correct |
3 ms |
3020 KB |
Output is correct |
4 |
Correct |
3 ms |
3020 KB |
Output is correct |
5 |
Correct |
4 ms |
3020 KB |
Output is correct |
6 |
Correct |
3 ms |
3020 KB |
Output is correct |
7 |
Correct |
3 ms |
3148 KB |
Output is correct |
8 |
Correct |
3 ms |
3020 KB |
Output is correct |
9 |
Correct |
4 ms |
3020 KB |
Output is correct |
10 |
Correct |
3 ms |
3020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
3520 KB |
Output is correct |
2 |
Correct |
10 ms |
3660 KB |
Output is correct |
3 |
Correct |
21 ms |
4252 KB |
Output is correct |
4 |
Correct |
21 ms |
4240 KB |
Output is correct |
5 |
Correct |
43 ms |
5412 KB |
Output is correct |
6 |
Correct |
42 ms |
5456 KB |
Output is correct |
7 |
Correct |
42 ms |
5672 KB |
Output is correct |
8 |
Correct |
43 ms |
5420 KB |
Output is correct |
9 |
Correct |
42 ms |
5764 KB |
Output is correct |
10 |
Correct |
45 ms |
9116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
4044 KB |
Output is correct |
2 |
Correct |
11 ms |
3908 KB |
Output is correct |
3 |
Correct |
27 ms |
5524 KB |
Output is correct |
4 |
Correct |
25 ms |
5132 KB |
Output is correct |
5 |
Correct |
53 ms |
7928 KB |
Output is correct |
6 |
Correct |
57 ms |
6476 KB |
Output is correct |
7 |
Correct |
59 ms |
8332 KB |
Output is correct |
8 |
Correct |
46 ms |
6416 KB |
Output is correct |
9 |
Correct |
47 ms |
6204 KB |
Output is correct |
10 |
Correct |
45 ms |
6064 KB |
Output is correct |