QOJ.ac

QOJ

Time Limit: 4 s Memory Limit: 256 MB Total points: 100

#9036. 駄作

Statistics

菜菜子给了你一棵 $n$ 个节点的树,结点标号从 $1$ 到 $n$ 。

定义树上两点 $a,b$ 的距离 $d(a,b)$ 是最小的非负整数 $k$ ,满足存在结点序列 $v_0,v_1,...,v_k$ ,满足 $v_0=a,v_k=b$ ,且对于 $0\leq i\leq k-1$ 有 $v_i$ 和 $v_{i+1}$ 之间在树上有一条边相连。

有 $m$ 个询问,每个询问包含参数 $p_0,d_0,p_1,d_1$ ,求:

$$\sum\limits_{d(p_0,a)\leq d_0}\sum\limits_{d(p_1,b)\leq d_1}d(a,b)$$

输入格式

第一行一个整数 $n$ ,表示树的节点数目。

接下来一行 $n-1$ 个整数 $f_2,f_3,...,f_n$ ,依次表示 $i$ 和 $f_i$ ( $1\leq f_i\leq i-1$ )之间有一条边。

接下来一行一个整数 $m$ ,表示询问数目。

接下来 $m$ 行依次描述所有询问:每行四个整数 $p_0,d_0,p_1,d_1$ ( $1\leq p_0,p_1\leq n,0\leq d_0,d_1\leq n-1$ )描述一组询问。

输出格式

共 $m$ 行,依次回答各组询问:每行输出一行一个整数表示这组询问的答案。

样例数据

样例输入

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

子任务

Idea:nzhtl1477,Solution:ccz181078,Code:ccz181078,Data:ccz181078

对于 $100\%$ 的数据,保证 $1\leq n\leq 10^5,1\leq m\leq 10^5$ 。

About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.