307858: CF1425I. Impressive Harvesting of The Orchard

Memory Limit:256 MB Time Limit:7 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Impressive Harvesting of The Orchard

题意翻译

Chanek先生有一个果园,果园的结构是一棵三叉的有根树。树上有N个顶点,编号从 $1$ 到 $ N$ ,并且树的根是顶点 $1$ 。对于 $2 \le i \le N$ ,定义 $P_i$ 表示顶点$i$的父节点。树的高度不大于 $10$,这里的树的高度被定义为树根到树上顶点的最大距离。 果园的每个顶点上都存在一个灌木丛。最初所有的灌木丛上都有果实,顶点 $i$ 的灌木丛在最后一次被采摘后的 $A_i$ 天后将会长出果实。目前已经有果实的灌木丛上不会再长出果实。 Chanek先生会到他的果园参观 $ Q $ 天。在第 $i$ 天,他将采集节点 $X_i$ 的子树内所有灌木上的果实。你需要求出每一天所有被采集的灌木到 $ X_i $ 的距离之和,以及当天被采集的果实的数量。

题目描述

Mr. Chanek has an orchard structured as a rooted ternary tree with $ N $ vertices numbered from $ 1 $ to $ N $ . The root of the tree is vertex $ 1 $ . $ P_i $ denotes the parent of vertex $ i $ , for $ (2 \le i \le N) $ . Interestingly, the height of the tree is not greater than $ 10 $ . Height of a tree is defined to be the largest distance from the root to a vertex in the tree. There exist a bush on each vertex of the tree. Initially, all bushes have fruits. Fruits will not grow on bushes that currently already have fruits. The bush at vertex $ i $ will grow fruits after $ A_i $ days since its last harvest. Mr. Chanek will visit his orchard for $ Q $ days. In day $ i $ , he will harvest all bushes that have fruits on the subtree of vertex $ X_i $ . For each day, determine the sum of distances from every harvested bush to $ X_i $ , and the number of harvested bush that day. Harvesting a bush means collecting all fruits on the bush. For example, if Mr. Chanek harvests all fruits on subtree of vertex $ X $ , and harvested bushes $ [Y_1, Y_2, \dots, Y_M] $ , the sum of distances is $ \sum_{i = 1}^M \text{distance}(X, Y_i) $ $ \text{distance}(U, V) $ in a tree is defined to be the number of edges on the simple path from $ U $ to $ V $ .

输入输出格式

输入格式


The first line contains two integers $ N $ and $ Q $ $ (1 \le N,\ Q,\le 5 \cdot 10^4) $ , which denotes the number of vertices and the number of days Mr. Chanek visits the orchard. The second line contains $ N $ integers $ A_i $ $ (1 \le A_i \le 5 \cdot 10^4) $ , which denotes the fruits growth speed on the bush at vertex $ i $ , for $ (1 \le i \le N) $ . The third line contains $ N-1 $ integers $ P_i $ $ (1 \le P_i \le N, P_i \ne i) $ , which denotes the parent of vertex $ i $ in the tree, for $ (2 \le i \le N) $ . It is guaranteed that each vertex can be the parent of at most $ 3 $ other vertices. It is also guaranteed that the height of the tree is not greater than $ 10 $ . The next $ Q $ lines contain a single integer $ X_i $ $ (1 \le X_i \le N) $ , which denotes the start of Mr. Chanek's visit on day $ i $ , for $ (1 \le i \le Q) $ .

输出格式


Output $ Q $ lines, line $ i $ gives the sum of distances from the harvested bushes to $ X_i $ , and the number of harvested bushes.

输入输出样例

输入样例 #1

2 3
1 2
1
2
1
1

输出样例 #1

0 1
0 1
1 2

输入样例 #2

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

输出样例 #2

6 5
3 2
4 4

说明

For the first example: - On day 1, Mr. Chanek starts at vertex $ 2 $ and can harvest the bush at vertex 2. - On day 2, Mr. Chanek starts at vertex $ 1 $ and only harvest from bush $ 1 $ (bush 2's fruit still has not grown yet). - On day 3, Mr. Chanek starts at vertex $ 1 $ and harvests the fruits on bush $ 1 $ and $ 2 $ . The sum of distances from every harvested bush to $ 1 $ is $ 1 $ . For the second example, Mr. Chanek always starts at vertex $ 1 $ . The bushes which Mr. Chanek harvests on day one, two, and three are $ [1, 2, 3, 4, 5], [2, 3], [1, 2, 3, 5] $ , respectively.

Input

题意翻译

Chanek先生有一个果园,果园的结构是一棵三叉的有根树。树上有N个顶点,编号从 $1$ 到 $ N$ ,并且树的根是顶点 $1$ 。对于 $2 \le i \le N$ ,定义 $P_i$ 表示顶点$i$的父节点。树的高度不大于 $10$,这里的树的高度被定义为树根到树上顶点的最大距离。 果园的每个顶点上都存在一个灌木丛。最初所有的灌木丛上都有果实,顶点 $i$ 的灌木丛在最后一次被采摘后的 $A_i$ 天后将会长出果实。目前已经有果实的灌木丛上不会再长出果实。 Chanek先生会到他的果园参观 $ Q $ 天。在第 $i$ 天,他将采集节点 $X_i$ 的子树内所有灌木上的果实。你需要求出每一天所有被采集的灌木到 $ X_i $ 的距离之和,以及当天被采集的果实的数量。

加入题单

上一题 下一题 算法标签: