#P7671. [GDOI2016] 疯狂动物城

    ID: 8351 远端评测题 2000ms 2048MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>2016各省省选广东树链剖分可持久化线段树

[GDOI2016] 疯狂动物城

Description

Note: We provide a formal statement at the end.

Zootopia is a federation with NN states, and the federation forms a tree. That is, there are N1N-1 undirected roads connecting the NN states, and all NN states are connected. The states are numbered 1,2,3,,N1,2,3,\dots,N. Each state has exactly one machine. Starting from the moment after a machine is activated, it begins producing energy stones, producing one per unit time. An energy stone takes effect immediately when produced, and in each unit time it can save a certain number of carnivores. The types of energy stones produced by machines in different states may differ: in state ii, the machine produces energy stones that can save aia_i carnivores per unit time.

Nick and Judy do not have much time left, so they decide to split up. Nick starts from state XX and heads to state YY, traveling along the shortest path from XX to YY. Nick starts at time 00 and moves one state every unit time. Each time he arrives at a state, Nick activates the machine there. Nick wants to know, during the time from when he leaves XX until he arrives at YY, how many carnivores are saved in total. Nick is hesitant about which route to choose, so he will ask you several queries, hoping that you, who are smarter than him, can help.

While he is asking you, the situation in Zootopia is also changing. The Zootopia Federal Security Bureau can perform a modification operation X,Y,ΔX,Y,\Delta: it upgrades the machines in all states on the shortest path from XX to YY (including states XX and YY). After the upgrade, the number of carnivores that the energy stones produced by these machines can save per unit time increases by Δ\Delta.

Of course, the sloth Flash will not sit still. He has a monitor that watches the machines in every state. Whenever any machine is upgraded, the monitor saves the current attributes aia_i of machines in all states. Flash can use a mysterious weapon to perform a rollback operation XX, restoring the current machine attributes of all states to the state saved at the XX-th time (where X=0X=0 means the initial state before any upgrade). Note that saving happens only when a modification operation is executed.

Now you are given MM operations in order. If an operation is a query, output how many carnivores are saved in total when Nick travels from XX to YY under the current situation. Since the answer may be large, you only need to output it modulo 2016050120160501. Note that all MM operations are encrypted.


Formal statement:

You are given a tree with NN nodes. Then there are MM operations of three types:

  • 1 x y w: add ww to the node weight of every node on the path from xx to yy.
  • 2 x y: a query. Let the set of nodes on the path from xx to yy be S(x,y)S(x,y), and let the path length between nodes p,qp,q be dis(p,q)\text{dis}(p,q). Compute $\sum_{i\in S(x,y)}\sum_{j\le \text{dis}(i,y)}a_i\cdot j$. Output the answer modulo 2016050120160501.
  • 3 x: restore the node weights of the whole tree to the state right after the xx-th 1 operation.

Online processing is required.

Input Format

The first line contains 22 integers N,MN,M, indicating the number of nodes and the number of operations.

The next N1N-1 lines each contain 22 integers u,vu,v, indicating that there is an edge between uu and vv in the tree.

The next line contains NN integers, the initial values of aia_i for the machines in the NN states.

The next MM lines each start with a number indicating the operation type:

  • Type 1: a modification operation, followed by 33 integers X,Y,ΔX',Y',\Delta.
  • Type 2: a query operation, followed by 22 integers X,YX',Y'.
  • Type 3: a rollback operation, followed by 11 integer XX'.

Here X,YX',Y' are encrypted numbers. The correct values are X=Xxor lastans,Y=Yxor lastansX=X' \text{xor lastans},Y=Y' \text{xor lastans}. Here lastans\text{lastans} is the answer of the previous 2 operation modulo 2016050120160501; if there has been no 2 operation before, then it equals 00.

Output Format

For each operation 2, output one line containing the queried answer modulo 2016050120160501.

5 6
1 2
2 3
3 4
4 5
1 2 3 4 5
1 1 5 2
3 0
1 1 3 2
1 3 4 2
3 2
2 1 5
73
5 4
1 2
1 3
2 4
3 5
1 1 1 2 2
1 1 4 2
2 1 4
3 12
2 13 8
12
4

Hint

Constraints: For all data, 1n,m1051\le n,m\le 10^5, 1ai,Δ1051\le a_i,\Delta\le 10^5, 1x,yn1\le x,y\le n.

For 20%20\% of the data, n,m2000n,m\le 2000.

For another 20%20\% of the data, the tree is a chain and there is no operation 3.

For another 40%40\% of the data, the tree is a chain.

Translated by ChatGPT 5