Domain: Algorithms & Data Structures

You are given the following tree-path query problem.

A tree has $n$ nodes numbered from $1$ to $n$. Each node $i$ has an integer value $a_i$.

Each query gives two nodes $x$ and $y$. Let the simple path from $x$ to $y$ be

$$x = p_0,\; p_1,\; p_2,\; \ldots,\; p_r = y$$

Define

$$S = \sum_{k=0}^{r} \bigl(a_{p_k} \oplus k\bigr)$$

where $\oplus$ is bitwise XOR and $k$ is the 0-based position along the path.

The original input format is:

- First line: integer $t$, the number of test cases.
- For each test case:
  - First line: integer $n$ with $1 \le n \le 5 \times 10^5$
  - Next $n-1$ lines: edges $u, v$
  - Next line: $n$ integers $a_1, a_2, \ldots, a_n$ with $0 \le a_i \le 10^9$
  - Next line: integer $q$ with $1 \le q \le 10^5$
  - Next $q$ lines: queries $x, y$

Constraints:

- The edges form a valid tree.
- The sum of $n$ over all test cases is at most $5 \times 10^5$.
- The sum of $q$ over all test cases is at most $10^5$.

Use this sample instance:

**Sample Input**
```text
1
4
1 2
2 3
3 4
1 2 3 4
3
1 4
4 1
2 4
```

**Sample Output**
```text
12
8
10
```

Write a technical explanation of the solution. Your answer must contain all of the following:

1. A bitwise decomposition of $S$ using symbols $b_k(j)$ for the $j$-th bit of $a_{p_k}$ and $P_j(k)$ for the $j$-th bit of the index $k$, including the per-bit contribution formula.
2. Definitions of $S_0$, $S_1$, and $C_1$, and the identity
   $$\sum_{k=0}^{r} (b_k(j) \oplus P_j(k)) = S_0 + (C_1 - S_1).$$
3. A closed form for $C_1$ in terms of $r_{\text{len}} = r+1$, `full_blocks = floor(r_len / 2^{j+1})`, and `rem = r_len mod 2^{j+1}`.
4. A direct computation for the sample queries showing exactly that $(1,4) \mapsto 12$, $(4,1) \mapsto 8$, and $(2,4) \mapsto 10$.
5. A bitwise verification of the query $(1,4)$ showing per-bit contributions $4$, $4$, and $4$ for bits $j=0,1,2$.
6. A naive per-query algorithm that reconstructs the path and scans positions and bits, with time complexity $O(r \cdot B + \log n)$ per query.
7. An optimized method based on binary lifting with per-bit phase counts, with preprocessing complexity $O(n \cdot \log n \cdot B)$ and query complexity $O(B \cdot \log n)$.


Grading rubric:
criterion: Does the answer define b_k(j) as the j-th bit of a_{p_k}, define P_j(k) as the j-th bit of the path index k, and state that the contribution of bit j is 2^j times the sum of b_k(j) XOR P_j(k) over the path positions?
weight: 15
criterion: Does the answer define S0, S1, and C1 for a fixed bit j and state the identity sum_k (b_k(j) XOR P_j(k)) = S0 + (C1 - S1)?
weight: 15
criterion: Does the answer give the closed form C1 = full_blocks * 2^j + max(0, rem - 2^j), with full_blocks = floor(r_len / 2^{j+1}), rem = r_len mod 2^{j+1}, and r_len = r + 1?
weight: 15
criterion: Does the answer directly compute the sample query results (1,4) = 12, (4,1) = 8, and (2,4) = 10?
weight: 15
criterion: Does the answer verify the sample query (1,4) by bitwise decomposition with per-bit contributions 4, 4, and 4 for bits j = 0, 1, and 2?
weight: 10
criterion: Does the answer describe a naive per-query method that reconstructs the path and scans positions and bits, and state its per-query time complexity as O(r * B + log n)?
weight: 10
criterion: Does the answer describe an optimized binary-lifting approach with per-bit phase counts and state preprocessing complexity O(n * log n * B) and query complexity O(B * log n)?
weight: 20

Answer:
## Bitwise Decomposition

Let the path for a query be $p_0, p_1, \ldots, p_r$. For each bit position $j \ge 0$, define

$$b_k(j) = ((a_{p_k} \gg j) \mathbin{\&} 1)$$

and

$$P_j(k) = ((k \gg j) \mathbin{\&} 1).$$

Then the $j$-th bit of $a_{p_k} \oplus k$ is

$$b_k(j) \oplus P_j(k).$$

So the contribution of bit $j$ to

$$S = \sum_{k=0}^{r} (a_{p_k} \oplus k)$$

is

$$2^j \cdot \sum_{k=0}^{r} (b_k(j) \oplus P_j(k)).$$

Therefore,

$$S = \sum_{j \ge 0} 2^j \cdot \sum_{k=0}^{r} (b_k(j) \oplus P_j(k)).$$

## Simplifying the Per-Bit Count

Fix a bit $j$. Let

- $S_0$ be the sum of $b_k(j)$ over positions with $P_j(k)=0$.
- $S_1$ be the sum of $b_k(j)$ over positions with $P_j(k)=1$.
- $C_1$ be the number of positions with $P_j(k)=1$.

Then

$$\sum_{k=0}^{r} (b_k(j) \oplus P_j(k)) = S_0 + (C_1 - S_1).$$

Reason:

- If $P_j(k)=0$, then $b_k(j) \oplus P_j(k)=b_k(j)$, so those positions contribute $S_0$.
- If $P_j(k)=1$, then $b_k(j) \oplus P_j(k)=1-b_k(j)$, so those positions contribute $C_1-S_1$.

Adding the two parts gives the identity.

## Closed Form for $C_1$

Let

$$r_{\text{len}} = r+1.$$

For a fixed bit $j$, the sequence $P_j(k)$ over consecutive indices has period $2^{j+1}$ and looks like

- $2^j$ zeros,
- then $2^j$ ones.

Define

$$\text{full\_blocks} = \left\lfloor \frac{r_{\text{len}}}{2^{j+1}} \right\rfloor,\qquad \text{rem} = r_{\text{len}} \bmod 2^{j+1}.$$

Each full block contributes exactly $2^j$ ones, and the remainder contributes $\max(0, \text{rem} - 2^j)$ ones. Hence

$$C_1 = \text{full\_blocks} \cdot 2^j + \max(0, \text{rem} - 2^j).$$

## Direct Computation on the Sample

The sample tree is the chain $1-2-3-4$ with values $a=[1,2,3,4]$.

For query $(1,4)$, the path is $[1,2,3,4]$:

$$S = (1 \oplus 0) + (2 \oplus 1) + (3 \oplus 2) + (4 \oplus 3) = 1 + 3 + 1 + 7 = 12.$$

For query $(4,1)$, the path is $[4,3,2,1]$:

$$S = (4 \oplus 0) + (3 \oplus 1) + (2 \oplus 2) + (1 \oplus 3) = 4 + 2 + 0 + 2 = 8.$$

For query $(2,4)$, the path is $[2,3,4]$:

$$S = (2 \oplus 0) + (3 \oplus 1) + (4 \oplus 2) = 2 + 2 + 6 = 10.$$

These are exactly the sample outputs: $12$, $8$, and $10$.

## Bitwise Verification for Query $(1,4)$

Here $r=3$, so the path has four positions $k=0,1,2,3$.

For $j=0$:

- $P_0(k) = [0,1,0,1]$
- $b_k(0)$ from values $[1,2,3,4]$ is $[1,0,1,0]$
- $S_0 = 1+1 = 2$
- $S_1 = 0+0 = 0$
- $C_1 = 2$

So the contribution is

$$2^0 \cdot (S_0 + C_1 - S_1) = 1 \cdot (2+2-0) = 4.$$

For $j=1$:

- $P_1(k) = [0,0,1,1]$
- $b_k(1) = [0,1,1,0]$
- $S_0 = 0+1 = 1$
- $S_1 = 1+0 = 1$
- $C_1 = 2$

So the contribution is

$$2^1 \cdot (1+2-1) = 4.$$

For $j=2$:

- $P_2(k) = [0,0,0,0]$
- $b_k(2) = [0,0,0,1]$
- $S_0 = 1$
- $S_1 = 0$
- $C_1 = 0$

So the contribution is

$$2^2 \cdot (1+0-0) = 4.$$

Thus the per-bit contributions for $j=0,1,2$ are $4$, $4$, and $4$, summing to $12$.

## Naive Per-Query Method

A direct method for one query is:

1. Reconstruct the path from $x$ to $y$ using an LCA-based path decomposition.
2. Enumerate the path positions $k=0$ to $r$.
3. For each relevant bit $j$, scan all positions, compute or accumulate the needed values, and add the contribution for that bit.

If $B$ is the number of relevant bit positions, this costs

$$O(r \cdot B + \log n)$$

per query: $O(\log n)$ to support path reconstruction with LCA machinery and $O(r \cdot B)$ to scan the path and bits. This is too slow in the worst case because $r$ can be $\Theta(n)$.

## Optimized Method

Use binary lifting with per-bit phase counts.

For each node $u$, jump size $2^k$, and bit $j$, store two counts over that upward segment:

- how many nodes with bit $j$ equal to $1$ lie at offsets whose $j$-th offset bit is $0$,
- how many lie at offsets whose $j$-th offset bit is $1$.

This lets two adjacent $2^k$ segments be merged in $O(1)$ per bit because the second half is shifted by exactly $2^k$, so for each bit $j$ its phase either stays the same or swaps depending on whether that shift flips the $j$-th offset bit.

During a query:

1. Split the path using the LCA.
2. Decompose each side into $O(\log n)$ lifted segments.
3. Maintain the global offset along the full path so each stored segment count is mapped to the correct phase.
4. Aggregate per-bit counts and then apply the formula above.

The complexities are:

- preprocessing: $O(n \cdot \log n \cdot B)$
- per query: $O(B \cdot \log n)$

This meets the given constraints.