#include <map>
#include <algorithm>
#include <vector>
#include <stdio.h>
#define N 100000
#define MOD 1000000000
#define INF 2147483646
#define min2(x,y) (x<y?x:y)
#define max2(x,y) (x>y?x:y)
using namespace std;
int n, tree1_len, tree2_len, tree1_cnt[N], tree2_cnt[N];
long long out;
pair<int, long long> t_save[N];
vector<int> tree1[N], tree2[N];
map<int, int > tree1_NUM[N], tree2_NUM[N];
map<int, bool> M[N], tree1_link[N], tree2_link[N];
pair<int, long long> count_tree1(int lev, int up) {
long long cnt = 0, t = 0;
pair<int, long long> temp;
for (auto i : tree1[lev])
if (i != up) {
t_save[i] = temp = count_tree1(i, lev);
t += temp.second, cnt += temp.first;
}
for (auto i : tree1[lev])
if (i != up)
out += (long long)t_save[i].first*(t - t_save[i].second), out %= MOD;
out += (long long)tree1_cnt[lev] * t, out %= MOD;
cnt += (long long)tree1_cnt[lev];
return{ (int)cnt,t + cnt };
}
pair<int, long long> count_tree2(int lev, int up) {
long long cnt = 0, t = 0;
pair<int, long long> temp;
for (auto i : tree2[lev])
if (i != up) {
t_save[i] = temp = count_tree2(i, lev);
t += temp.second, cnt += temp.first;
}
for (auto i : tree2[lev])
if (i != up) out += (long long)t_save[i].first*(t - t_save[i].second), out %= MOD;
out += (long long)tree2_cnt[lev] * t, out %= MOD;
cnt += (long long)tree2_cnt[lev];
return{ (int)cnt,t + cnt };
}
int DistanceSum(int _n, int *X, int *Y) {
n = _n;
int x_min = INF, y_min = INF;
for (int i = 0; i < n; i++) x_min = min2(x_min, X[i]), y_min = min2(y_min, Y[i]);
for (int i = 0; i < n; i++) M[X[i] - x_min][Y[i] - y_min] = true;
for (int i = 0; i < n; i++) {
int tx = X[i] - x_min, ty = Y[i] - y_min;
int ttx = tx, tty = ty;
if (!M[tx][ty - 1]) {
tree1_len++;
while (M[tx][ty]) {
tree1_cnt[tree1_len]++;
tree1_NUM[tx][ty] = tree1_len;
ty++;
}
} tx = ttx, ty = tty;
if (!M[tx - 1][ty]) {
tree2_len++;
while (M[tx][ty]) {
tree2_cnt[tree2_len]++;
tree2_NUM[tx][ty] = tree2_len;
tx++;
}
}
}
int ttx, tty, x, y;
for (int i = 0; i < n; i++) {
int tx = X[i] - x_min, ty = Y[i] - y_min;
ttx = tree1_NUM[tx][ty], tty = tree1_NUM[tx + 1][ty];
x = min2(ttx, tty), y = max2(ttx, tty);
if (x && y && tree1_link[x][y] == false) {
tree1_link[x][y] = true, tree1_link[y][x] = true;
tree1[x].push_back(y), tree1[y].push_back(x);
}
ttx = tree2_NUM[tx][ty], tty = tree2_NUM[tx][ty + 1];
x = min2(ttx, tty), y = max2(ttx, tty);
if (x && y && tree2_link[x][y] == false) {
tree2_link[x][y] = true, tree2_link[y][x] = true;
tree2[x].push_back(y), tree2[y].push_back(x);
}
}
count_tree1(1, 0), count_tree2(1, 0);
return (int)out;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31784 KB |
Output is correct |
2 |
Correct |
3 ms |
31784 KB |
Output is correct |
3 |
Correct |
9 ms |
31784 KB |
Output is correct |
4 |
Correct |
6 ms |
31784 KB |
Output is correct |
5 |
Correct |
6 ms |
31784 KB |
Output is correct |
6 |
Correct |
6 ms |
31784 KB |
Output is correct |
7 |
Correct |
6 ms |
31784 KB |
Output is correct |
8 |
Correct |
3 ms |
31916 KB |
Output is correct |
9 |
Correct |
3 ms |
31784 KB |
Output is correct |
10 |
Correct |
16 ms |
31784 KB |
Output is correct |
11 |
Correct |
16 ms |
31784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
32048 KB |
Output is correct |
2 |
Correct |
6 ms |
32048 KB |
Output is correct |
3 |
Correct |
9 ms |
32312 KB |
Output is correct |
4 |
Correct |
6 ms |
32180 KB |
Output is correct |
5 |
Correct |
16 ms |
32444 KB |
Output is correct |
6 |
Correct |
9 ms |
32180 KB |
Output is correct |
7 |
Correct |
6 ms |
32444 KB |
Output is correct |
8 |
Correct |
9 ms |
32180 KB |
Output is correct |
9 |
Correct |
6 ms |
32180 KB |
Output is correct |
10 |
Correct |
9 ms |
32180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
34832 KB |
Output is correct |
2 |
Correct |
43 ms |
34964 KB |
Output is correct |
3 |
Correct |
129 ms |
39436 KB |
Output is correct |
4 |
Correct |
113 ms |
39568 KB |
Output is correct |
5 |
Correct |
396 ms |
46956 KB |
Output is correct |
6 |
Correct |
326 ms |
47092 KB |
Output is correct |
7 |
Correct |
336 ms |
47668 KB |
Output is correct |
8 |
Correct |
379 ms |
46824 KB |
Output is correct |
9 |
Correct |
243 ms |
47632 KB |
Output is correct |
10 |
Correct |
323 ms |
57844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
38396 KB |
Output is correct |
2 |
Correct |
73 ms |
37000 KB |
Output is correct |
3 |
Correct |
346 ms |
48148 KB |
Output is correct |
4 |
Correct |
263 ms |
44656 KB |
Output is correct |
5 |
Correct |
836 ms |
64284 KB |
Output is correct |
6 |
Correct |
583 ms |
53492 KB |
Output is correct |
7 |
Correct |
909 ms |
64948 KB |
Output is correct |
8 |
Correct |
616 ms |
54188 KB |
Output is correct |
9 |
Correct |
613 ms |
52104 KB |
Output is correct |
10 |
Correct |
566 ms |
51444 KB |
Output is correct |