luogu#P5237. 动态仙人掌 IV

动态仙人掌 IV

Background

Template problem.

Problem Description

There are nn vertices, numbered from 1n1 \sim n. Vertex ii has a weight wiw_i. Initially, there are no edges.

There are mm operations. An operation is considered valid if, after applying it, every connected component of this undirected graph is a cactus, and there are no self-loops. Otherwise, the operation is invalid. Invalid operations are ignored. There are six types of operations:

  1. Add an edge of weight ww between vertices vv and uu.

  2. Delete an edge of weight ww between vertices vv and uu.

  3. Add dd to the weight of every vertex on the shortest path from vertex vv to vertex uu.

  4. Add dd to the weight of every vertex in the sub-cactus uu, rooted at vertex vv.

  5. Query information about the shortest path from vertex vv to vertex uu.

    Output two integers min,σmin,\sigma separated by a space, representing the minimum vertex weight and the sum of vertex weights on the shortest path.

    If there is no path reachable, then min=1,σ=1min=-1,\sigma =-1.

    If the shortest path is not unique, then min=2,σ=2min=-2,\sigma =-2.

  6. Query information about the sub-cactus uu, rooted at vertex vv.

    Output two integers min,σmin,\sigma separated by a space, representing the minimum vertex weight and the sum of vertex weights in sub-cactus uu.

    If vv and uu are not connected, then min=1,σ=1min=-1,\sigma =-1.

The definition of the sub-cactus uu rooted at vertex vv is: after deleting the edges on all simple paths between vv and uu, the connected component containing uu.

Input Format

The first line contains two positive integers n,mn,m separated by spaces, meaning there are nn vertices and mm operations in total.

The next line contains nn positive integers. The ii-th positive integer is wiw_i.

The next mm lines each describe one operation. Each line starts with three integers: opti,vi,uiopt_i,v_i,u_i.

If opti{1,2,3,4}opt_i \in \{ 1,2,3,4 \}, then one more integer follows; otherwise, the line ends.

Output Format

For operations 55 and 66, output the corresponding results.

11 23
10 5 11 7 8 14 30 3 16 20 19
1 1 2 5
1 2 3 3
1 3 4 7
1 4 5 8
1 2 6 10
1 6 7 15
1 4 7 3
1 6 8 9
1 6 8 6
1 7 9 12
1 9 11 10
1 7 10 4
1 9 10 8
5 5 11
5 2 10
6 8 7
3 8 5 100
5 1 7
6 8 7
4 11 7 1000
5 8 3
4 3 2 2333
5 1 5
-2 -2
5 73
16 85
5 263
16 185
1005 4233
1011 9907

Hint

Constraints
For 100%100\% of the testdata, 1n50000,1m2500001 \leq n \leq 50000, 1 \leq m \leq 250000.

It is guaranteed that in operations 11 and 22, ww satisfies 1w100001 \leq w \leq 10000, so computations involving edge weights will not exceed the range of a 32-bit signed integer.

It is guaranteed that the initial wiw_i does not exceed 10910^9, and that the sum of all dd in operations 33 and 44 does not exceed 10910^9.

Translated by ChatGPT 5