#P10180. 半彩三重奏

半彩三重奏

Background

FanFan is not satisfied with building only a deep-blue tree. He thinks a tree with only deep blue is too monotonous, so he wants to dye this tree with colors.

Problem Description

Since it is now the fantasy Year of the Dragon, FanFan’s deep-blue tree has been dyed with colors, and the color of node ii is aia_i.

FanFan is someone who changes his mind easily. In the next qq days, he will change the colors he likes every day. On day ii, the two colors he likes are xi,yix_i, y_i (xiyix_i \neq y_i).

But to take care of his tree, he needs to move on the tree often, and he will only pass through colors he likes.

Specifically, on day ii, FanFan will choose an ordered pair of nodes (u,v)(u,v), then move along the unique simple path uvu \to v, and all nodes on the path (including uu and vv) must have colors in {xi,yi}\{x_i, y_i\} (uu may be equal to vv). After that, he can do one Yoimiya pull.

Every day, you need to pull a C6 Yoimiya, and then tell FanFan how many possible ordered node pairs he can choose.

Input Format

The first line contains two positive integers n,qn, q.

The next line contains nn positive integers a1,a2,,ana_1, a_2, \cdots, a_n, representing the color of each node.

The next line contains n1n-1 positive integers p2,p3,,pnp_2, p_3, \cdots, p_n, meaning that for all 2in2 \le i \le n, there is an edge (pi,i)(p_i, i) in the tree.

The next qq lines each contain two positive integers xi,yix_i, y_i.

Output Format

For each query, output one positive integer per line representing the answer.

5 3
1 2 1 3 3
1 1 2 2
1 2
1 3
2 3
9
6
9

Hint

Sample 11 Explanation

The shape of the tree is shown in the figure:

For the first query, the only valid paths are paths from one node to another among {1,2,3}\{1,2,3\}.

For the second query, the only valid paths are $1 \to 1, 1 \to 3, 3 \to 1, 3 \to 3, 4 \to 4, 5 \to 5$.

Subtask Constraints

This problem uses bundled subtasks.

For 100%100\% of the data, 1n1061 \le n \le 10^6, 1q2×1061 \le q \le 2 \times 10^6, 1ai,x,yn1 \le a_i, x, y \le n, xyx \neq y. It is guaranteed that pi<ip_i < i.

Subtask ID nn qq Special Property Score Dependencies
Subtask #1 100\le 100 None 77 None
Subtask #2 2000\le 2000 1818 11
Subtask #3 105\le 10^5 2×105\le 2\times 10^5 A 55 None
Subtask #4 B 1919
Subtask #5 None 2121 1,2,3,41,2,3,4
Subtask #6 2×105\le 2\times 10^5 5×105\le 5\times 10^5 1010 55
Subtask #7 106\le 10^6 2×106\le 2\times 10^6 2020 66

Special Property A: pi=1p_i = 1.

Special Property B: pi=i1p_i = i - 1.

Translated by ChatGPT 5