#P5399. [Ynoi2018] 駄作
[Ynoi2018] 駄作
Description
Caicaizi gives you 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 , there is an edge 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 , representing the number of nodes in the tree.
The next line contains integers , which indicate that there is an edge between and ().
The next line contains an integer , representing the number of queries.
The next lines describe all queries. Each line contains four integers (), describing one query.
Output Format
Output lines, answering the queries in order. Each line outputs one integer, 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
Idea: nzhtl1477, Solution: ccz181078, Code: ccz181078, Data: ccz181078
Constraints: for of the data, and .
Translated by ChatGPT 5
京公网安备 11011102002149号