Sweets

시간 제한3초메모리 제한2048 MB

요약
루트가 있는 트리에서 각 시장의 학습 수치가 갱신될 때마다, 루트에서 임의의 노드까지 가는 경로에서 성공할 수 있는 시장 수의 최댓값을 구한다.
난이도

어려움10점 중 9점

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

문제

Sandu graduated from high school and decided to pursue his passion as a candy salesperson.

Balti, a city in Moldova, has NN markets, which are connected with streets between them. The marketplace has an interesting structure. Each market can be accessed from any other market by traveling through some number of streets, and there are exactly N−1N - 1 streets. Also, Sandu is currently staying at market 11. So, the markets form a rooted tree structure where market 11 is the root.

Additionally, each market ii has a toughness level t_it\_i and a learning level l_il\_i. Initially the learning level of each market is 00, and Sandu has a selling skill level of 00.

When Sandu visits market ii, his selling skill level increases by l_il\_i. Sandu has success at market ii if his selling skill level is at least t_it\_i (the market's toughness level). Note that Sandu's selling skill level increases as soon as he enters the market ii, regardless of whether he was successful or not. This means his selling skill level increases before trying to do anything inside the market.

Also, as Balti is a really busy city, on each of the following QQ days there will be an event happening. On day jj, event jj will happen. An event is described by two positive integers - u_ju\_j and x_jx\_j meaning that on day jj, there will be an event at the market u_ju\_j and the learning level for the corresponding market will be permanently increased by x_jx\_j. In other words, event jj means that on day jj the learning level will be increased by x_jx\_j (l_u_j:=l_u_j+x_jl\_{u\_j} := l\_{u\_j} + x\_j).

Sandu plans to visit some markets and sell candies in them. He will pick some market kk and will visit all markets on the path from the first market to market kk, in that order. Sandu wants to succeed at as many markets as possible. He will continue his journey towards market kk regardless of whether he was successful or not. Additionally, every day, Sandu starts at market 11 and his selling skill level resets, starting each day with a selling skill level of 00.

For each day jj, help Sandu find the largest number of markets he can be successful at, if he optimally picks the location of the final market of that day.

입력

The first line of input contains two integers NN and QQ (1≤N,Q≤5⋅1051 ≤ N,Q ≤ 5 \cdot 10^5).

The second line contains N−1N - 1 integers that represent the rooted tree structure of the markets: p_2,…,p_Np\_2 , \dots , p\_N, meaning that there exists an edge between p_ip\_i and ii, and p_ip\_i is the parent of ii.

Additionally for each ii, the condition 1≤p_i<i1 ≤ p\_i < i is always satisfied.

The third line contains NN integers: t_1,t_2,…,t_Nt\_1 , t\_2 , \dots , t\_N (0≤t_i≤1090 ≤ t\_i ≤ 10^9) — the toughness level of the given markets.

Then, QQ lines follow, representing the events happening on day j=1,2,...,Qj = 1, 2,...,Q.

Line jj contains two integers — u_ju\_j and x_jx\_j describing the event for jj th day (1≤u_j≤N1 ≤ u\_j ≤ N, 1≤x_j≤1091 ≤ x\_j ≤ 10^9).

출력

Output QQ lines - in the jj-th line you should output the answer for jj-th day.

제한

  • 1≤N,Q≤5⋅1051 ≤ N,Q ≤ 5 \cdot 10^5.
  • 1≤p_i<i1 ≤ p\_i < i is always satisfied.
  • 0≤t_i≤1090 ≤ t\_i ≤ 10^9 for all ii (1≤i≤N1 ≤ i ≤ N).
  • 1≤u_j≤N1 ≤ u\_j ≤ N for all jj (1≤j≤Q1 ≤ j ≤ Q).
  • 1≤x_j≤1091 ≤ x\_j ≤ 10^9 for all jj (1≤j≤Q1 ≤ j ≤ Q).

예제3

  1. 예제 1

    입력
    12 5
    1 1 3 3 1 6 7 1 9 10 11
    1 2 6 3 5 4 6 5 2 3 4 5
    1 1
    1 1
    3 2
    6 3
    9 6
    
    예상 출력
    1
    2
    2
    3
    5
    
  2. 예제 2

    입력
    5 4
    1 2 3 4
    1 2 5 6 7
    1 1
    1 2
    1 1
    1 2
    
    예상 출력
    1
    2
    2
    4
    
  3. 예제 3

    입력
    5 5
    1 1 1 1
    1 2 3 4 5
    4 4
    2 2
    5 5
    1 1
    3 3
    
    예상 출력
    1
    1
    1
    2
    2