Editorial for XORacci Tree
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Observations
First, let's analyze the relation . You may notice how it is very similar to the recurrence in the well known Fibonacci Sequence, where
.
There are two properties of the bitwise XOR operator that are helpful here:
If we consider a single XOR Fibonacci Sequence:
Based off of the two properties above, we can see that the sequence repeats every 3 terms:
Therefore, for every modify operation, if we let be the number of edges in the simple path from node
to node
, the value of
depends on
.
Subtask 1
In this subtask, for all modify operations, so we only need to consider one rooted tree.
Notice how nodes with the same will have the same
and
. Let
be the value of
of nodes
with
.
For each modify operation, we can update the following:
For each query operation, the value of is simply
.
Time Complexity: .
Subtask 2
For the full solution, we need to be able to efficiently do the following for modify operations:
for all
such that
for all
such that
for all
such that
Without loss of generality, let's only consider the first task (the remaining two can be done in similar ways).
We can do this by building a centroid tree of the original tree
. One useful property of a centroid tree is that for any two nodes
and
, the lowest common ancestor of
and
in
will always lie on the simple path of
and
in
. Therefore, the set of nodes that we need to update are
, where
is the lowest common ancestor of the two nodes in
(Note:
is still referring to the original tree
).
To update, we can iterate all ancestors of
in
(including
) and make a note to update all
such that
is a descendant of
in
and
. However, just doing this is incorrect, since when
, we must also ensure that
. To do this, notice how some descendants of
(the previous ancestor that we considered) would have
as their lowest common ancestor with
. Therefore, we need to make an additional note to do
(revert the update), such that
is a descendant of
and
.
For query operations, we can calculate by iterating all ancestors of
(including itself), and updating
based on the notes we made in previous modify operations.
Building the centroid tree can be done in through centroid decomposition, and because the height of a centroid tree is
, each operation can be done in
.
Time Complexity: .
Comments