#P7357. 「PMOI-1」中位数

    ID: 8146 远端评测题 3000ms 128MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>树形数据结构二分O2优化可持久化线段树可持久化

「PMOI-1」中位数

Problem Description

Given a rooted tree with nn nodes, rooted at 11. The ii-th node has a node weight aia_i.

Define the function f(u,v)f(u,v) as the median of the multiset of node weights of all nodes on the path from uu to vv.

Note that the definition of median in this problem is slightly different from the standard mathematical one: for a sequence of length tt, its median is defined as the t+12\left\lceil\frac{t+1}2\right\rceil-th smallest number in the sequence.

Define the function cover(x1,y1,x2,y2)\text{cover}(x_1,y_1,x_2,y_2) to indicate whether the path from x1x_1 to y1y_1 completely covers the path from x2x_2 to y2y_2. If it completely covers, then cover(x1,y1,x2,y2)=1\text{cover}(x_1,y_1,x_2,y_2)=1; otherwise, cover(x1,y1,x2,y2)=0\text{cover}(x_1,y_1,x_2,y_2)=0.

You need to process qq operations in order, in the following formats:

1 u: an update operation, where you need to XOR the weight of node uu with 11;

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, uu is not an ancestor of vv and vv is not an ancestor of uu, and uvu \neq v.

For each operation of the second type, output the corresponding value.

Input Format

The first line contains two positive integers nn and qq, representing the number of nodes in the tree and the number of queries.

The second line contains nn integers, where the ii-th number is the node weight aia_i.

The next n1n-1 lines each contain two integers x,yx,y, describing an edge connecting xx and yy.

The next qq lines each start with an integer optopt, indicating whether this is an update or a query. If opt=1opt=1, this is an update, and it is followed by an integer uu. If opt=2opt=2, this is a query, and it is followed by two integers u,vu,v. 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 (i=4,j=8)(i=4,j=8) satisfies cover(i,j,u,v)=1\text{cover}(i,j,u,v)=1. In this case, f(i,j)=3f(i,j)=3.

The second operation is an update. The node weight of node 33 is changed to 22.

The third operation is a query. When i=4,j=3i=4,j=3, cover(i,j,u,v)=1\text{cover}(i,j,u,v)=1 and f(i,j)=4f(i,j)=4. It is not hard to see that for all other node pairs (i,j)(i,j) satisfying cover(i,j,u,v)=1\text{cover}(i,j,u,v)=1, none has f(i,j)>4f(i,j)>4.

[Constraints]

  • Subtask 1 (8 pts): n,q50n,q\le50;
  • Subtask 2 (12 pts): n,q2×103n,q\le2\times10^3;
  • Subtask 3 (16 pts): n,q4×104n,q\le4\times10^4;
  • 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 11 operations;
  • Subtask 6 (12 pts): it is guaranteed that in each query, both uu and vv are leaf nodes;
  • Subtask 7 (30 pts): no special restrictions.

The random method in Subtask 4 is: randomly generate a permutation pp. For i[2,n]i\in[2,n], connect pip_i to a uniformly random node among p1,p2,...,pi1p_1,p_2,...,p_{i-1}.

For 100%100\% of the testdata, 1n,q,ai1051\le n,q,a_i \le 10^5. It is guaranteed that every query satisfies that uu is not an ancestor of vv and vv is not an ancestor of uu, and uvu \neq v.

Translated by ChatGPT 5