AT_abc477_g.Frequency Query on Tree

省选/NOI-

通过率:0%

时间限制:3.00s

内存限制:1024MB

AC君温馨提醒

该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

给你一棵包含 NN 个顶点的树,顶点编号为 11 到 NN。第 ii 条边连接顶点 uiu_i 和顶点 viv_i。此外,顶点 ii 上写有一个整数 xix_i。
你需要处理 QQ 个查询。对于每个查询,给定整数 s,t,a,bs, t, a, b;求满足以下条件的整数 yy 的个数:

  • 设 ff 表示在顶点 ss 与顶点 tt 之间的路径上,所写整数恰好为 yy 的顶点个数,则需满足 a≤f≤ba \leq f \leq b。

输入格式

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

NN QQ
x1x_1 x2x_2 …\dots 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

输入从标准输入中按以下格式给出:

NN QQ
x1x_1 x2x_2 …\dots 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

每个查询 queryq\mathrm{query}_q 的格式如下:

ss tt aa bb

输出格式

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

输出 QQ 行。第 qq 行应包含第 qq 个查询的答案。

输入输出样例

  • 输入#1

    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

    输出#1

    3
    1
    3
    0
  • 输入#2

    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

    输出#2

    1
    2
    0
    1
    0
    2
    1
    0

说明/提示

Sample 1 Explanation:
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.

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.

样例 1 解释:
考虑第一个查询。
连接顶点 11 与顶点 55 的路径上的顶点依次为:顶点 11、顶点 22、顶点 33、顶点 44 和顶点 55。
这些顶点上所写的整数依次为 1,2,3,1,21, 2, 3, 1, 2。因此,满足条件的整数 yy 共有三个:1,2,31, 2, 3。

约束条件

  • 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
  • 给定图是一棵树。
  • 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
  • 所有输入值均为整数。

输入解题思路,AI测评打分。不知道怎么写?

首页