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 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 hv .
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 hv cookies in it. In this case, we say that the tree is filled up.
You have q queries. Each query changes the value of hv 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 998244353 .
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,…,i−1 .
输入格式
The first line contains a single integer n ( 1≤n<200000 ) — the number of vertices in the tree.
The second line contains n−1 integers p2,p3,…,pn ( 1≤pi<i ), meaning that the parent of vertex i is pi .
The third line contains n integers h1,h2,…,hn ( 0≤hi≤109 ) — the hunger values of vertices. If vertex i is not a leaf, then hi=0 and this value is irrelevant. However, hi=0 may also hold if i is a leaf.
The fourth line contains a single integer q ( 0≤q≤200000 ) — the number of queries.
Each of the next q lines contains two integers v and x ( 1≤v≤n , 0≤x≤109 ), 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 q+1 integers, i -th of them being the minimum number of cookies needed to fill up the machine after (i−1) updates, modulo 998244353 .
输入输出样例
输入#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 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→2→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→2→4 , 1→3 , 1→2→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 3999999997 . Do not forget to print the answer modulo 998244353 .