# Cf739 B. ayona and a tree

Time：2022-1-8

Meaning:

A tree with some weight\(val(u)\)And edge weighted tree, point 1 is the root node. if\(v\)stay\(u\)In the subtree of,\(v\neq u\)And\(dis(u,v)\le val(v)\), it is called\(v\)cover\(u\)Control. Ask how many points each point can control.

Idea:

\(u\)Can control\(v\)Namely\(dis(u,v)\le val(v) \iff dep(v)-dep(u)\le val(v) \iff dep(u)>= dep(v)-val(v)\)

Method 1: PBDs, RB_ Tree, also called ordered_ set

For each\(u\), just find out “set s is less than or equal to”\(dep(u)\)Where s is the number of all points in the subtree of U (excluding u itself)\(dep(v)-val(v)\)A collection of values.

use`order_of_key()`Strict less than\(dep(u)+1\)Number of points. To ensure that only the subtree of u is found, record the values before and after the subtree of u, and then make a difference.

Note ordered_ The elements in set cannot be repeated, so they are saved as pairs to prevent repetition. When searching, take second as 0 to ensure that it will not be calculated to be exactly equal to\(dep(u)+1\)All points less than or equal to\(dep(u)\)Point of

``````#include
#include
using namespace __gnu_pbds;
tree,null_type,less>,rb_tree_tag,
tree_order_statistics_node_update> S;
int t;

ll dep[N]; int ans[N];
void dfs(int u)
{
int before = S.order_of_key({dep[u] + 1, 0});

for(int i = h[u]; i; i = ne[i])
{
int v = e[i];
dep[v] = dep[u] + w[i];

dfs(v);
}
int after = S.order_of_key({dep[u] + 1, 0});
ans[u] = after - before;

S.insert({dep[u] - val[u], t++});
}

dfs(1);``````

The practice of multiplication and dichotomy needs to be supplemented