VitalyKo's blog

By VitalyKo, 7 months ago, In English

Let's say we have values on nodes, not edges (with edges everything is fine) and want to answer some path queries with MO algorithm

Usual implementation would do something like this

    move l, r
    ...
    if (u != lca && v != lca) toggle(lca);
    ans[query_id] = ...;
    if (u != lca && v != lca) toggle(lca);

Which is absolutely ok when toggling each node works in $$$O(1)$$$, but that's not always the case. We can have some values inside the node $$$v$$$ and toggling takes $$$size(values[v])$$$ operations. Then that extra toggle for lca node would break the time complexity.

I can think of some fixes to that:

1) Change something in the algorithm so you wouldn't need to extra toggle for lca.

2) Change the way you sort the queries, so you have a small amount of queries segments where lca is the same, so you can toggle it once in the start of the segment and toggle it back in the end.

3) Create a new tree where each node would have only a single value but it will be still possible to represent each path with a segment (and we won't increase total sizes too much).

But I can't find how to do any of these. Any suggestions?

Full text and comments »

  • Vote: I like it
  • +76
  • Vote: I do not like it