#CF2236G. 布尔兰迪亚准则 / G. Criterion in Burlandia
布尔兰迪亚准则 / G. Criterion in Burlandia
G. Criterion in Burlandia
Source: Codeforces 2236G
Contest: Codeforces Round 1103 (Div. 3)
Time limit: 3 seconds
Memory limit: 512 megabytes
Statement
Regions in Burlandia form a graph with vertices and edges, and there is exactly one path between any two vertices. Formally, the regions form a tree.
Each region has a friendliness value .
There are queries. In each query, a pair of friends located in different regions is given. They want to know how many subsegments of the path between these regions are hospitable.
It is known that in Burlandia there are two criteria for evaluating relationships — XOR and sum. A subsegment of the path, containing some vertices lying on the path from region to region , is called hospitable if it is non-empty and the sum of friendliness values on this subsegment does not exceed their XOR.
More formally, for each query you are given two vertices and . Consider the shortest path from vertex to vertex in the tree. Let the vertices form this path, where , . You need to find the number of subsegments of this path for which the following condition holds: $$ a_{v_{l}} \oplus a_{v_{l+1}} \oplus \ldots \oplus a_{v_{r}} \geq (a_{v_{l}} + a_{v_{l+1}} + \ldots + a_{v_{r}}), $$ where — the boundaries of the subsegment of vertices on the path from to .
Input
Each test consists of multiple test cases. The first line contains a single integer () — the number of test cases. Then the descriptions of the test cases follow.
The first line of each test case contains integers () — the number of vertices in the tree, and () — the number of queries.
The second line contains an array of non-negative integers — the friendliness values of the regions ().
The next lines describe the edges of the tree: each line contains integers () — an edge.
Then lines follow describing the queries. Each query is given by integers (, ) — the vertices that define the path for which you need to count the number of hospitable subsegments.
It is guaranteed that the sum of and the sum of over all test cases do not exceed , and that the edges indeed form a tree.
Output
For each query output an answer on a separate line.
Note
For clarity, consider the third query from the third sample.
The path from vertex to vertex consists of vertices .
The subsegment does not satisfy the condition, since the XOR on it is , while the sum is .
The subsegment does not satisfy the condition, since the XOR on it is $a_2 \oplus a_4 \oplus a_3 = 2 \oplus 4 \oplus 4 = 2$, while the sum is .
It can be shown that all other 4 subsegments satisfy the condition.
For each query, output the answer in a separate line.
Examples
3
4 3
0 0 4 1
1 2
1 3
1 4
1 4
2 3
2 4
4 3
0 4 1 2
1 3
1 4
2 4
1 2
2 3
2 4
4 3
3 2 4 4
1 2
2 4
3 4
1 2
1 3
2 3
3
6
6
6
10
3
2
5
4