CF1863H.Goldberg Machine 3

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

There is a complete rooted binary tree, that is, a rooted tree in which each vertex has either 00 or 22 children. The root of the tree is vertex 11 . A node without children is called a leaf. Each leaf has a hunger value, we denote the hunger value of leaf vv by hvh_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 vv has at least hvh_v cookies in it. In this case, we say that the tree is filled up.

You have qq queries. Each query changes the value of hvh_v for some leaf vv . You need to print q+1q + 1 numbers, the ii -th of them being the minimum number of cookies required to fill up the machine after (i1)(i - 1) updates if you can pick any initial state for every selector. Since these numbers may be very large, print the answers modulo 998244353998\,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 ii -th query, you should also consider the effect of queries 1,2,,i11, 2, \ldots, i - 1 .

输入格式

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

The second line contains n1n - 1 integers p2,p3,,pnp_2, p_3, \ldots, p_n ( 1pi<i1\le p_i < i ), meaning that the parent of vertex ii is pip_i .

The third line contains nn integers h1,h2,,hnh_1, h_2, \ldots, h_n ( 0hi1090\le h_i\le 10^9 ) — the hunger values of vertices. If vertex ii is not a leaf, then hi=0h_i = 0 and this value is irrelevant. However, hi=0h_i = 0 may also hold if ii is a leaf.

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

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

It is guaranteed that the tree in the input is a full binary tree rooted at vertex 11 . It is also guaranteed that in each query vv is a leaf.

输出格式

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

输入输出样例

  • 输入#1

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

    输出#1

    0
    1
    2
    3
    7
    7022585

说明/提示

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 11 pointing to vertex 33 . Then one cookie will immediately descend to 33 .

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

After the third query, we choose the selector in vertex 11 pointing to vertex 22 and the selector in vertex 22 pointing to vertex 44 . Then the three cookies will descend via the paths 1241 \to 2 \to 4 , 131 \to 3 , 1251 \to 2 \to 5 .

After the fourth query, we choose the selector in vertex 11 pointing to vertex 33 . Regardless of the initial state of the selector in vertex 22 , after seven cookies are inserted, four of them will be in leaf 33 , and one or two cookies will be in each of the leaves 44 and 55 (note that exceeding the hunger value is allowed).

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

首页