公式

I - Tree, Game, and Query 解説 by Lulusphere


Let the current root be \(R\), and define

\[ C_i=V_iW_i. \]

Consider the value

\[ X_R=\bigoplus_{d(R,i)\text{ is odd}} C_i. \]

We claim that the current player wins if and only if

\[ X_R\ne 0. \]

First, suppose \(X_R=0\). If we move \(k\) stones from some vertex to its parent, then exactly one value among the vertices at odd distance from \(R\) changes.

If the starting vertex is at odd distance, then its value changes from \(C\) to \(C-k\). If the starting vertex is at even distance, then its parent is at odd distance, and the parent’s value changes from \(C\) to \(C+k\).

In both cases, one value included in the xor changes, so after the move we always have \(X_R\ne 0\).

Now suppose \(X_R\ne 0\). As in ordinary Nim, choose a vertex \(v\) at odd distance from \(R\) whose \(C_v\) contains the highest bit of \(X_R\). Let

\[ C'_v=C_v\oplus X_R. \]

Then \(C'_v<C_v\). By moving \(C_v-C'_v\) stones from \(v\) to its parent, the xor over all odd-distance vertices becomes \(0\). Since \(v\) is at odd distance from \(R\), it is not the root, so this move is always valid.

Therefore, the winner is determined only by

\[ X_R=\bigoplus_{d(R,i)\text{ is odd}} V_iW_i. \]

If \(X_R=0\), Lulu wins. Otherwise, Terra wins.

It remains to maintain this xor value under queries.

Fix the tree root as vertex \(1\), and compute the depth of every vertex. For any current root \(R\),

\[ d(R,i)\equiv depth(R)+depth(i)\pmod 2. \]

Thus, if \(depth(R)\) is even, we need the xor of active vertices with odd depth. If \(depth(R)\) is odd, we need the xor of active vertices with even depth.

Maintain two values:

\[ X_0=\bigoplus_{\substack{depth(i)\text{ is even}\\ W_i=1}} V_i, \qquad X_1=\bigoplus_{\substack{depth(i)\text{ is odd}\\ W_i=1}} V_i. \]

If the current root \(R\) has even depth, the required value is \(X_1\). Otherwise, it is \(X_0\).

A query of type \(2\ z\) only changes the current root to \(z\). The values \(X_0\) and \(X_1\) are still defined with respect to the fixed root \(1\), so nothing else has to be updated.

Now consider a query of type \(1\ x\ y\), which flips \(W_i\) for every vertex on the path \(P(x,y)\).

When \(W_i\) changes between \(0\) and \(1\), the corresponding xor value changes by xor-ing \(V_i\) once. Therefore, we only need to know the xor of \(V_i\) on the path \(P(x,y)\), separated by depth parity.

Precompute two prefix xor arrays. Let \(P_0(v)\) be the xor of \(V_i\) over even-depth vertices on the path from \(1\) to \(v\), and let \(P_1(v)\) be the same value for odd-depth vertices.

Let

\[ l=\operatorname{lca}(x,y). \]

The xor of even-depth vertices on \(P(x,y)\) is initially

\[ P_0(x)\oplus P_0(y). \]

This misses the LCA once, so if \(depth(l)\) is even, we additionally xor \(V_l\). The odd-depth xor is computed in the same way.

Let these two values be \(Y_0\) and \(Y_1\). Then query \(1\ x\ y\) is processed as

\[ X_0\leftarrow X_0\oplus Y_0, \qquad X_1\leftarrow X_1\oplus Y_1. \]

After each query, we check the xor value corresponding to the current root. If it is \(0\), the winner is Lulu; otherwise, the winner is Terra.

The preprocessing consists of computing depths, LCA information, and the two prefix xor arrays. Each query is processed using one LCA computation.

Therefore, the total time complexity is \({\cal O}((N+Q)\log N)\).