Meow

시간 제한0.5초메모리 제한1024 MB

요약
루트 있는 트리에서 값을 한 점씩 Q번 바꾸면서, 값이 1부터 L까지 순서대로 늘어선 조상 사슬의 개수를 세고 그 개수들의 가중 합을 구한다.
난이도

어려움10점 중 8점

유형
트리, 동적 계획법, 구현, DFS
정답자
아직 제출이 없습니다

문제

In the CS club there's a new Pokemon, Meow2. Being passionate about trees, Meow2 has a rooted tree with NN nodes, labeled from 00 to N−1N-1. Node 00 is the root of the tree, and every for every other node ii its father has a label smaller than ii. Each node has an associated value, an integer between 11 and LL.

Meow2 also has an array S=\[1,2,…,L]S=\[1,2,\dots ,L] of length LL. He wants to know the number of occurrences of SS in the tree. More exactly, he wants to count the number of sequences A_1,A_2,…,A_LA\_1,A\_2,\dots ,A\_L​ such that the value associated with node A_i=iA\_i=i, and for each 1≤i\<L1≤i\<L node A_iA\_i is an ancestor of node A_i+1A\_{i+1}.

Being an ever evolving Pokemon, Meow2 keeps changing the initial tree. He has a magical array of changes, PP, of length QQ. At each step ii, 0≤i\<Q0≤i\<Q, he changes the value of node ii\\%N to P_iP\_i, 1≤P_i≤L1≤P\_i≤L.

Meow2 would like to know after each change the number of occurrences of SS in the tree, as defined above. If we denote by ans_ians\_i the number of occurrences of SS after the iith change, you should find:

  • O=1×ans_0+2×ans_i+⋯+Q×ans_Q−1O=1 \times ans\_0+2 \times ans\_i+\dots +Q \times ans\_{Q-1}

입력

The first line contains 33 integers NN, LL and QQ.

The second line contains an array FF of length N−1N-1, where F_iF\_i is the father of node ii.

The third line contains an array of length NN, representing the initial values of the nodes.

The next QQ lines contains an integer each, representing the changes made on the tree.

출력

Output a single integer OO modulo 109+710^9+7.

제한

  • 1≤N≤1051≤N≤10^5
  • 1≤L≤N1≤L≤N
  • 1≤Q≤2×1051≤Q≤2 \times 10^5

힌트

The individual answers are: 0,0,1,1,2,20,0,1,1,2,2

Below you can see the tree after the first update

예제1

  1. 예제 1

    입력
    6 2 6
    0 1 0 3 0
    1 2 1 2 1 2
    2
    1
    2
    1
    2
    1
    
    예상 출력
    29