/*************************************
* author: marvinthang *
* created: 04.06.2023 20:21:54 *
*************************************/
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define left ___left
#define right ___right
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define MASK(i) (1LL << (i))
#define BIT(x, i) ((x) >> (i) & 1)
#define __builtin_popcount __builtin_popcountll
#define ALL(v) (v).begin(), (v).end()
#define REP(i, n) for (int i = 0, _n = (n); i < _n; ++i)
#define REPD(i, n) for (int i = (n); i--; )
#define FOR(i, a, b) for (int i = (a), _b = (b); i < _b; ++i)
#define FORD(i, b, a) for (int i = (b), _a = (a); --i >= _a; )
#define FORE(i, a, b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORDE(i, b, a) for (int i = (b), _a = (a); i >= _a; --i)
#define scan_op(...) istream & operator >> (istream &in, __VA_ARGS__ &u)
#define print_op(...) ostream & operator << (ostream &out, const __VA_ARGS__ &u)
#ifdef LOCAL
#include "debug.h"
#else
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define DB(...) 23
#define db(...) 23
#define debug(...) 23
#endif
template <class U, class V> scan_op(pair <U, V>) { return in >> u.first >> u.second; }
template <class T> scan_op(vector <T>) { for (size_t i = 0; i < u.size(); ++i) in >> u[i]; return in; }
template <class U, class V> print_op(pair <U, V>) { return out << '(' << u.first << ", " << u.second << ')'; }
template <size_t i, class T> ostream & print_tuple_utils(ostream &out, const T &tup) { if constexpr(i == tuple_size<T>::value) return out << ")"; else return print_tuple_utils<i + 1, T>(out << (i ? ", " : "(") << get<i>(tup), tup); }
template <class ...U> print_op(tuple<U...>) { return print_tuple_utils<0, tuple<U...>>(out, u); }
template <class Con, class = decltype(begin(declval<Con>()))> typename enable_if <!is_same<Con, string>::value, ostream&>::type operator << (ostream &out, const Con &con) { out << '{'; for (__typeof(con.begin()) it = con.begin(); it != con.end(); ++it) out << (it == con.begin() ? "" : ", ") << *it; return out << '}'; }
// end of template
const int MOD = 1e9;
int DistanceSum(int N, int *X, int *Y) {
auto add = [] (int &a, int b) {
a += b;
if (a >= MOD) a -= MOD;
};
auto solve = [&] (void) {
vector <pair <int, int>> points;
REP(i, N) points.emplace_back(X[i], Y[i]);
sort(ALL(points));
vector <vector <int>> ver;
REP(i, N) {
int j = i;
while (j + 1 < N && points[j].fi == points[j + 1].fi && points[j].se + 1 == points[j + 1].se) ++j;
ver.push_back(vector<int>({points[i].fi, points[i].se, points[j].se, j - i + 1}));
i = j;
}
int n = ver.size();
vector <vector <int>> adj(n);
int j = 0;
REP(i, n) {
while (ver[j][0] + 1 < ver[i][0] || (ver[j][0] + 1 == ver[i][0] && ver[j][2] < ver[i][1])) ++j;
while (ver[j][0] + 1 == ver[i][0] && ver[j][1] <= ver[i][2]) {
adj[i].push_back(j);
adj[j].push_back(i);
++j;
}
j = max(0, j - 1);
}
int res = 0;
function <int(int, int)> dfs = [&] (int u, int par) {
int sz_u = ver[u][3];
for (int v: adj[u]) if (v != par) {
int sz_v = dfs(v, u);
sz_u += sz_v;
add(res, 1LL * (N - sz_v) * sz_v % MOD);
}
return sz_u;
};
dfs(0, 0);
return res;
};
int res = solve();
REP(i, N) swap(X[i], Y[i]);
add(res, solve());
return res;
}
#ifdef LOCAL
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define inbuf_len 1 << 16
#define outbuf_len 1 << 16
int DistanceSum(int N, int *X, int *Y);
int main() {
int tmp;
file("city");
/* Set input and output buffering */
char *inbuf, *outbuf;
inbuf = (char*) malloc(inbuf_len * sizeof(char));
outbuf = (char*) malloc(outbuf_len * sizeof(char));
tmp = setvbuf(stdin, inbuf, _IOFBF, inbuf_len);
assert(tmp == 0);
tmp = setvbuf(stdout, outbuf, _IOFBF, outbuf_len);
assert(tmp == 0);
int N, i;
tmp = scanf("%d", &N);
assert(tmp == 1);
int *sq_x, *sq_y;
sq_x = (int*) malloc(N * sizeof(int));
sq_y = (int*) malloc(N * sizeof(int));
for (i = 0; i < N; i++) {
tmp = scanf("%d %d", &sq_x[i], &sq_y[i]);
assert(tmp == 2);
}
int ds = DistanceSum(N, sq_x, sq_y);
printf("%d\n", ds);
return 0;
}
#endif
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
284 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
2 ms |
404 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
1028 KB |
Output is correct |
2 |
Correct |
8 ms |
1140 KB |
Output is correct |
3 |
Correct |
22 ms |
2084 KB |
Output is correct |
4 |
Correct |
20 ms |
2112 KB |
Output is correct |
5 |
Correct |
47 ms |
3696 KB |
Output is correct |
6 |
Correct |
38 ms |
3768 KB |
Output is correct |
7 |
Correct |
39 ms |
3996 KB |
Output is correct |
8 |
Correct |
39 ms |
3640 KB |
Output is correct |
9 |
Correct |
51 ms |
3840 KB |
Output is correct |
10 |
Correct |
44 ms |
8520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
1748 KB |
Output is correct |
2 |
Correct |
10 ms |
1452 KB |
Output is correct |
3 |
Correct |
27 ms |
3752 KB |
Output is correct |
4 |
Correct |
25 ms |
2956 KB |
Output is correct |
5 |
Correct |
62 ms |
7096 KB |
Output is correct |
6 |
Correct |
43 ms |
4664 KB |
Output is correct |
7 |
Correct |
55 ms |
7260 KB |
Output is correct |
8 |
Correct |
45 ms |
4680 KB |
Output is correct |
9 |
Correct |
46 ms |
4280 KB |
Output is correct |
10 |
Correct |
48 ms |
4000 KB |
Output is correct |