Under construction

Still being built. Things may change

Tree and XOR Queries

Time limit4sMemory limit1024 MB

Summary
Maintain a rooted weighted tree under edge-weight updates; for each query, output the sum of path xors d(a,b) over all a in subtree(x) and b in subtree(y).
Level

Hard8 of 10

Topics
Tree, Bit manipulation, DFS, Segment tree
Solved
No attempts yet

Problem

A tree is given, rooted at node 1, with weights on its edges. Every node is numbered 1,2,⋯ ,N1, 2, \cdots, N. For two nodes xx and yy, let d(x,y)d(x,y) be the xor of all weights on the simple path from xx to yy. If x=yx=y, then d(x,y)=0d(x,y)=0.

Write a program that handles the following two queries.

1 xx vv : change the weight of the edge from node xx to the parent of node xx to vv.

2 xx yy : for any vertex aa in the subtree of xx and any vertex bb in the subtree of yy, output the sum of all values d(a,b)d(a,b).

Input

The first line gives the integer NN, the number of nodes in the tree. (2≤N≤100,000)(2 \leq N \leq 100,000)

Each of the next N−1N-1 lines gives three integers x,y,dx, y, d describing an edge. This means the edge connecting node xx and node yy in the initial tree has weight dd. (1≤x,y≤N(1 \leq x, y \leq N, x≠yx \neq y, 0≤d≤106)0 \leq d \leq 10^6)

The next line gives the integer QQ, the number of queries. (1≤Q≤100,000)(1 \leq Q \leq 100,000)

The next QQ lines give one query per line in one of the following forms. All inputs are integers.

  • 1 xx vv (2≤x≤N(2 \leq x \leq N, 0≤v≤106)0 \leq v \leq 10^6)
  • 2 xx yy (1≤x,y≤N)(1 \leq x, y \leq N)

Query type 2 appears at least once, and xx and yy may be equal.

Output

Print the result of each type 2 query in order, one per line.

Hint

See the link below for the xor operation.

Link

Examples1

  1. Example 1

    Input
    5
    1 2 1
    1 3 0
    2 4 4
    2 5 10
    4
    2 3 3
    2 4 1
    1 3 10
    2 5 3
    
    Expected output
    0
    28
    1