#P6177. 【模板】树分块 / Count on a tree II

    ID: 6943 远端评测题 2000ms 512MiB 尝试: 2 已通过: 0 难度: 8 上传者: 标签>倍增最近公共祖先 LCA树链剖分可持久化分块

【模板】树分块 / Count on a tree II

Background

Originally bzoj2589.

Problem Description

Given a tree with nn nodes, each node has an integer value. The value on node ii is valival_i.

There are mm queries. Each query gives u,vu', v. You need to decrypt them to get u,vu, v, and then ask: how many distinct integers are on the path from uu to vv.

Decryption method: u=uxorlastansu = u' \operatorname{xor} lastans.

Here, lastanslastans is the answer to the previous query. If there is no previous query, it is 00.

Input Format

The first line contains two integers nn and mm.

The second line contains nn integers. The ii-th integer represents valival_i.

In the next n1n - 1 lines, each line contains two integers u,vu, v, describing an edge.

In the next mm lines, each line contains two integers u,vu', v, describing a query.

Output Format

For each query, output one integer per line, which is the answer.

8 2
105 2 9 3 8 5 7 7 
1 2
1 3
1 4
3 5
3 6
3 7
4 8
2 5
3 8
4
4

Hint

For 100%100\% of the testdata, 1u,vn4×1041 \le u, v \le n \le 4 \times 10^4, 1m1051 \le m \le 10^5, 0u,vali<2310 \le u', val_i < 2^{31}

Translated by ChatGPT 5