Christmas Tree Ornament

Time limit1sMemory limit1024 MB

Summary
A tree of N lamps is built incrementally and recolored M times; after each recolor, report how many edges join two lamps of equal color.
Level

Medium4 of 10

Topics
Tree, Implementation
Solved
No attempts yet

Problem

Byteland Party Factory is preparing to launch a new Christmas-tree ornament. To build the prototype, two lamps were first connected to each other with a wire, and then, (N−2)(N - 2) times, a new lamp was taken and connected with a wire to one of the already existing lamps. The result is an ornament made of NN colored lamps. The factory stocks KK different lamp colors.

Once the first prototype was ready, it was handed over to the decoration department. There it was decided that a good measure of the ornament's beauty is the number of wires that connect two lamps of the same color. The department then replaced one existing lamp with another MM times, and after each replacement they wanted to know the beauty of the resulting ornament.

Write a program that, given the ornament's initial prototype and the list of replacements made by the decoration department, computes the beauty of the ornament after each replacement.

Input

The first line of the input contains three integers: the number of lamps in the ornament NN (2≤N≤300 0002 \le N \le 300\,000), the number of replacements made by the decoration department MM (1≤M≤300 0001 \le M \le 300\,000), and the number of possible lamp colors KK (1≤K≤1091 \le K \le 10^9).

The second line contains NN integers AiA_i (1≤Ai≤K1 \le A_i \le K), giving the colors of the lamps in the order they were added to the ornament.

The third line contains N−2N - 2 integers PiP_i (1≤Pi≤i+11 \le P_i \le i + 1), where PiP_i tells which lamp the lamp numbered (i+2)(i + 2) was connected to.

Each of the following MM lines contains two integers XiX_i and YiY_i (1≤Xi≤N1 \le X_i \le N, 1≤Yi≤K1 \le Y_i \le K), meaning that in the ii-th replacement the lamp XiX_i was replaced by a lamp of color YiY_i.

The lamps are numbered 11 to NN in the order they were added; lamps 11 and 22 are the initial pair joined by a wire.

Output

Output exactly MM lines. On line ii, print the number of lamp pairs that are joined by a wire and have the same color in the configuration after the ii-th replacement.

Examples2

  1. Example 1

    Input
    3 3 3
    1 2 3
    2
    2 1
    3 1
    2 2
    
    Expected output
    1
    2
    0
    
  2. Example 2

    Input
    7 1 4
    2 1 2 4 4 1 2
    1 1 2 1 2
    2 2
    
    Expected output
    3