PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000115. Mắc bẫy (In a Trap)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Mắc bẫy (In a Trap)

In a Trap

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an undirected tree with nn vertices numbered from 11 to nn, rooted at vertex 11. Vertex ii contains a nonnegative integer aia_i.

Answer qq queries, each specified by vertices uu and vv. Vertex uu is guaranteed to be an ancestor of vv (a vertex is its own ancestor).

Let P(u,v)P(u,v) denote the set of vertices on the unique path between uu and vv, including both endpoints. Let dist⁡(x,v)\operatorname{dist}(x,v) be the number of edges on the path between xx and vv.

For each query, output

$$\max_{x\in P(u,v)} \bigl(a_x\;\operatorname{XOR}\;\operatorname{dist}(x,v)\bigr).$$

Here, XOR⁡\operatorname{XOR} denotes bitwise exclusive OR. Take the maximum of the individual XOR values, not the XOR of all values on the path. The tree and its values never change.

Input

  • The first line contains integers n,qn,q.
  • The second line contains a1,…,ana_1,\ldots,a_n.
  • Each of the next n−1n-1 lines contains two vertices x,yx,y, representing an undirected edge.
  • Each of the next qq lines contains two vertices u,vu,v describing a query.

The edges form a connected tree without self-loops. In every query, uu is an ancestor of vv in the tree rooted at 11.

Output

For every query output the maximum value on its own line, in input order.

Subtasks

  • Subtask 1 (15 points): 1≤n≤1001\le n\le100, 1≤q≤1001\le q\le100, 0≤ai≤n0\le a_i\le n; all tree/query conditions above hold.
  • Subtask 2 (20 points): 1≤n≤5001\le n\le500, 1≤q≤5001\le q\le500, 0≤ai≤n0\le a_i\le n; all tree/query conditions above hold.
  • Subtask 3 (25 points): 1≤n≤1041\le n\le10^4, 1≤q≤3⋅1041\le q\le3\cdot10^4, 0≤ai≤n0\le a_i\le n; all tree/query conditions above hold.
  • Subtask 4 (40 points): 1≤n≤5⋅1041\le n\le5\cdot10^4, 1≤q≤1.5⋅1051\le q\le1.5\cdot10^5, 0≤ai≤n0\le a_i\le n; 1≤x,y,u,v≤n1\le x,y,u,v\le n; exactly n−1n-1 edges form a tree rooted at 11, and uu is an ancestor of vv in every query.

Examples

Example 1

Input

5 3
0 3 2 1 4
1 2
2 3
3 4
3 5
1 4
1 5
2 4

Output

3
4
3

Explanation

For (1,4)(1,4), visiting x=4,3,2,1x=4,3,2,1 yields XOR values 1,3,1,31,3,1,3, so the maximum is 33. For (1,5)(1,5), the values are 4,3,1,34,3,1,3, giving 44. For (2,4)(2,4), they are 1,3,11,3,1, giving 33.

Example 2

Input

5 4
1 2 3 4 5
1 2
2 3
3 4
4 5
1 5
2 5
1 4
3 3

Output

5
5
4
3

Explanation

The tree is a chain 1−2−3−4−51-2-3-4-5. For (3,3)(3,3), the path consists of vertex 33 only, so the answer is a3XOR⁡0=3a_3\operatorname{XOR}0=3. For (1,4)(1,4), the value at x=4x=4 is 4XOR⁡0=44\operatorname{XOR}0=4, and no other path vertex produces a larger value.