#P8965. 坠梦 | Falling into Dream

    ID: 10032 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 5 上传者: 标签>洛谷原创O2优化前缀和洛谷月赛

坠梦 | Falling into Dream

Background

The gods toy with the mortal world. So-called fate is nothing more than a die thrown by the gods.

The butterfly that the flower cannot wait for eventually becomes a strange dream, restarting again and again.

The gods’ puppets are strangled time after time; in the name of love, they vanish into the sea of flowers of time.

Behind countless obsessions, there is always a twisted “truth”.

What you promised never appeared. Sleepless all night, perhaps I was just being self-willed, loving this human world once on your behalf.

“The most devout only pray; only the undevout have demands.”

There was never faith; by risking one’s life to save someone, one was fortunate enough to reach heaven.

Problem Description

Given an unrooted tree with nn nodes, each edge has a non-negative integer weight. The nodes are numbered from 11 to nn.

For each pair of nodes (x,y)(x, y), define the distance dis(x,y)\operatorname{dis}(x, y) as the XOR sum of edge weights on the unique simple path between xx and yy.

Given two nodes x,yx, y, define the value of node ii as the XOR of the distances from xx to ii and from yy to ii, i.e.,

$$\operatorname{val}_{x, y}(i) = \operatorname{dis}(x, i) \oplus \operatorname{dis}(y, i) \textsf{。}$$

Now there are qq queries. Each query gives four integers x,y,l,rx, y, l, r. You need to compute $\displaystyle \bigoplus_{i = l}^{r} \operatorname{val}_{x, y}(i)$, i.e., compute

$$\operatorname{val}_{x, y}(l) \oplus \operatorname{val}_{x, y}(l + 1) \oplus \cdots \oplus \operatorname{val}_{x, y}(r - 1) \oplus \operatorname{val}_{x, y}(r) \textsf{。}$$

In the formulas above, \oplus denotes the bitwise XOR operator.

Input Format

The first line contains two integers n,qn, q.

The next n1n - 1 lines each contain three integers u,v,wu, v, w, indicating that there is an edge of weight ww between uu and vv.

The next qq lines each contain four integers x,y,l,rx, y, l, r, indicating one query.

Output Format

Output qq lines, each containing one integer, the answer to the corresponding query.

3 2
1 2 1
2 3 1
1 2 1 3
2 3 2 3

1
0

Hint

Sample Explanation

The tree given by the input is shown in the figure above. For distances between pairs of nodes, we have:

  • $\operatorname{dis}(1, 1) = \operatorname{dis}(1, 3) = \operatorname{dis}(2, 2) = \operatorname{dis}(3, 1) = \operatorname{dis}(3, 3) = 0$, and
  • $\operatorname{dis}(1, 2) = \operatorname{dis}(2, 1) = \operatorname{dis}(2, 3) = \operatorname{dis}(3, 2) = 1$.

Query 11: $\operatorname{val}_{1, 2}(1) \oplus \operatorname{val}_{1, 2}(2) \oplus \operatorname{val}_{1, 2}(3) = (0 \oplus 1) \oplus (1 \oplus 0) \oplus (0 \oplus 1) = 1 \oplus 1 \oplus 1 = 1$.

Query 22: $\operatorname{val}_{2, 3}(2) \oplus \operatorname{val}_{2, 3}(3) = (0 \oplus 1) \oplus (1 \oplus 0) = 1 \oplus 1 = 0$.


Constraints

This problem uses bundled testdata.

Subtask ID nn \le qq \le Score
1 100100 1010 24
2 10610^6 14
3 100100 10610^6
4 10610^6 48

For 100%100\% of the data, it is guaranteed that 1n,q1061 \le n, q \le {10}^6, 1u,v,x,yn1 \le u, v, x, y \le n, 1lrn1 \le l \le r \le n, and 0w<2310 \le w < 2^{31}.


Hint

The maximum I/O size of this problem reaches 60 MiB. Please pay attention to I/O efficiency.

Translated by ChatGPT 5