A2924 「BalkanOI 2023 Day2」Super Tree

暂无评定 传统题 BalkanOI(巴尔干半岛) 上传者
时间限制4000ms
内存限制1024MiB
通过 / 提交0/0

A2924「BalkanOI 2023 Day2」Super Tree

暂无评定 传统题 BalkanOI(巴尔干半岛) 上传者
时间限制4000ms
内存限制1024MiB
通过 / 提交0/0

Description

You are given a rooted tree with nn vertices, identified by indices 0,…,n−10, \ldots, n-1. The root has index 00. For each i∈{0,…,n−1}i \in\{0, \ldots, n-1\}, the vertex ii (i.e., the vertex with index ii ) has an integer aia_{i} assigned to it. Let fvf_{v} be the value of the bitwise AND⁡\operatorname{AND} (henceforth denoted by &\& ) of the values aia_{i} on the simple path from the vertex vv to the root. (Note that the simple path from a vertex xx to a vertex yy includes both xx and yy.) Let the power of the tree be the value of

∑0≤u,v<nfu⋅fv\sum_{0 \leq u, v<n} f_{u} \cdot f_{v}

and let the superpower of the tree be the value of (note the difference in ranges)

∑0≤u<v<nfu⋅fv\sum_{0 \leq u<v<n} f_{u} \cdot f_{v}

For a clarifying example, see the explanation of the sample test cases below.

We will say that a vertex uu belongs to the subtree of aa vertex vv if vv belongs to the simple path from the vertex uu to the root. Note that the subtree of a vertex xx includes the vertex xx itself.

You are presented with qq updates. Each update is described by two integers, vv and xx, and it requires you to set au:=au&xa_{u}:=a_{u} \& x for each vertex uu in the subtree of vertex vv. After each update, you should output the power and superpower of the current tree.

As output values can be large, print them modulo 109+710^{9}+7.

Input format

The first line of the input contains the integers nn and qq.

The second line of the input contains n−1n-1 integers, namely p1,p2,…,pn−1p_{1}, p_{2}, \ldots, p_{n-1}, which determine the structure of the tree. For each i∈{1,…,n−1}i \in\{1, \ldots, n-1\}, pip_{i} is the index of the parent of vertex ii, and it holds that 0≤pi<i0 \leq p_{i}<i.

The third line of the input contains nn integers, namely a0,a1,…,an−1a_{0}, a_{1}, \ldots, a_{n-1}. These are the values assigned to the vertices.

Each of the following qq lines contains two integers, vv (0≤v<n0 \leq v<n) and xx. These integers specify the individual updates.

Output format

Output q+1q+1 lines. Each line should contain two integers separated by a space. In the first line, print the power and the superpower (modulo 109+710^{9}+7) of the initial tree. In the ii-th line of the remaining qq lines (i∈{1,…,q}i \in\{1, \ldots, q\}), print the power and the superpower (modulo 109+710^{9}+7) of the tree after the ii-th update.

Input bounds

  • 1≤n,q≤1061 \leq n, q \leq 10^{6}.
  • 0≤ai<2600 \leq a_{i}<2^{60} for each i∈{0,…,n−1}i \in\{0, \ldots, n-1\}.
  • 0≤x<2600 \leq x<2^{60} for each update (v,x)(v, x).

Scoring

For a given test case, your solution will receive 50%50 \% of the score if it correctly computes all power values but incorrectly computes at least one superpower value for that test case.

Likewise, 50%50 \% of the score for a given test case will be awarded to a solution that correctly computes all superpower values for that test case but incorrectly computes at least one power value.

Subtasks

  1. (4 points) n=3n=3.
  2. (7 points) n,q≤700n, q \leq 700.
  3. (13 points) n,q≤5000n, q \leq 5000.
  4. (6 points) n≤105,pi=i−1n \leq 10^{5}, p_{i}=i-1 (for each i∈{1,…,n−1}i \in\{1, \ldots, n-1\} ), and ai,x<220a_{i}, x<2^{20} (for each i∈{0,…,n−1}i \in\{0, \ldots, n-1\} and for each update (v,x)(v, x) ).
  5. (7 points) pi=i−1p_{i}=i-1 (for each i∈{1,…,n−1}i \in\{1, \ldots, n-1\} ).
  6. (12 points) ai,x<220a_{i}, x<2^{20} (for each i∈{0,…,n−1}i \in\{0, \ldots, n-1\} and for each update (v,x)(v, x) ).
  7. (14 points) n≤105n \leq 10^{5}.
  8. (11 points) n≤5⋅105n \leq 5 \cdot 10^{5}.
  9. (26 points) No additional constraints.
3 3
0 0
7 3 4
1 6
2 2
0 3
196 61
169 50
81 14
25 6

Initially, we have

f0=7,f1=7&3=3,f2=7&4=4f_{0}=7, f_{1}=7 \& 3=3, f_{2}=7 \& 4=4

Therefore, the power of the tree is equal to

$$\begin{gathered} f_{0} \cdot f_{0}+f_{0} \cdot f_{1}+f_{0} \cdot f_{2}+f_{1} \cdot f_{0}+f_{1} \cdot f_{1}+f_{1} \cdot f_{2}+f_{2} \cdot f_{0}+f_{2} \cdot f_{1}+f_{2} \cdot f_{2}= \\ =7 \cdot 7+7 \cdot 3+7 \cdot 4+3 \cdot 7+3 \cdot 3+3 \cdot 4+4 \cdot 7+4 \cdot 3+4 \cdot 4=196 . \end{gathered}$$

The superpower is equal to

$$f_{0} \cdot f_{1}+f_{0} \cdot f_{2}+f_{1} \cdot f_{2}=7 \cdot 3+7 \cdot 4+3 \cdot 4=61 \text {. }$$

After the first update:

$$\begin{gathered} a_{0}=7, a_{1}=3 \& 6=2, a_{2}=4 ; \\ f_{0}=7, f_{1}=2, f_{2}=4 . \end{gathered}$$

After the second update:

$$\begin{gathered} a_{0}=7, a_{1}=2, a_{2}=4 \& 2=0 \\ f_{0}=7, f_{1}=2, f_{2}=0 \end{gathered}$$

After the third update:

$$\begin{gathered} a_{0}=7 \& 3=3, a_{1}=2 \& 3=2, a_{2}=0 \& 3=0 \\ f_{0}=3, f_{1}=2, f_{2}=0 \end{gathered}$$
4 2
0 0 1
6 5 6 2
1 2
0 3
256 84
144 36
16 4

Initially, we have

$$f_{0}=6, f_{1}=6 \& 5=4, f_{2}=6 \& 6=6, f_{3}=2 \& 5 \& 6=0$$

After the first update:

$$\begin{gathered} a_{0}=6, a_{1}=5 \& 2=0, a_{2}=6, a_{3}=2 \& 2=2 ; \\ f_{0}=6, f_{1}=0, f_{2}=6, f_{3}=2 \& 0=0 \end{gathered}$$

After the second update:

$$\begin{gathered} a_{0}=7, a_{1}=2, a_{2}=4 \& 2=0 \\ f_{0}=7, f_{1}=2, f_{2}=0 \end{gathered}$$
7 3
0 0 1 1 2 2
7 6 5 7 3 4 2
4 4
3 3
2 1
900 367
784 311
576 223
256 83