Tree queries

5

1 votes
Datastructures, Segment Trees, XOR, Advanced Data Structures, Data Structures, Modular exponentiation, Segment tree, Modulo arithmetic
Problem

You are given a tree T with N nodes rooted at 1. The ith node of the tree has been assigned value Ai. You are required to handle two types of queries:

  • 1 v x: Update the value of node v to x, that is Av=x.
  • 2 v: Consider the multiset of values of nodes present in the subtree of node v. Consider all non-empty subsets of this set. Alice and Bob play a game on each of these subsets (independently) alternating turns.
    • For each subset, Alice starts the game. In each turn, the current player must choose some element of the current subset and reduce it to some non-negative element. The player who cannot make a move loses. Note that it is necessary to reduce or decrease the element.
    • You need to find the number of subsets where Bob is guaranteed to win. The number of subsets can be very large, output it modulo 1000000007(109+7)

Input format

  • The first line contains an integer that denotes the number of nodes in tree T.
  • The second line contains N space-separated integers denoting the initial value on the nodes of the tree(Ai)
  • (N1) lines follow. Each of these lines contains two space-separated integers u and v denoting that there is an edge between these two nodes.
  • The next line contains an integer Q denoting the number of queries.
  • Q lines follow. Each line is a query of the type 1 v x or 2 v.

Output format

For each query of type 2 v, print the number of non-empty subsets of the multiset of values present in a subtree of node v when Bob wins.

Constraints

1N5×1051Ai1061u,vNGiven edges form a tree1Q1051x106

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

We have N=4. The values on nodes are: A1=1,A2=2,A3=3,A4=3. Next, Q=3.

The first query is of the type 2 1. Consider the multiset of values of nodes present in subtree of node 1. The multiset is S={1, 2, 3, 3}. Consider the subset {1}. Alice starts the game and can reduce it to {0}. Now, Bob cannot reduce it further and so Alice wins. The 3 non-empty subsets of S where Bob wins are {1, 2, 3}, {1, 2, 3}, {3, 3}. Note that {1, 2, 3} is repeated twice because {3} appears twice. 

The second query is of the type 1 4 4. We update the value of A4 to 4. Therefore, the array becomes A=[1,2,3,4].

The third query is again of the type 2 1. Consider the multiset of values of nodes present in subtree of node 1. The multiset is S={1, 2, 3, 4}. There is only 1 non-empty subset of multiset S where Bob wins which is {1, 2, 3}.

Editor Image

?