Operations and tree

5

4 votes
Advanced Data Structures, Data Structures, Fenwick Tree, Medium
Problem

Yuqi has an rooted indexed tree T of N nodes indexed from 1 to N. The node 1 is the root. Now on each node there is a value Vi .

He can cast several kinds of magic:

  • Magic Kind 1: given a,b increase the value in the subtree of node a by b. (1aN,0|b|109)

  • Magic Kind 2: given a,b increase the value of node a by b (1aN,0|b|109)

  • Magic Kind 3: given a if a is in the Blacklist remove it, otherwise add it into the blacklist. (1aN)

  • Magic Kind 4: given a.tell Yuqi the value of node a. (1aN)

Nodes in Blacklist's value will not be affected by any other magic until it's removed.

Now he gives you the list of magic to proceed. You need to proceed them in order. Note that value of any node can be negative at any moment.

Input Format

The first line contains one integer N (1N105)

The next N-1 lines each contains two numbers x,y (1x,yN) indicating an edge.

The next line contains N integers, the array V. (1Vi109)

The following line contains one integer q - the number of magic to proceed.(1q105)

Then next q lines can be:

  • 1 a b Magic Kind 1

  • 2 a b Magic Kind 2

  • 3 a Magic Kind 3

  • 4 a Magic Kind 4

Output Format

For each magic 4, output the answer. 

Subtasks

The author will warmly give you some hints here. 

In some testcases, the tree is a chain.(ie. for each 2xn.,there exists an edge connecting x and x-1)

In some other testcases, the depth of the tree is (ie. no edge connects two non-root element)

 

Sample Input
4
1 2
1 3
2 4
1 2 3 4
7
3 3
1 1 1
2 2 2
4 1
4 2
4 3
4 4
Sample Output
2
5
3
5
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

The tree looks like follow:

tree

the node 3 is in blacklist from the beginning to the end, so the value of it never changes.

The node #1 and #4 get +1 in operation 2.

The node #2 gets +1 in operation 2 and +2 in operation 3.

Hints:

Subtasks

The author will warmly give you some hints here. 

In some testcases, the tree is a chain.(ie. for each 2xn.,there exists an edge connecting x and x-1)

In some other testcases, the depth of the tree is (ie. no edge connects two non-root element)

Editor Image

?