#P2633. Count on a tree

    ID: 1644 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>线段树最近公共祖先,LCA主席树可持久化

Count on a tree

Description

Given a tree with nn nodes, each node has a weight. There are mm queries. For each query, you are given u,v,ku, v, k, and you need to answer the kk-th smallest node weight on the path between u xor lastu \text{ xor last} and vv.

Here, last\text{last} is the answer to the previous query, initially defined as 00, i.e., in the first query, uu is not xored.

Input Format

The first line contains two integers n,mn, m.

The second line contains nn integers, where the ii-th integer denotes the weight of node ii.

Each of the next n1n - 1 lines contains two integers x,yx, y, indicating that there is an edge between node xx and node yy.

Each of the last mm lines contains three integers u,v,ku, v, k, representing one query.

Output Format

Output mm lines, each containing a positive integer, which is the answer to each query.

8 5
105 2 9 3 8 5 7 7
1 2
1 3
1 4
3 5
3 6
3 7
4 8
2 5 1
0 5 2
10 5 3
11 5 4
110 8 2
2
8
9
105
7

Hint

Constraints:
For 100%100\% of the testdata, 1n,m1051 \le n, m \le 10^5, and node weights are in [1,2311][1, 2 ^ {31} - 1].

Please refrain from brute force...

Source: bzoj2588 Spoj10628.

The testdata for this problem is self-made by Luogu, generated using CYaRon in 5 minutes.

Translated by ChatGPT 5