#P3899. [湖南集训] 更为厉害

    ID: 2836 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>线段树湖南深度优先搜索,DFS主席树

[湖南集训] 更为厉害

Description

Let T\text T be a rooted tree. We make the following definitions:

  • Let aa and bb be two distinct nodes in T\text T. If aa is an ancestor of bb, then we say "aa is more formidable than bb."
  • Let aa and bb be two distinct nodes in T\text T. If the distance between aa and bb in the tree is no more than a given constant xx, then we say "aa and bb are about the same."

Given a rooted tree T\text T with nn nodes, numbered from 11 to nn, where the root is node 11. You need to answer qq queries. In each query, two integers pp and kk are given. Determine how many ordered triples (a,b,c)(a, b, c) satisfy:

  1. a,b,ca, b, c are three distinct nodes in T\text T, and aa is node pp.
  2. aa and bb are both more formidable than cc.
  3. aa and bb are about the same, where the constant in “about the same” is the given kk.

Input Format

The first line contains two positive integers nn and qq, representing the number of nodes in the rooted tree and the number of queries, respectively.

The next n1n - 1 lines each describe an edge of the tree. Each line contains two integers uu and vv, indicating that there is an edge between nodes uu and vv.

The next qq lines each describe an operation. The ii-th line contains two integers, representing pp and kk for the ii-th query.

Output Format

Output qq lines. Each line corresponds to a query and contains the answer to that query.

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

Hint

The tree in the sample is shown below:

For the first and third queries, the valid triples are (2,1,4)(2,1,4), (2,1,5)(2,1,5), and (2,4,5)(2,4,5).

For the second query, the only valid triple is (4,2,5)(4,2,5).

The constraints for all test points are as follows (note that Luogu does not evaluate according to the following):

For all queries in the full testdata, 1p,kn1 \le p, k \le n.

  • On 2023.9.15, a set of hack testdata was added.

Translated by ChatGPT 5