#P8201. [传智杯 #4 决赛] [yLOI2021] 生活在树上(hard version)

    ID: 9274 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>树形数据结构Special JudgeO2优化最近公共祖先 LCA前缀和差分传智杯

[传智杯 #4 决赛] [yLOI2021] 生活在树上(hard version)

Background

This problem is the harder version of P8200, and the solutions to the two problems are slightly different. The difference in the statement between this problem and P8200 is that this problem gives node weights on the tree, rather than edge weights.

Xiao Zhi lives in “ChuanZhi Kingdom”, a country with nn cities, where the cities are connected by n1n-1 roads.

Each city has a wealth index wiw_i. We define the cost for Xiao Zhi to travel from city aa to city bb as $\mathrm{dis}_{a, b} = \bigoplus \limits_{u \in \mathrm{path}\left(a, b\right)} w_u$, where \bigoplus denotes bitwise XOR (if you do not know what bitwise XOR is, please refer to the hints/explanations below), and path(a,b)\mathrm{path}\left(a,b\right) denotes the set of nodes on the simple path from aa to bb (including aa and bb). That is, disa,b\mathrm{dis}_{a, b} means: after listing all nodes on the simple path from aa to bb as u1,u2,u3,u_1, u_2, u_3, \dots, compute wu1wu2wu3w_{u_1} \bigoplus w_{u_2}\bigoplus w_{u_3} \dots.

One day, Xiao Zhi got the chance to participate in the ChuanZhi Cup. On his way to the contest venue, he thought of a problem, but it seems he cannot solve it, so he told you this problem. Smart classmate, can you help him?

Problem Description

Xiao Zhi said: “Since our country has only nn cities and n1n-1 roads, then our country is equivalent to a tree. I am thinking: in our country, does there exist a city such that ‘the XOR of the cost to city aa and the cost to city bb equals kk’? It is so hard, I cannot figure it out. Can you help me?”

That is, given cities a,ba, b and an integer kk, please determine whether there exists a city tt such that $\mathrm{dis}_{t, a} \bigoplus \mathrm{dis}_{t, b} = k$.

Input Format

The first line contains two integers nn, mm, denoting the number of cities and the number of queries.

The second line contains nn integers wiw_i, denoting the wealth index of city ii.

The next n1n-1 lines each contain two integers x,yx, y, indicating that there is an edge connecting city xx and city yy.

The next mm lines each contain three integers a,b,ka, b, k, indicating that Xiao Zhi travels from city aa to city bb, and the meaning of kk is the same as in the description.

Output Format

Output a total of mm lines.

For the ii-th query, if there exists at least one city tt that satisfies the requirement, output Yes.

If there is no city that satisfies the condition, output No.

The output is case-insensitive. For example, Yes, yES, YES, yes, etc. are all accepted as Yes.

5 3
2 6 8 1 5
1 2
1 3
2 4
2 5
1 2 4
2 3 12
2 3 10
nO
No
YeS
5 10
93 97 100 93 93
2 1
3 2
4 3
5 1
5 2 93
4 1 93
3 2 100
3 2 100
2 3 9999998
1 2 93
2 3 97
1 2 93
2 3 97
4 3 93
no
nO
yEs
yEs
No
yEs
yeS
YES
yES
yes

Hint

“Tree”: A tree is an undirected simple connected graph with nn nodes and n1n-1 edges.

“Bitwise XOR”: Bitwise XOR is a binary operation. Compare the binary bits of two numbers bit by bit: the same gives 00, different gives 11. For example, $3 \bigoplus 5 = (011)_2 \bigoplus (101)_2 = (110)_2 = 6$.

Explanation of Sample 1

The figure below shows the map of ChuanZhi Kingdom.

t{1,2,3,4,5}\forall t \in \{1, 2, 3, 4, 5\}, it is impossible to have $\mathrm{dis} _{t,1} \bigoplus \mathrm{dis}_{t, 2} = 4$ and $\mathrm{dis}_{t, 2} \bigoplus \mathrm{dis}_{t, 3} = 12$, so output No.

If we take t=4t=4, then $\mathrm{dis}_{t, 2} \bigoplus \mathrm{dis}_{t, 3} = 10$, so output Yes.

Constraints

For all test points, it is guaranteed that 1<n5×1051 < n \leq 5 \times 10^5, 1m5×1051 \leq m \leq 5 \times 10^5, 0wi1×1070 \leq w_i \leq 1\times 10^7.

For each query, it is guaranteed that 1a,bn1 \leq a,b \leq n and aba \neq b, 0k1×1070 \leq k \leq 1\times 10^7.

Notes

  • Please pay attention to the impact of constant factors on program efficiency.
  • For two numbers x,y107x, y \leq 10^7, xyx \bigoplus y may be greater than 10710^7. Please pay special attention to this.

Translated by ChatGPT 5