Problem
When you learn about Heavy Light Decomposition tree problems take on new meaning. But writing HLD every tree problem with queries so boring. In this post, I will show some problems (as well as ideas for solving them) in which you can write HLD, but with a little thought, the solution becomes easier.
Modifications and receiving at the point
Task: A tree is given, as well as requests to add in a subtree and on the path to the root, as well as to find out the value at the top.
First idea — HLD! But stop, we only need the value at the point, maybe can easier? Yes, we will solve task in offline mode.
Idea: How to find out what changes occurred at the top at time $$$t$$$? Need to store some structure, for example, segment tree and answer for query in time $$$t$$$ is sum on prefix $$$t$$$. Now we have add in subtree and on the path to the root. For subtere adding we will remember for each vertex that in its subtree we need to add the number $$$x$$$ and the query time. Also for path adding we will remember this. Now we run the dfs on this tree with next algorithm:
- Checking all subtree addings and remember in segment tree with adding in point $$$t$$$ value $$$x$$$
- Go to the childs with dfs
- Checking all paths to the root adding and remember as well as in 1
- Get the answer on all queries with sum on prefix $$$t$$$
- Remove all subtree addings (add $$$-x$$$ on prefix $$$t$$$)
DFS implementation:
// ...
void dfs (int u, int p) {
for (auto [t, x]: subtree_query[u]) {
segment_tree.upd(t, x);
}
for (int child: graph[u]) {
if (child != p) {
dfs(child, u);
}
}
for (auto [x, t]: path_root_query[u]) {
segment_tree.upd(t, x);
}
for (auto t: query[u]) {
answer[t] = segment_tree.get(0, t);
}
for (auto [t, x]: subtree_query[u]) {
segment_tree.upd(t, -x);
}
}
// ...
NOTE: instead of a segment tree, you can use a fenwick tree
Asymptotics is $$$O(n + q \cdot log{n})$$$
Adding on path, get the value at the point
Task: Given a tree and given queries to add x on the path and get the value at the point. Sounds like a normal task on HLD, because we need to add on path of two vertex. But here you can do without hld.
Idea: Find the LCA of 2 vertex, call it $$$l$$$. We will be again work in offline mode, we know of query his time $$$t$$$. First we solve the problem, if first there are change requests and then get requests at the point. We will make next trick — we create a new array let's call him $$$a$$$ add $$$-1$$$ to $$$a_l$$$ and parent of $$$l$$$, add $$$1$$$ to $$$a_u$$$ and $$$a_v$$$. Next we run dfs on this tree and for every vertex $$$u$$$ count $$$\sum{a_i}$$$ where $$$i$$$ is vertex in subtree of $$$u$$$. Note that this sum will be equal to the number that will be written at the top after all requests. How to solve our originially problem? Let's add a segment tree for time $$$t$$$ just like you did in the previous task.
DFS implementation:
// ...
void dfs (int u) {
for (auto [t, x]: subtree_query[u]) {
segment_tree.upd(t, x);
}
for (int child: graph[u]) {
if (child != p[u]) {
dfs(child, u);
}
}
for (auto t: query[u]) {
answer[t] = segment_tree.get(0, t);
}
}
// ...
void add (int u, int v, int t) {
int l = lca(u, v);
subtree_query[l].push_back({t, -1});
subtree_query[p[l]].push_back({t, -1});
subtree_query[u].push_back({t, 1});
subtree_query[v].push_back({t, 1});
}
Note: we also may change segment tree on fenwick tree
Asymptotics $$$O(n + q \cdot log n)$$$