#include<bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define sz(a) (int)a.size()
#define ll long long
const int N = 1e5;
const int T = 20;
vector <pair <int, int> > g[N];
int n, k;
int used[N];
int _sz[N];
vector <int> h_p[N];
vector <int> Trees[N];
ll ans = 0;
void update(int ind, int v, int l, int r, int x, int c)
{
Trees[ind][v] += c;
if(l == r)
{
return;
}
if(x <= (r + l) / 2)
{
update(ind, v * 2, l, (r + l) / 2, x, c);
}
else
{
update(ind, v * 2 + 1, (r + l) / 2 + 1, r, x, c);
}
}
int get_ans(int ind, int v, int l, int r, int al, int ar)
{
if(al > ar)
{
return 0;
}
if(l >= al && r <= ar)
{
return Trees[ind][v];
}
if(l <= ar && r >= al)
{
return get_ans(ind, v * 2, l, (r + l) / 2, al, ar) + get_ans(ind, v * 2 + 1, (r + l) / 2 + 1, r, al, ar);
}
return 0;
}
int coun[N];
void dfs(int v, int p = -1)
{
_sz[v] = 1;
for(auto to : g[v])
{
if(!used[to.X] && to.X != p)
{
dfs(to.X, v);
_sz[v] += _sz[to.X];
}
}
}
int centroid(int v, int sz1, int p = -1)
{
for(auto to : g[v])
{
if(!used[to.X] && to.X != p && _sz[to.X] * 2 > sz1)
{
return centroid(to.X, sz1, v);
}
}
return v;
}
int par[N];
int H[N];
vector <pair <int, int> > vec;
vector <int> h_t[N];
void go_to(int v, int maxd, int h, int p = -1)
{
if(maxd >= h)
{
ans++;
}
h_t[par[v]].push_back(h);
H[v] = h;
coun[par[v]]++;
vec.push_back({maxd, v});
for(auto to : g[v])
{
if(used[to.X] || to.X == p)
{
continue;
}
par[to.X] = par[v];
go_to(to.X, max(maxd, to.Y), h + 1, v);
}
}
void build(int v)
{
dfs(v);
v = centroid(v, _sz[v]);
used[v] = 1;
for(auto to : g[v])
{
h_t[to.X].clear();
coun[to.X] = 0;
Trees[to.X + 1].clear();
if(!used[to.X])
{
par[to.X] = to.X;
go_to(to.X, to.Y, 1);
sort(h_t[to.X].begin(), h_t[to.X].end());
Trees[to.X + 1].resize(4 * coun[to.X]);
}
}
sort(vec.begin(), vec.end());
for(auto p : vec)
{
int t_l1 = p.X - H[p.Y];
int t_l2 = upper_bound(h_t[par[p.Y]].begin(), h_t[par[p.Y]].end(), t_l1) - h_t[par[p.Y]].begin();
t_l2--;
ans += get_ans(0, 1, 0, N - 1, 0, t_l1) - get_ans(par[p.Y] + 1, 1, 0, coun[par[p.Y]] - 1, 0, t_l2);
update(0, 1, 0, N - 1, H[p.Y], 1);
int d1 = lower_bound(h_t[par[p.Y]].begin(), h_t[par[p.Y]].end(), H[p.Y]) - h_t[par[p.Y]].begin();
update(par[p.Y] + 1, 1, 0, coun[par[p.Y]] - 1, d1, 1);
}
// cout << v << " " << ans << "\n";
for(auto p : vec)
{
int t_l1 = p.X - H[p.Y];
int t_l2 = upper_bound(h_t[par[p.Y]].begin(), h_t[par[p.Y]].end(), t_l1) - h_t[par[p.Y]].begin();
t_l2--;
update(0, 1, 0, N - 1, H[p.Y], -1);
int d1 = lower_bound(h_t[par[p.Y]].begin(), h_t[par[p.Y]].end(), H[p.Y]) - h_t[par[p.Y]].begin();
update(par[p.Y] + 1, 1, 0, coun[par[p.Y]] - 1, d1, -1);
}
vec.clear();
for(auto to : g[v])
{
if(!used[to.X])
{
build(to.X);
}
}
}
signed main()
{
// ifstream cin("input1.txt.4c");
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for(int i = 0; i < n - 1; i++)
{
int x, y, w;
cin >> x >> y >> w;
x--;
y--;
w -= k;
g[x].push_back({y, w});
g[y].push_back({x, w});
}
Trees[0].resize(4 * N);
build(0);
cout << ans * 2;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
11240 KB |
Output is correct |
2 |
Correct |
8 ms |
11212 KB |
Output is correct |
3 |
Correct |
9 ms |
11276 KB |
Output is correct |
4 |
Correct |
13 ms |
11544 KB |
Output is correct |
5 |
Correct |
13 ms |
11552 KB |
Output is correct |
6 |
Correct |
12 ms |
11596 KB |
Output is correct |
7 |
Correct |
12 ms |
11524 KB |
Output is correct |
8 |
Correct |
12 ms |
11596 KB |
Output is correct |
9 |
Correct |
10 ms |
11468 KB |
Output is correct |
10 |
Correct |
11 ms |
11472 KB |
Output is correct |
11 |
Correct |
10 ms |
11396 KB |
Output is correct |
12 |
Correct |
11 ms |
11528 KB |
Output is correct |
13 |
Correct |
11 ms |
11480 KB |
Output is correct |
14 |
Correct |
11 ms |
11520 KB |
Output is correct |
15 |
Correct |
10 ms |
11468 KB |
Output is correct |
16 |
Correct |
12 ms |
11404 KB |
Output is correct |
17 |
Correct |
10 ms |
11428 KB |
Output is correct |
18 |
Correct |
11 ms |
11396 KB |
Output is correct |
19 |
Correct |
11 ms |
11468 KB |
Output is correct |
20 |
Correct |
12 ms |
11468 KB |
Output is correct |
21 |
Correct |
12 ms |
11412 KB |
Output is correct |
22 |
Correct |
11 ms |
11392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
11260 KB |
Output is correct |
2 |
Correct |
8 ms |
11212 KB |
Output is correct |
3 |
Correct |
10 ms |
11220 KB |
Output is correct |
4 |
Correct |
14 ms |
11576 KB |
Output is correct |
5 |
Correct |
69 ms |
15380 KB |
Output is correct |
6 |
Correct |
446 ms |
33932 KB |
Output is correct |
7 |
Correct |
717 ms |
58108 KB |
Output is correct |
8 |
Correct |
942 ms |
58620 KB |
Output is correct |
9 |
Correct |
742 ms |
58296 KB |
Output is correct |
10 |
Correct |
999 ms |
58524 KB |
Output is correct |
11 |
Correct |
769 ms |
58116 KB |
Output is correct |
12 |
Correct |
1012 ms |
58616 KB |
Output is correct |
13 |
Correct |
765 ms |
58216 KB |
Output is correct |
14 |
Correct |
945 ms |
58552 KB |
Output is correct |
15 |
Correct |
977 ms |
58396 KB |
Output is correct |
16 |
Correct |
972 ms |
58496 KB |
Output is correct |
17 |
Correct |
1022 ms |
58544 KB |
Output is correct |
18 |
Correct |
975 ms |
58372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
11240 KB |
Output is correct |
2 |
Correct |
8 ms |
11212 KB |
Output is correct |
3 |
Correct |
9 ms |
11276 KB |
Output is correct |
4 |
Correct |
13 ms |
11544 KB |
Output is correct |
5 |
Correct |
13 ms |
11552 KB |
Output is correct |
6 |
Correct |
12 ms |
11596 KB |
Output is correct |
7 |
Correct |
12 ms |
11524 KB |
Output is correct |
8 |
Correct |
12 ms |
11596 KB |
Output is correct |
9 |
Correct |
10 ms |
11468 KB |
Output is correct |
10 |
Correct |
11 ms |
11472 KB |
Output is correct |
11 |
Correct |
10 ms |
11396 KB |
Output is correct |
12 |
Correct |
11 ms |
11528 KB |
Output is correct |
13 |
Correct |
11 ms |
11480 KB |
Output is correct |
14 |
Correct |
11 ms |
11520 KB |
Output is correct |
15 |
Correct |
10 ms |
11468 KB |
Output is correct |
16 |
Correct |
12 ms |
11404 KB |
Output is correct |
17 |
Correct |
10 ms |
11428 KB |
Output is correct |
18 |
Correct |
11 ms |
11396 KB |
Output is correct |
19 |
Correct |
11 ms |
11468 KB |
Output is correct |
20 |
Correct |
12 ms |
11468 KB |
Output is correct |
21 |
Correct |
12 ms |
11412 KB |
Output is correct |
22 |
Correct |
11 ms |
11392 KB |
Output is correct |
23 |
Correct |
9 ms |
11260 KB |
Output is correct |
24 |
Correct |
8 ms |
11212 KB |
Output is correct |
25 |
Correct |
10 ms |
11220 KB |
Output is correct |
26 |
Correct |
14 ms |
11576 KB |
Output is correct |
27 |
Correct |
69 ms |
15380 KB |
Output is correct |
28 |
Correct |
446 ms |
33932 KB |
Output is correct |
29 |
Correct |
717 ms |
58108 KB |
Output is correct |
30 |
Correct |
942 ms |
58620 KB |
Output is correct |
31 |
Correct |
742 ms |
58296 KB |
Output is correct |
32 |
Correct |
999 ms |
58524 KB |
Output is correct |
33 |
Correct |
769 ms |
58116 KB |
Output is correct |
34 |
Correct |
1012 ms |
58616 KB |
Output is correct |
35 |
Correct |
765 ms |
58216 KB |
Output is correct |
36 |
Correct |
945 ms |
58552 KB |
Output is correct |
37 |
Correct |
977 ms |
58396 KB |
Output is correct |
38 |
Correct |
972 ms |
58496 KB |
Output is correct |
39 |
Correct |
1022 ms |
58544 KB |
Output is correct |
40 |
Correct |
975 ms |
58372 KB |
Output is correct |
41 |
Correct |
7 ms |
11212 KB |
Output is correct |
42 |
Correct |
724 ms |
58168 KB |
Output is correct |
43 |
Correct |
893 ms |
58596 KB |
Output is correct |
44 |
Correct |
733 ms |
58292 KB |
Output is correct |
45 |
Correct |
917 ms |
58524 KB |
Output is correct |
46 |
Correct |
704 ms |
58192 KB |
Output is correct |
47 |
Correct |
894 ms |
58632 KB |
Output is correct |
48 |
Correct |
694 ms |
58232 KB |
Output is correct |
49 |
Correct |
919 ms |
58644 KB |
Output is correct |
50 |
Correct |
899 ms |
58356 KB |
Output is correct |
51 |
Correct |
952 ms |
58552 KB |
Output is correct |
52 |
Correct |
277 ms |
31772 KB |
Output is correct |
53 |
Correct |
320 ms |
32116 KB |
Output is correct |
54 |
Correct |
284 ms |
31548 KB |
Output is correct |
55 |
Correct |
342 ms |
32116 KB |
Output is correct |
56 |
Correct |
330 ms |
32004 KB |
Output is correct |
57 |
Correct |
588 ms |
40940 KB |
Output is correct |
58 |
Correct |
654 ms |
39992 KB |
Output is correct |
59 |
Correct |
720 ms |
41116 KB |
Output is correct |
60 |
Correct |
760 ms |
40892 KB |
Output is correct |
61 |
Correct |
766 ms |
40976 KB |
Output is correct |
62 |
Correct |
475 ms |
36624 KB |
Output is correct |
63 |
Correct |
597 ms |
37176 KB |
Output is correct |
64 |
Correct |
659 ms |
38004 KB |
Output is correct |
65 |
Correct |
27 ms |
12364 KB |
Output is correct |
66 |
Correct |
8 ms |
11212 KB |
Output is correct |