Inaho VIII

View as PDF

Submit solution

Points: 20
Time limit: 1.4s
Memory limit: 512M

Author:
Problem types

Inaho was thinking of a tree problem, when he came up with this rather beautiful problem!

Given a tree originally rooted at 1 containing N nodes each with a value vi and an arbitrary value K, support Q of the following operations:

  • 1 R Reroot the tree so that node R is the root.
  • 2 a b Print the highest common ancestor of nodes a and b.
  • 3 a b Print the sum of all nodes' vi on the path from a to b, inclusive.
  • 4 a b Print the product of all nodes' vi on the path from a to b, inclusive, modulo 109+7.
  • 5 a b Print the minimum of all nodes' vi on the path from a to b, inclusive.
  • 6 a b Print the maximum of all nodes' vi on the path from a to b, inclusive.
  • 7 a b Print the greatest common divisor of all nodes' vi on the path from a to b, inclusive.
  • 8 a b Print the bitwise AND of all nodes' vi on the path from a to b, inclusive.
  • 9 a b Print the bitwise OR of all nodes' vi on the path from a to b, inclusive.
  • 10 a b Print the bitwise XOR of all nodes' vi on the path from a to b, inclusive.
  • 11 a b Print the number of nodes whose vi>K on the path from a to b, inclusive.
  • 12 a b Print the number of nodes whose vi<K on the path from a to b, inclusive.
  • 13 a b Print the value vi that minimizes viK, and vi>K of all nodes on the path from a to b, inclusive. Print K if there is no such node where vi>K.
  • 14 a b Print the value vi that minimizes Kvi, and vi<K of all nodes on the path from a to b, inclusive. Print K if there is no such node where vi<K.

It is guaranteed 1a,b,RN.

Input Specification

The first line will contain three space-separated integers, N,Q,K (1N,Q105,1K1000), the number of nodes, the number of operations, and the arbitrary value K, respectively.

The second line will contain N space-separated integers, v1,v2,,vN (1vi1000), the values of each node.

The next N1 lines will each contain two space-separated integers, a,b (1a,bN), indicating that nodes a and b are connected by an edge. It is guaranteed the entire tree is connected.

The next Q lines will each contain a valid operation as defined above.

Output Specification

For each operation that requires something to be outputted (everything except operation 1), print the answer on its own line.

Sample Input

Copy
6 15 3
4 10 2 2 5 1
1 2
1 3
3 4
3 5
3 6
2 1 2
1 3
2 1 2
3 2 5
4 4 1
5 1 6
6 3 5
7 2 3
8 3 4
9 5 3
10 6 2
11 2 6
12 3 1
13 4 5
14 1 2

Sample Output

Copy
1
3
21
16
1
5
2
2
7
13
2
1
5
3

Comments

There are no comments at the moment.