G. 树上的频率询问 / Frequency Query on Tree

    传统题 5000ms 1024MiB

树上的频率询问 / Frequency Query on Tree

该比赛已结束,您无法在比赛模式下递交该题目。您可以点击“在题库中打开”以普通模式查看和递交本题。

Problem Statement

You are given a tree with NN vertices numbered 11 to NN. The ii-th edge connects vertex uiu_i and vertex viv_i. Also, integer xix_i is written on vertex ii.

Process QQ queries. In each query, you are given integers s,t,a,bs,t,a,b; find the number of integers yy satisfying the following condition.

  • Let ff be the number of vertices with integer yy written on them among the vertices on the path connecting vertex ss and vertex tt. Then, a≤f≤ba \leq f \leq b holds.

Constraints

  • 2≤N≤2×1052 \leq N \leq 2 \times 10^5
  • 1≤Q≤2×1051 \leq Q \leq 2 \times 10^5
  • 1≤ui<vi≤N1 \leq u_i \lt v_i \leq N
  • The given graph is a tree.
  • 1≤xi≤N1 \leq x_i \leq N
  • 1≤s<t≤N1 \leq s \lt t \leq N
  • 1≤a≤b≤N1 \leq a \leq b \leq N
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

  • NN QQ
  • x1x_1 x2x_2 …\ldots xNx_N
  • u1u_1 v1v_1
  • u2u_2 v2v_2
  • ⋮\vdots
  • uN−1u_{N-1} vN−1v_{N-1}
  • query1\mathrm{query}_1
  • query2\mathrm{query}_2
  • ⋮\vdots
  • queryQ\mathrm{query}_Q

Each query queryq\mathrm{query}_q is given in the following format:

  • ss tt aa bb

Output

Output QQ lines. The qq-th line should contain the answer to the qq-th query.

5 4
1 2 3 1 2
1 2
2 3
3 4
4 5
1 5 1 2
1 4 2 3
3 5 1 1
2 4 2 5
3
1
3
0

Consider the first query. The vertices on the path connecting vertex 11 and vertex 55 are, in order, vertex 11, vertex 22, vertex 33, vertex 44, and vertex 55. The integers written on these vertices are, in order, 1,2,3,1,21, 2, 3, 1, 2. Thus, there are three integers yy satisfying the condition: 1,2,31,2,3.

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

ABC477 赛后模拟赛 ✅️

未参加
状态
已结束
规则
IOI
题目
7
开始于
2026-9-26 21:40
结束于
2026-10-10 21:40
持续时间
336 小时
主持人
参赛人数
49