CF1806E.Tree Master

提高+/省选-

通过率:0%

时间限制:3.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

You are given a tree with nn weighted vertices labeled from 11 to nn rooted at vertex 11. The parent of vertex ii is pip_i and the weight of vertex ii is aia_i. For convenience, define p1=0p_1=0.

For two vertices xx and yy of the same depth†^\dagger, define f(x,y)f(x,y) as follows:

  • Initialize ans=0\mathrm{ans}=0.
  • While both xx and yy are not 00:
    • ans←ans+ax⋅ay\mathrm{ans}\leftarrow \mathrm{ans}+a_x\cdot a_y;
    • x←pxx\leftarrow p_x;
    • y←pyy\leftarrow p_y.
  • f(x,y)f(x,y) is the value of ans\mathrm{ans}.

You will process qq queries. In the ii-th query, you are given two integers xix_i and yiy_i and you need to calculate f(xi,yi)f(x_i,y_i).

†^\dagger The depth of vertex vv is the number of edges on the unique simple path from the root of the tree to vertex vv.

你被给定一棵包含 nn 个带权顶点的树,顶点编号为 11 到 nn,根节点为顶点 11。顶点 ii 的父节点为 pip_i,其权值为 aia_i。为方便起见,定义 p1=0p_1 = 0。

对于两个深度相同†^\dagger 的顶点 xx 和 yy,定义函数 f(x,y)f(x,y) 如下:

  • 初始化 ans=0\mathrm{ans} = 0;
  • 当 x≠0x \neq 0 且 y≠0y \neq 0 时,重复执行:
    • ans←ans+ax⋅ay\mathrm{ans} \leftarrow \mathrm{ans} + a_x \cdot a_y;
    • x←pxx \leftarrow p_x;
    • y←pyy \leftarrow p_y;
  • f(x,y)f(x,y) 即为最终的 ans\mathrm{ans} 值。

你需要处理 qq 个查询。在第 ii 个查询中,你将获得两个整数 xix_i 和 yiy_i,需计算 f(xi,yi)f(x_i, y_i)。

†^\dagger 顶点 vv 的深度定义为从树的根节点到顶点 vv 的唯一简单路径上的边数。

输入格式

The first line contains two integers nn and qq (2≤n≤1052 \le n \le 10^5; 1≤q≤1051 \le q \le 10^5).

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1051 \le a_i \le 10^5).

The third line contains n−1n-1 integers p2,…,pnp_2, \ldots, p_n (1≤pi<i1 \le p_i \lt i).

Each of the next qq lines contains two integers xix_i and yiy_i (1≤xi,yi≤n1\le x_i,y_i\le n). It is guaranteed that xix_i and yiy_i are of the same depth.

第一行包含两个整数 nn 和 qq(2≤n≤1052 \le n \le 10^5;1≤q≤1051 \le q \le 10^5)。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤1051 \le a_i \le 10^5)。

第三行包含 n−1n-1 个整数 p2,…,pnp_2, \ldots, p_n(1≤pi<i1 \le p_i \lt i)。

接下来的 qq 行中,每行包含两个整数 xix_i 和 yiy_i(1≤xi,yi≤n1\le x_i,y_i\le n)。保证 xix_i 和 yiy_i 处于同一深度。

输出格式

Output qq lines, the ii-th line contains a single integer, the value of f(xi,yi)f(x_i,y_i).

输出 qq 行,第 ii 行包含一个整数,即 f(xi,yi)f(x_i,y_i) 的值。

输入输出样例

  • 输入#1

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

    输出#1

    33
    27
  • 输入#2

    14 8
    3 2 5 3 1 4 2 2 2 5 5 5 2 4
    1 2 3 1 1 4 7 3 3 1 5 3 8
    4 4
    4 10
    13 10
    3 12
    13 9
    3 12
    9 10
    11 5

    输出#2

    47
    53
    48
    36
    42
    36
    48
    14

说明/提示

Consider the first example:

In the first query, the answer is a4⋅a5+a3⋅a3+a2⋅a2+a1⋅a1=3+4+25+1=33a_4\cdot a_5+a_3\cdot a_3+a_2\cdot a_2+a_1\cdot a_1=3+4+25+1=33.

In the second query, the answer is a6⋅a6+a2⋅a2+a1⋅a1=1+25+1=27a_6\cdot a_6+a_2\cdot a_2+a_1\cdot a_1=1+25+1=27.

考虑第一个例子:

在第一个查询中,答案为 a4⋅a5+a3⋅a3+a2⋅a2+a1⋅a1=3+4+25+1=33a_4\cdot a_5+a_3\cdot a_3+a_2\cdot a_2+a_1\cdot a_1=3+4+25+1=33。

在第二个查询中,答案为 a6⋅a6+a2⋅a2+a1⋅a1=1+25+1=27a_6\cdot a_6+a_2\cdot a_2+a_1\cdot a_1=1+25+1=27。

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

首页