#P5374. [THUPC 2019] 不用找的树
[THUPC 2019] 不用找的树
Description
You are given a tree with nodes, numbered from to .
Define the distance between two nodes on the tree as the smallest non-negative integer such that there exists a node sequence with , , and for every , there is an edge directly connecting and in the tree.
There are queries. Each query contains parameters . Compute:
$$\sum\limits_{d(p_0,a)\leq d_0}\sum\limits_{d(p_1,b)\leq d_1}d(a,b)$$Input Format
The first line contains an integer , the number of nodes in the tree.
The next line contains integers , indicating that there is an edge between and ().
The next line contains an integer , the number of queries.
The next lines describe the queries. Each line contains four integers (), describing one query.
Constraints: .
Output Format
Output lines. For each query, output one integer on its own line, which is the answer to that query.
7
1 1 2 3 5 2
5
5 1 5 0
2 0 5 0
2 2 4 5
7 2 2 4
3 2 5 4
2
3
69
57
70
Hint
Copyright Information
From THUPC (THU Programming Contest, Tsinghua University Programming Contest) 2019.
Resources such as editorials can be found at https://github.com/wangyurzee7/THUPC2019.
Translated by ChatGPT 5
京公网安备 11011102002149号