#P7671. [GDOI2016] 疯狂动物城
[GDOI2016] 疯狂动物城
Description
Note: We provide a formal statement at the end.
Zootopia is a federation with states, and the federation forms a tree. That is, there are undirected roads connecting the states, and all states are connected. The states are numbered . 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 , the machine produces energy stones that can save carnivores per unit time.
Nick and Judy do not have much time left, so they decide to split up. Nick starts from state and heads to state , traveling along the shortest path from to . Nick starts at time 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 until he arrives at , 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 : it upgrades the machines in all states on the shortest path from to (including states and ). After the upgrade, the number of carnivores that the energy stones produced by these machines can save per unit time increases by .
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 of machines in all states. Flash can use a mysterious weapon to perform a rollback operation , restoring the current machine attributes of all states to the state saved at the -th time (where means the initial state before any upgrade). Note that saving happens only when a modification operation is executed.
Now you are given operations in order. If an operation is a query, output how many carnivores are saved in total when Nick travels from to under the current situation. Since the answer may be large, you only need to output it modulo . Note that all operations are encrypted.
Formal statement:
You are given a tree with nodes. Then there are operations of three types:
1 x y w: add to the node weight of every node on the path from to .2 x y: a query. Let the set of nodes on the path from to be , and let the path length between nodes be . Compute $\sum_{i\in S(x,y)}\sum_{j\le \text{dis}(i,y)}a_i\cdot j$. Output the answer modulo .3 x: restore the node weights of the whole tree to the state right after the -th1operation.
Online processing is required.
Input Format
The first line contains integers , indicating the number of nodes and the number of operations.
The next lines each contain integers , indicating that there is an edge between and in the tree.
The next line contains integers, the initial values of for the machines in the states.
The next lines each start with a number indicating the operation type:
- Type 1: a modification operation, followed by integers .
- Type 2: a query operation, followed by integers .
- Type 3: a rollback operation, followed by integer .
Here are encrypted numbers. The correct values are . Here is the answer of the previous 2 operation modulo ; if there has been no 2 operation before, then it equals .
Output Format
For each operation 2, output one line containing the queried answer modulo .
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, , , .
For of the data, .
For another of the data, the tree is a chain and there is no operation 3.
For another of the data, the tree is a chain.
Translated by ChatGPT 5
京公网安备 11011102002149号