#abc351g. G - Hash on Tree

G - Hash on Tree

Score: 650650 points

问题陈述

给定一个有根树,有 NN 个顶点,编号为 11NN。 顶点 11 是根,顶点 ii 的父顶点 (2iN)(2 \leq i \leq N) 是顶点 pip_i (pi<i)(p_i < i)。 此外,还有一个序列 A=(A1,A2,,AN)A = (A_1, A_2, \dots, A_N)

有根树的 哈希值 计算如下:

  • 定义 f(n)f(n) (1nN)(1 \leq n \leq N) 如下,按顺序 n=N,N1,,2,1n = N, N-1, \dots, 2, 1
    • 如果顶点 nn 是叶子,f(n)=Anf(n) = A_n
    • 如果顶点 nn 不是叶子,f(n)=An+cC(n)f(c)\displaystyle f(n) = A_n + \prod_{c \in C(n)} f(c),其中 C(n)C(n)nn 的子顶点集合。
  • 有根树的哈希值是 f(1)mod998244353f(1) \bmod{998244353}

按给定顺序处理 QQ 个查询。 每个查询提供 vvxx,因此更新 AvA_vxx,然后计算有根树的哈希值。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

You are given a rooted tree with NN vertices numbered 11 to NN.
Vertex 11 is the root, and the parent of vertex ii (2iN)(2 \leq i \leq N) is vertex pip_i (pi<i)(p_i < i).
Additionally, there is a sequence A=(A1,A2,,AN)A = (A_1, A_2, \dots, A_N).

The hash value of the rooted tree is calculated as follows:

  • Define f(n)f(n) (1nN)(1 \leq n \leq N) as follows in the order n=N,N1,,2,1n = N, N-1, \dots, 2, 1.
    • If vertex nn is a leaf, f(n)=Anf(n) = A_n.
    • If vertex nn is not a leaf, f(n)=An+cC(n)f(c)\displaystyle f(n) = A_n + \prod_{c \in C(n)} f(c), where C(n)C(n) is the set of children of nn.
  • The hash value of the rooted tree is f(1)mod998244353f(1) \bmod{998244353}.

Process QQ queries in the order they are given.
Each query provides vv and xx, so update AvA_v to xx and then compute the hash value of the rooted tree.

Constraints

  • 2N2×1052 \leq N \leq 2 \times 10^5
  • 1Q2×1051 \leq Q \leq 2 \times 10^5
  • 1pi<i1 \leq p_i < i
  • 0Ai<9982443530 \leq A_i < 998244353
  • 1vN1 \leq v \leq N
  • 0x<9982443530 \leq x < 998244353
  • All input values are integers.

Input

The input is given from Standard Input in the following format, where queryi\mathrm{query}_i represents the ii-th query:

NN QQ

p2p_2 p3p_3 \dots pNp_N

A1A_1 A2A_2 \dots ANA_N

query1\mathrm{query}_1

query2\mathrm{query}_2

\vdots

queryQ\mathrm{query}_Q

Each query is given in the following format:

vv xx

Output

Print QQ lines. The ii-th line should contain the answer to the ii-th query.

Sample Input 1

3 2
1 1
3 5 1
3 4
2 1

Sample Output 1

23
7

Initially, A=(3,5,1)A = (3, 5, 1).
The first query is processed as follows:

  • Update A3A_3 to 44. Now A=(3,5,4)A = (3, 5, 4).
  • The hash value of the rooted tree is calculated as follows to be 2323, which should be printed.
    • Vertex 33 has no children. Thus, f(3)=4f(3) = 4.
    • Vertex 22 has no children. Thus, f(2)=5f(2) = 5.
    • Vertex 11 has children 22 and 33. Thus, f(1)=3+5×4=23f(1) = 3 + 5 \times 4 = 23.
    • f(1)mod998244353=23f(1) \bmod{998244353} = 23 is the hash value of the rooted tree.

The second query is processed as follows:

  • Update A2A_2 to 11. Now A=(3,1,4)A = (3, 1, 4).
  • The hash value of the rooted tree is calculated as follows to be 77:
    • Vertex 33 has no children. Thus, f(3)=4f(3) = 4.
    • Vertex 22 has no children. Thus, f(2)=1f(2) = 1.
    • Vertex 11 has children 22 and 33. Thus, f(1)=3+1×4=7f(1) = 3 + 1 \times 4 = 7.
    • f(1)mod998244353=7f(1) \bmod{998244353} = 7 is the hash value of the rooted tree.

Sample Input 2

5 4
1 1 2 2
2 5 4 4 1
3 3
5 0
4 5
5 2

Sample Output 2

29
17
17
47

Sample Input 3

10 10
1 2 1 2 5 6 3 5 1
766294629 440423913 59187619 725560240 585990756 965580535 623321125 550925213 122410708 549392044
1 21524934
9 529970099
6 757265587
8 219853537
5 687675301
5 844033519
8 780395611
2 285523485
6 13801766
3 487663184

Sample Output 3

876873846
952166813
626349486
341294449
466546009
331098453
469507939
414882732
86695436
199797684
}