Under construction

Still being built. Things may change

XOR, Sets, Trees, and Queries

Time limit2sMemory limit1024 MB

Summary
Given a tree with a value on each vertex, find for each query path the minimum element of the closure of its path values under XOR, doubling, and halving.
Level

Medium7 of 10

Topics
Bit manipulation, Tree, DFS
Solved
No attempts yet

Problem

For a finite set SS of positive integers, f(S)f(S) is defined as the smallest finite set that satisfies the following conditions. Exactly one such set exists.

  • S⊆f(S)S \subseteq f(S).
  • For any positive integers xx and yy, if x,y∈f(S)x, y \in f(S) and x≠yx \neq y, then x⊕y∈f(S)x \oplus y \in f(S). Here ⊕\oplus is the bitwise XOR.
  • For any positive integer xx, if x∈f(S)x \in f(S) and x<21000x < 2^{1000}, then 2x∈f(S)2x \in f(S).
  • For any positive integer xx, if 2x∈f(S)2x \in f(S) and x<21000x < 2^{1000}, then x∈f(S)x \in f(S).

A tree has NN vertices. For every ii (1≤i≤N1 \le i \le N), vertex ii has a positive integer AiA_i written on it. You must process QQ queries.

For a query u,vu, v, let PP be the set of vertices on the shortest path from vertex uu to vertex vv. Output the minimum element of the set f({Ar∣r∈P})f(\{A_r \mid r \in P\}).

Input

The first line contains the number of vertices NN and the number of queries QQ, separated by a space.

The second line contains the positive integers A1,A2,…,ANA_1, A_2, \ldots, A_N written on the vertices, separated by spaces.

From the third line, the next N−1N - 1 lines each contain two vertex numbers aa and bb, separated by a space, that are connected by an edge of the tree.

From line N+2N + 2, the next QQ lines each contain two integers uu and vv, separated by a space, that describe a query.

Output

For each query, print the answer on its own line. Print QQ lines in total.

Constraints

  • 1≤N,Q≤100,0001 \le N, Q \le 100{,}000
  • 0<Ai<2300 < A_i < 2^{30} (1≤i≤N1 \le i \le N)
  • In every query, 1≤u,v≤N1 \le u, v \le N

Examples2

  1. Example 1

    Input
    3 2
    15 8 39
    1 2
    1 3
    3 2
    1 3
    
    Expected output
    1
    5
    
  2. Example 2

    Input
    7 6
    19 5 27 3 46 57 39
    1 2
    1 3
    1 4
    2 5
    2 6
    3 7
    1 2
    2 2
    2 6
    5 6
    3 4
    3 7
    
    Expected output
    1
    5
    5
    3
    1
    5