stablo — COCI CONTEST_#3
Task type: batch | Time limit: 2 s | Memory limit: 512 MB | Difficulty: 16
Algorithms: lowest common ancestor

# Task Stablo

Toni decided to create a task for HONI (and COCI). Since he doesn't like kids, he decided to make the task as difficult as possible. He came up with a complex problem involving a tree that constantly changes, solely to make contestants suffer as much as possible.

You are given a weightless tree with $N$ nodes, where the root of the tree is node 1. Each node has an associated value $v[i]$. The structure of the tree is defined using an array $p[i]$, where for each $i$ from 1 to $N - 1$, $p[i]$ denotes the parent of $i + 1$.

A function $f(y)$ is defined for a node $y$ in the tree as:

$$f(y) = \sum_{x \in S_y} d(x, y) \cdot v[x]$$

where $d(x, y)$ denotes the distance between nodes $x$ and $y$, while $S_y$ contains all nodes for which $y$ is an ancestor.

You are given $Q$ queries with two nodes $x$ and $y$. For each query, the following transformation must be simulated in the tree, and the function $f(y)$ needs to be calculated:

1. Attach all nodes for which $x$ is the parent to the parent of $x$
2. Remove $x$ from the tree
3. Insert node $x$ back into the tree, between $y$ and the descendant of $y$ from whose subtree $x$ was removed.

If $y$ is the parent of $x$, the tree remains unchanged. It is always true that $x$ is in the subtree of $y$. For each query, the value of $f(y)$ must be calculated after the tree is temporarily modified according to the procedure described above. The tree modifications are not permanent, i.e. after each query, the tree returns to its original state.

## Input

The first line contains two integers $N$ and $Q$ ($1 \le N, Q \le 5 \cdot 10^5$), the number of nodes in the tree and the number of queries, respectively.

The second line contains $N$ integers $v[i]$ ($1 \le v[i] \le 10^6$), representing the value of each node.

The third line contains $N - 1$ integers $p[i]$ ($1 \le p[i] \le i$), where $p[i]$ denotes the parent of node $i + 1$.

Each of the next $Q$ lines contains two integers $x$ and $y$ ($1 \le x, y \le N$), denoting the nodes involved in the operation described above.

## Output

In the next $Q$ lines output the value of the function $f(y)$ on the modified tree.

## Scoring

| Subtask | Points | Constraints                                           |
|---------|--------|-------------------------------------------------------|
| 1       | 21     | $1 \le N, Q \le 1000$                                |
| 2       | 37     | Tree is a chain, $p[i] = i$ for every $i$ from 1 to $N - 1$ |
| 3       | 22     | Each node will be the parent of at most 20 nodes      |
| 4       | 40     | No additional constraints.                            |

## Examples

| Input                                     | Input                                                   | Input                               |
|-------------------------------------------|---------------------------------------------------------|-------------------------------------|
| `3 1`                                     | `3 2`                                     | `5 3`                                 |
| `1 2 3`                                     | `4 5 6`                                     | `2 5 2 2 2`                             |
| `1 2`                                     | `1 1`                                     | `1 2 3 2`                             |
| `3 1`                                     | `2 1`                                     | `4 3`                                 |
|                                           | `3 1`                                     | `3 2`                                 |
| Output                                    | Output                                    | `5 1`                                 |
| `7`                                       | `11`                                      | Output                                |
|                                           | `11`                                      | `2`                                   |
|                                           |                                           | `8`                                   |
|                                           |                                           | `26`                                  |

**Clarification of the first example:** After applying the operation on a tree, node 3 is at a distance of 1 from node 1, and node 2 is at a distance of 2 from node 1. The result is $3 + 2 \cdot 2 = 7$.

Starter code: