News
Time limit2sMemory limit1024 MB
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 workers, numbered from to . The company structure is strictly hierarchical: every worker except number has exactly one direct supervisor. So every worker has at least one subordinate (direct or indirect), including himself. For example, worker has exactly subordinates, including himself. Of course, there is no situation where some subordinate of a worker is his direct supervisor. For some worker , call a -level subordinate. Then his direct subordinates are called -level subordinates of . All of their direct subordinates (which are indirect subordinates of ) are called -level subordinates of , 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 and number , and tells the news to all -level, -level (if they exist), ..., -level (if they exist) subordinates of . Call all these subordinates the -subordinates of . 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 -subordinates of 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 , the number of workers in Deni's company. From each of the next lines read two integers and , which show that worker is a direct subordinate of worker . From the next line read integers , where is if worker knows the news at the beginning and otherwise. From the next line read one integer , the number of queries. From each of the last lines, read queries of two types:
- type (news announcement query): – Deni tells the news to all the -subordinates of .
- type (question query): – Deni asks for the number of workers that know the news among the -subordinates of .
Output
For every query of type , on separate lines in the same order as in the input, write one integer: the answer to the corresponding question.
Constraints
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 :
The -level subordinate of worker is , the -level subordinates of worker are workers and , the -level subordinates of worker are and , and there are no -level or -level subordinates of worker . Workers , , and know the news, so the answer to this question query is .
For query :
The -subordinates of worker are workers , , and . Workers and already know the news, so only worker learns the news at this time.
For the second query :
The -subordinates of worker are , , , , and . Workers , , , and know the news, so the answer to this query is .