#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
//#pragma GCC optimize("O3")
//#pragma GCC target("avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
//#pragma GCC target("avx,avx2,sse,sse2,sse3,sse4,popcnt")
using namespace std;
using namespace __gnu_pbds;
#define int long long
#define float long double
#define elif else if
#define endl "\n"
#define mod 1000000007
#define pi acos(-1)
#define eps 0.000000001
#define inf 1000'000'000'000'000'000LL
#define FIXED(a) cout << fixed << setprecision(a)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define time_init auto start = std::chrono::high_resolution_clock::now()
#define time_report \
auto end = std::chrono::high_resolution_clock::now(); \
std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms" << endl
#define debug(x) \
{ cerr << #x << " = " << x << endl; }
#define len(x) (int) x.size()
#define sqr(x) ((x) * (x))
#define cube(x) ((x) * (x) * (x))
#define bit(x, i) (((x) >> (i)) & 1)
#define set_bit(x, i) ((x) | (1LL << (i)))
#define clear_bit(x, i) ((x) & (~(1LL << (i))))
#define toggle_bit(x, i) ((x) ^ (1LL << (i)))
#define low_bit(x) ((x) & (-(x)))
#define count_bit(x) __builtin_popcountll(x)
#define srt(x) sort(all(x))
#define rsrt(x) sort(rall(x))
#define mp make_pair
#define maxel(x) (*max_element(all(x)))
#define minel(x) (*min_element(all(x)))
#define maxelpos(x) (max_element(all(x)) - x.begin())
#define minelpos(x) (min_element(all(x)) - x.begin())
#define sum(x) (accumulate(all(x), 0LL))
#define product(x) (accumulate(all(x), 1LL, multiplies<int>()))
#define gcd __gcd
#define lcm(a, b) ((a) / gcd(a, b) * (b))
#define rev(x) (reverse(all(x)))
#define shift_left(x, k) (rotate(x.begin(), x.begin() + k, x.end()))
#define shift_right(x, k) (rotate(x.rbegin(), x.rbegin() + k, x.rend()))
#define is_sorted(x) (is_sorted_until(all(x)) == x.end())
#define is_even(x) (((x) &1) == 0)
#define is_odd(x) (((x) &1) == 1)
#define pow2(x) (1LL << (x))
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
size_t operator()(pair<int, int> Y) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(Y.first + FIXED_RANDOM) ^ (splitmix64(Y.second + FIXED_RANDOM) >> 1);
}
};
template<typename T>
using min_heap = priority_queue<T, vector<T>, greater<T>>;
template<typename T>
using max_heap = priority_queue<T, vector<T>, less<T>>;
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T>
using matrix = vector<vector<T>>;
template<typename T>
using graph = vector<vector<T>>;
using hashmap = gp_hash_table<int, int, custom_hash>;
template<typename T>
using hashset = gp_hash_table<T, null_type, custom_hash>;
template<typename T>
vector<T> vect(int n, T val) {
return vector<T>(n, val);
}
template<typename T>
vector<vector<T>> vect(int n, int m, T val) {
return vector<vector<T>>(n, vector<T>(m, val));
}
template<typename T>
vector<vector<vector<T>>> vect(int n, int m, int k, T val) {
return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, val)));
}
template<typename T>
vector<vector<vector<vector<T>>>> vect(int n, int m, int k, int l, T val) {
return vector<vector<vector<vector<T>>>>(n, vector<vector<vector<T>>>(m, vector<vector<T>>(k, vector<T>(l, val))));
}
template<typename T>
matrix<T> new_matrix(int n, int m, T val) {
return matrix<T>(n, vector<T>(m, val));
}
template<typename T>
graph<T> new_graph(int n) {
return graph<T>(n);
}
template<class T, class S>
inline bool chmax(T &a, const S &b) {
return (a < b ? a = b, 1 : 0);
}
template<class T, class S>
inline bool chmin(T &a, const S &b) {
return (a > b ? a = b, 1 : 0);
}
using i8 = int8_t;
using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;
using i128 = __int128_t;
using u8 = uint8_t;
using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;
using u128 = __uint128_t;
template<typename T>
using vec = vector<T>;
using pII = pair<int, int>;
template<typename T>
using enumerated = pair<T, int>;
int solve(int n, vec<pII> points) {
auto do_it = [&]() { ;
vec<int> a;
set<pII> ps(all(points));
set<pII> points_set(all(points));
map<pII, int> id;
int cur = 0;
while (!ps.empty()) {
auto [x, y] = *ps.begin();
int k = 0;
int nx = x + 1;
int ny = y;
while (ps.count({x, y})) {
id[{x, y}] = cur;
ps.erase({x, y});
k++;
x--;
}
x = nx;
y = ny;
while (ps.count({x, y})) {
id[{x, y}] = cur;
ps.erase({x, y});
k++;
x++;
}
cur++;
a.push_back(k);
}
set<pII> edges;
graph<int> g(cur);
vec<pII> moves = {{0, 1},
{0, -1}};
for (auto [x, y]: points) {
for (auto [dx, dy]: moves) {
int nx = x + dx;
int ny = y + dy;
if (!points_set.count({nx, ny})) continue;
int id_ = id[{nx, ny}];
edges.insert({id[{x, y}], id_});
edges.insert({id_, id[{x, y}]});
}
}
for (auto [v, u]: edges) {
g[v].push_back(u);
}
vec<int> sizes(cur);
int ans = 0;
function<void(int, int)> dfs = [&](int v, int p) {
sizes[v] = a[v];
for (auto u: g[v]) {
if (u == p) continue;
dfs(u, v);
int S = sizes[u];
int tS = n - S;
ans += S * tS;
sizes[v] += sizes[u];
}
};
dfs(0, -1);
return ans;
};
int ans = do_it();
for (int i = 0; i < n; i++) {
swap(points[i].first, points[i].second);
}
ans += do_it();
return ans;
}
i32 DistanceSum(i32 n, i32 *Xa_, i32 *Ya_) {
vec<pII> points(n);
for (int i = 0; i < n; i++) {
points[i] = {Xa_[i], Ya_[i]};
}
return solve(n, points) % 1000000000;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
440 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
600 KB |
Output is correct |
2 |
Correct |
3 ms |
444 KB |
Output is correct |
3 |
Correct |
4 ms |
828 KB |
Output is correct |
4 |
Correct |
5 ms |
788 KB |
Output is correct |
5 |
Correct |
7 ms |
892 KB |
Output is correct |
6 |
Correct |
5 ms |
860 KB |
Output is correct |
7 |
Correct |
5 ms |
860 KB |
Output is correct |
8 |
Correct |
5 ms |
860 KB |
Output is correct |
9 |
Correct |
5 ms |
604 KB |
Output is correct |
10 |
Correct |
5 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
71 ms |
3892 KB |
Output is correct |
2 |
Correct |
67 ms |
3920 KB |
Output is correct |
3 |
Correct |
218 ms |
9044 KB |
Output is correct |
4 |
Correct |
205 ms |
9300 KB |
Output is correct |
5 |
Correct |
506 ms |
17748 KB |
Output is correct |
6 |
Correct |
462 ms |
18000 KB |
Output is correct |
7 |
Correct |
468 ms |
18332 KB |
Output is correct |
8 |
Correct |
500 ms |
17748 KB |
Output is correct |
9 |
Correct |
462 ms |
18564 KB |
Output is correct |
10 |
Correct |
442 ms |
27228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
5528 KB |
Output is correct |
2 |
Correct |
79 ms |
4948 KB |
Output is correct |
3 |
Correct |
206 ms |
13008 KB |
Output is correct |
4 |
Correct |
232 ms |
11604 KB |
Output is correct |
5 |
Correct |
496 ms |
25808 KB |
Output is correct |
6 |
Correct |
476 ms |
20844 KB |
Output is correct |
7 |
Correct |
526 ms |
26240 KB |
Output is correct |
8 |
Correct |
515 ms |
21076 KB |
Output is correct |
9 |
Correct |
475 ms |
20052 KB |
Output is correct |
10 |
Correct |
495 ms |
19964 KB |
Output is correct |