#P16705. [SEATST 2026] XOR 传送 / XOR Teleport

    ID: 19036 远端评测题 4000ms 512MiB 尝试: 0 已通过: 0 显示难度省选/NOI− 上传者: 标签>交互题生成树字典树 Trie2026

[SEATST 2026] XOR 传送 / XOR Teleport

Problem Description

You are given a weighted tree with NN vertices, numbered from 00 to N−1N - 1. For each ii with 1≤i≤N−11 \le i \le N - 1, vertex ii is connected to its parent vertex P[i]P[i] (P[i]<iP[i] < i) by an edge with weight W[i]W[i] (W[i]≥0W[i] \ge 0). Note that vertex 00 has no parent; for convenience, we set P[0]=W[0]=−1P[0] = W[0] = -1.

The only way for Sasaki to move on the tree is by teleportation. Sasaki can teleport from vertex uu to vertex vv using ee energy if and only if all of the following conditions hold:

  • uu is an ancestor of vv, or vv is an ancestor of uu, and
  • the bitwise XOR sum of all edge weights on the path from uu to vv is at most ee.

Note: Each teleport does not consume energy; after each teleport, Sasaki still has ee energy.

::::info[When is uu an ancestor of vv?]{open} Vertex uu is an ancestor of vertex vv if at least one of the following is true:

  • Vertex uu is vertex vv itself (u=vu = v), or
  • Vertex uu is the parent of vertex vv (u=P[v]u = P[v]), or
  • Vertex uu is the parent of the parent of vertex vv (u=P[P[v]]u = P[P[v]]), or
  • Vertex uu is the parent of the parent of the parent of vertex vv (u=P[P[P[v]]]u = P[P[P[v]]]), or
  • and so on. ::::

::::info[What is the bitwise XOR sum (XOR)?]{open} The bitwise XOR sum of two non-negative integers aa and bb (denoted by a⊕ba \oplus b) is defined as follows:

  • When a⊕ba \oplus b is written in binary, for the digit at 2k2^k, the result is 11 if exactly one of aa and bb has a 11 at that digit; otherwise it is 00.

For example:

  • 3⊕5=63 \oplus 5 = 6 (in binary: 011⊕101=110011 \oplus 101 = 110).
  • 4⊕21=174 \oplus 21 = 17 (in binary: 100⊕10101=10001100 \oplus 10101 = 10001).

The bitwise XOR of multiple integers A[0],A[1],...,A[K−1]A[0], A[1], ..., A[K - 1] is defined as $A[0] \oplus A[1] \oplus A[2] \oplus ... \oplus A[K - 1]$.

Note that ⊕\oplus is commutative and associative. That is, a⊕b=b⊕aa \oplus b = b \oplus a and (a⊕b)⊕c=a⊕(b⊕c)(a \oplus b) \oplus c = a \oplus (b \oplus c). Therefore, the final result does not depend on the order of the integers or the order of the XOR operations. ::::

Miyako needs to answer QQ queries. Each query is specified by a pair of integers UU and VV. Miyako's task is to compute the minimum energy required for Sasaki to reach vertex VV from vertex UU using zero or more teleport operations.

Implementation Details

You need to implement the following functions:

void init(int N, std::vector<int> P, std::vector<int> W)
  • NN: the number of vertices in the tree.
  • P,WP, W: integer arrays of length NN that specify each vertex's parent and the connecting edge weight, respectively.
  • This function is called exactly once at the beginning (before any calls to minimum_energy).
int minimum_energy(int U, int V)
  • U,VU, V: a pair of integers describing one query.
  • This function is called exactly QQ times after init is called.
  • This function should return the answer to the given query.

Input Format

N
P[1] P[2] ... P[N - 1]
W[1] W[2] ... W[N - 1]
Q
U[0] V[0]
U[1] V[1]
...
U[Q - 1] V[Q - 1]

Here, U[j]U[j] and V[j]V[j] (for all 0≤j<Q0 \le j < Q) are the input parameters of the jj-th call to minimum_energy.

Output Format

A[0]
A[1]
...
A[Q - 1]

Here, A[j]A[j] is the answer to the jj-th query (for all 0≤j<Q0 \le j < Q).

Hint

Samples

Consider the following function call:

init(6, [-1, 0, 1, 0, 1, 2], [-1, 3, 2, 0, 2, 1])

This tree has 66 vertices, as shown in the figure below.

:::align{center} :::

minimum_energy(2, 4)

Sasaki can use the following teleports, requiring 11 energy to move from vertex 22 to vertex 44:

  • Teleport from vertex 22 to vertex 00. Vertex 00 is an ancestor of vertex 22, and the bitwise XOR sum of edge weights on the path from vertex 22 to vertex 00 is 2⊕3=12 \oplus 3 = 1.
  • Teleport from vertex 00 to vertex 44. Vertex 00 is an ancestor of vertex 44, and the bitwise XOR sum of edge weights on the path from vertex 00 to vertex 44 is 3⊕2=13 \oplus 2 = 1.

There is no teleport sequence that uses strictly less energy. Therefore, this call should return 11.

minimum_energy(3, 0)

Sasaki can use the following teleport, requiring 00 energy to move from vertex 33 to vertex 00:

  • Teleport from vertex 33 to vertex 00. Vertex 00 is an ancestor of vertex 33, and the bitwise XOR sum of edge weights on the path from vertex 33 to vertex 00 is 00.

Therefore, this call should return 00.

minimum_energy(1, 1)

Since both the start and the destination are vertex 11, Sasaki does not need to teleport at all, so the required energy is zero. Therefore, this call should return 00.

minimum_energy(0, 5)

Sasaki can use the following teleport, requiring 00 energy to move from vertex 00 to vertex 55:

  • Teleport from vertex 00 to vertex 55. Vertex 00 is an ancestor of vertex 55, and the bitwise XOR sum of edge weights on the path from vertex 00 to vertex 55 is 3⊕2⊕1=03 \oplus 2 \oplus 1 = 0.

Therefore, this call should return 00.

Constraints

  • 2≤N≤50 0002 \le N \le 50\ 000.
  • 1≤Q≤100 0001 \le Q \le 100\ 000。
  • P[0]=−1P[0] = -1.
  • For all 0≤i<N0 \le i < N, 0≤P[i]<i0 \le P[i] < i.
  • W[0]=−1W[0] = -1.
  • For all 0≤i<N0 \le i < N, 0≤W[i]<2200 \le W[i] < 2^{20}.
  • In each query, 0≤U,V<N0 \le U, V < N.

Subtasks

  1. (55 points) N≤10N \le 10.
  2. (99 points) For all 0≤i<N0 \le i < N, W[i]≤1W[i] \le 1.
  3. (1515 points) N≤200N \le 200.
  4. (00 points) Same as above.
  5. (2828 points) For all 0≤i<N0 \le i < N, W[i]<128W[i] < 128.
  6. (2828 points) N≤10 000N \le 10\ 000.
  7. (1515 points) No additional constraints.

Translated by ChatGPT 5