Home => ProblemSet => 3.15-01:Count on a tree II【模板】树分块
Problem1864--3.15-01:Count on a tree II【模板】树分块

1864: 3.15-01:Count on a tree II【模板】树分块

Time Limit: 2 Sec  Memory Limit: 512 MB  Submit: 0  Solved: 0
[ Submit ] [ Status ] [ Creator: ][ 参考程序 ]

Description

You are given a tree with N nodes. The tree nodes are numbered from 1 to N. Each node has an integer weight.
We will ask you to perform the following operation:
  • u v : ask for how many different integers that represent the weight of nodes there are on the path from u to v.
  • 给定 n 个结点的树,每个结点有一种颜色。
  • m 次询问,每次询问给出  u,v,回答 u,v 之间的路径上的结点的不同颜色数。
  • 1≤n≤4×104,1≤m≤105,颜色是不超过 2×109 的非负整数。


Input

In the first line there are two integers N and M. (N <= 40000, M <= 100000)

In the second line there are N integers. The i-th integer denotes the weight of the i-th node.

In the next N-1 lines, each line contains two integers u v, which describes an edge (uv).

In the next M lines, each line contains two integers u v, which means an operation asking for how many different integers that represent the weight of nodes there are on the path from u to v.

Output

For each operation, print its result.

Sample Input Copy

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
7 8

Sample Output Copy

4
4

Source/Category

莫队 DFS