Solving tasks with trees without HLD

Правка en6, от AndreyPavlov, 2022-08-01 19:41:14

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:

  1. Checking all subtree addings and remember in segment tree with adding in point $$$t$$$ value $$$x$$$
  2. Go to the childs with dfs
  3. Checking all paths to the root adding and remember as well as in 1
  4. Get the answer on all queries with sum on prefix $$$t$$$
  5. 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 lca.

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)$$$

Теги segment tree, fenwick tree, heavy-light, hld, tree, query, add, offline

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en18 Английский AndreyPavlov 2022-08-03 17:48:41 4
en17 Английский AndreyPavlov 2022-08-03 16:37:48 2 Tiny change: 'fter.\n\n[1. Max Flow' -> 'fter.\n\n[A. Max Flow'
en16 Английский AndreyPavlov 2022-08-02 16:31:31 128
en15 Английский AndreyPavlov 2022-08-02 14:13:42 3 Tiny change: ' \cdot log n)$\n\n### ' -> ' \cdot log{n})$\n\n### '
en14 Английский AndreyPavlov 2022-08-01 21:19:04 12
en13 Английский AndreyPavlov 2022-08-01 19:59:46 149
en12 Английский AndreyPavlov 2022-08-01 19:53:42 2 Tiny change: 'n point.\n[C. Prop' -> 'n point.\n\n[C. Prop'
en11 Английский AndreyPavlov 2022-08-01 19:53:01 139
en10 Английский AndreyPavlov 2022-08-01 19:48:40 3 Tiny change: ' dfs(child, u);\n ' -> ' dfs(child);\n '
en9 Английский AndreyPavlov 2022-08-01 19:46:20 156
en8 Английский AndreyPavlov 2022-08-01 19:44:20 4 Tiny change: 'o without lca.\n\n**Ide' -> 'o without hld.\n\n**Ide'
en7 Английский AndreyPavlov 2022-08-01 19:42:46 0 (published)
en6 Английский AndreyPavlov 2022-08-01 19:41:14 3 (saved to drafts)
en5 Английский AndreyPavlov 2022-08-01 19:29:07 0 (published)
en4 Английский AndreyPavlov 2022-08-01 19:28:38 80 Tiny change: 'ick tree\n\n### **' -> 'ick tree\nAsymptotics is $O(n + q log n)$\n\n### **'
en3 Английский AndreyPavlov 2022-08-01 18:31:58 1641 Tiny change: 'roblem**\n\nWhen y' -> 'roblem**\n===========\n\nWhen y'
en2 Английский AndreyPavlov 2022-08-01 18:03:48 1021
en1 Английский AndreyPavlov 2022-08-01 13:27:42 1025 Initial revision (saved to drafts)