#P7357. 「PMOI-1」中位数
「PMOI-1」中位数
Problem Description
Given a rooted tree with nodes, rooted at . The -th node has a node weight .
Define the function as the median of the multiset of node weights of all nodes on the path from to .
Note that the definition of median in this problem is slightly different from the standard mathematical one: for a sequence of length , its median is defined as the -th smallest number in the sequence.
Define the function to indicate whether the path from to completely covers the path from to . If it completely covers, then ; otherwise, .
You need to process operations in order, in the following formats:
1 u: an update operation, where you need to XOR the weight of node with ;
2 u v: a query, where you need to compute $\max\limits_{1\le i\le n,1\le j\le n}\{\text{cover}(i,j,u,v)f(i,j)\}$.
For the second type of operation, it is guaranteed that in every query, is not an ancestor of and is not an ancestor of , and .
For each operation of the second type, output the corresponding value.
Input Format
The first line contains two positive integers and , representing the number of nodes in the tree and the number of queries.
The second line contains integers, where the -th number is the node weight .
The next lines each contain two integers , describing an edge connecting and .
The next lines each start with an integer , indicating whether this is an update or a query. If , this is an update, and it is followed by an integer . If , this is a query, and it is followed by two integers . The specific meanings are described in the [Description].
Output Format
For each query, output one line containing the answer.
8 3
4 2 3 4 2 1 4 3
1 2
1 3
2 4
2 5
3 6
6 7
6 8
2 4 8
1 3
2 2 3
3
4
Hint
[Sample Explanation]
The first operation is a query. Clearly, only satisfies . In this case, .
The second operation is an update. The node weight of node is changed to .
The third operation is a query. When , and . It is not hard to see that for all other node pairs satisfying , none has .
[Constraints]
- Subtask 1 (8 pts): ;
- Subtask 2 (12 pts): ;
- Subtask 3 (16 pts): ;
- Subtask 4 (10 pts): the shape of the tree is guaranteed to be generated randomly;
- Subtask 5 (12 pts): it is guaranteed that there are no type operations;
- Subtask 6 (12 pts): it is guaranteed that in each query, both and are leaf nodes;
- Subtask 7 (30 pts): no special restrictions.
The random method in Subtask 4 is: randomly generate a permutation . For , connect to a uniformly random node among .
For of the testdata, . It is guaranteed that every query satisfies that is not an ancestor of and is not an ancestor of , and .
Translated by ChatGPT 5