Pinely Round 2 (Div. 1 + Div. 2)
H
Goldberg Machine 3
Codeforces
3500
原题
← GI →
翻译中…

There is a complete rooted binary tree, that is, a rooted tree in which each vertex has either $0$ or $2$ children. The root of the tree is vertex $1$. A node without children is called a leaf. Each leaf has a hunger value, we denote the hunger value of leaf $v$ by $h_v$.

翻译中…

Each inner vertex of the tree has a selector pointing to one of the children of the vertex.

翻译中…

This tree accepts cookies. Before launching the process you can choose the initial state of each selector individually. The process is as follows:

翻译中…
  • Initially there are no cookies in vertices.
  • You insert cookies into the root one by one.
  • As long as the cookie is not in a leaf, it falls to the child defined by the selector in the current vertex. This selector then changes its state to the opposite one, i. e. it starts pointing to the other child of the vertex.
  • You stop inserting cookies when each leaf $v$ has at least $h_v$ cookies in it. In this case, we say that the tree is filled up.
翻译中…

You have $q$ queries. Each query changes the value of $h_v$ for some leaf $v$. You need to print $q + 1$ numbers, the $i$-th of them being the minimum number of cookies required to fill up the machine after $(i - 1)$ updates if you can pick any initial state for every selector. Since these numbers may be very large, print the answers modulo $998\,244\,353$.

翻译中…

Please note that you can choose the initial state of all selectors independently between queries. However, the queries themselves are not independent: when answering the $i$-th query, you should also consider the effect of queries $1, 2, \ldots, i - 1$.

翻译中…

Input

翻译中…

The first line contains a single integer $n$ ($1\le n \lt 200\,000$) — the number of vertices in the tree.

翻译中…

The second line contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1\le p_i \lt i$), meaning that the parent of vertex $i$ is $p_i$.

翻译中…

The third line contains $n$ integers $h_1, h_2, \ldots, h_n$ ($0\le h_i\le 10^9$) — the hunger values of vertices. If vertex $i$ is not a leaf, then $h_i = 0$ and this value is irrelevant. However, $h_i = 0$ may also hold if $i$ is a leaf.

翻译中…

The fourth line contains a single integer $q$ ($0\le q\le 200\,000$) — the number of queries.

翻译中…

Each of the next $q$ lines contains two integers $v$ and $x$ ($1\le v\le n$, $0\le x\le 10^9$), meaning that the hunger value of vertex $v$ is set to $x$.

翻译中…

It is guaranteed that the tree in the input is a full binary tree rooted at vertex $1$. It is also guaranteed that in each query $v$ is a leaf.

翻译中…

Output

翻译中…

Output $q + 1$ integers, $i$-th of them being the minimum number of cookies needed to fill up the machine after $(i - 1)$ updates, modulo $998\,244\,353$.

翻译中…

Note

翻译中…

Consider the example. Before any queries are made, no cookies need to be inserted, since all hunger values of zero are trivially satisfied.

翻译中…

After the first query, we can choose the selector in vertex $1$ pointing to vertex $3$. Then one cookie will immediately descend to $3$.

翻译中…

After the second query, we choose the selector in vertex $1$ pointing to vertex $3$ and the selector in vertex $2$ pointing to vertex $4$. The first cookie will drop down to $3$ and change the state of the selector in vertex $1$: now it is pointing to $2$. The second cookie will go via the path $1 \to 2 \to 4$.

翻译中…

After the third query, we choose the selector in vertex $1$ pointing to vertex $2$ and the selector in vertex $2$ pointing to vertex $4$. Then the three cookies will descend via the paths $1 \to 2 \to 4$, $1 \to 3$, $1 \to 2 \to 5$.

翻译中…

After the fourth query, we choose the selector in vertex $1$ pointing to vertex $3$. Regardless of the initial state of the selector in vertex $2$, after seven cookies are inserted, four of them will be in leaf $3$, and one or two cookies will be in each of the leaves $4$ and $5$ (note that exceeding the hunger value is allowed).

翻译中…

The answer after the fifth query is $3\,999\,999\,997$. Do not forget to print the answer modulo $998\,244\,353$.

翻译中…

样例

Input #1
5
1 1 2 2
0 0 0 0 0
5
3 1
4 1
5 1
3 4
4 1000000000
Output #1
0
1
2
3
7
7022585
InputCopy OutputCopy
题目信息
题号
1863H
时间限制
8 s
内存限制
1024 MB
标签
动态规划
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。