This page is still under construction.

Parts of this page are still being built. What you see may change.

News

Time limit2sMemory limit1024 MB

Summary
On a rooted tree, range updates and range count queries target nodes within k levels of a given node; both must be answered online over up to 2*10^5 operations.
Level

Hard8 of 10

Topics
Tree, DFS, Segment tree, Sorting
Solved
No attempts yet

Problem

Deni is the boss of a company with NN workers, numbered from 11 to NN. The company structure is strictly hierarchical: every worker except number 11 has exactly one direct supervisor. So every worker has at least one subordinate (direct or indirect), including himself. For example, worker 11 has exactly NN subordinates, including himself. Of course, there is no situation where some subordinate of a worker is his direct supervisor. For some worker xx, call xx a 00-level subordinate. Then his direct subordinates are called 11-level subordinates of xx. All of their direct subordinates (which are indirect subordinates of xx) are called 22-level subordinates of xx, and so on.

Some of the workers know a breaking piece of news. Deni wants to inform all the company employees. So, multiple times she chooses worker xx and number kk, and tells the news to all 00-level, 11-level (if they exist), ..., kk-level (if they exist) subordinates of xx. Call all these subordinates the kk-subordinates of xx. The problem with this type of announcement is that most of the time, many chosen subordinates already know the piece of news. That is why Deni wants a system that can tell her the number of workers among all the kk-subordinates of xx that have already learned about the news. Write a program that can help her.

Input

From the first line of the standard input read one integer NN, the number of workers in Deni's company. From each of the next N−1N-1 lines read two integers xx and yy, which show that worker yy is a direct subordinate of worker xx. From the next line read NN integers b1,b2,…,bNb_1, b_2, \ldots, b_N, where bib_i is 11 if worker ii knows the news at the beginning and 00 otherwise. From the next line read one integer QQ, the number of queries. From each of the last QQ lines, read queries of two types:

  • type 11 (news announcement query): 11 xx kk – Deni tells the news to all the kk-subordinates of xx.
  • type 22 (question query): 22 xx kk – Deni asks for the number of workers that know the news among the kk-subordinates of xx.

Output

For every query of type 22, on separate lines in the same order as in the input, write one integer: the answer to the corresponding question.

Constraints

  • 2≤N≤2×1052 ≤ N ≤ 2 × 10^5
  • 1≤Q≤2×1051 ≤ Q ≤ 2 × 10^5
  • 0≤k≤N0 ≤ k ≤ N

Hint

The picture above shows the hierarchy of the company, and the workers that know the news at the beginning are colored orange.

For the first query 22 44 44:

The 00-level subordinate of worker 44 is 44, the 11-level subordinates of worker 44 are workers 77 and 88, the 22-level subordinates of worker 44 are 99 and 1010, and there are no 33-level or 44-level subordinates of worker 44. Workers 44, 88, and 1010 know the news, so the answer to this question query is 33.

For query 11 44 11:

The 11-subordinates of worker 44 are workers 44, 77, and 88. Workers 44 and 88 already know the news, so only worker 77 learns the news at this time.

For the second query 22 44 44:

The 44-subordinates of worker 44 are 44, 77, 88, 99, and 1010. Workers 44, 77, 88, and 1010 know the news, so the answer to this query is 44.

Examples1

  1. Example 1

    Input
    10
    1 2
    1 3
    3 4
    3 5
    3 6
    4 7
    4 8
    8 9
    8 10
    0 1 0 1 0 1 0 1 0 1
    9
    2 1 1
    2 4 4
    2 3 0
    1 1 2
    2 3 4
    1 4 1
    2 1 1
    2 4 4
    2 3 2
    
    Expected output
    1
    3
    0
    6
    3
    4
    6