#P8959. 「CGOI-3」灵气
「CGOI-3」灵气
Background
"Screams echo through the dungeon..."
After getting the AC for Shihua, ac enters the dungeon to farm aura...
The post-flower dungeon is very dangerous, and ac wants to fill the dungeon with one-way doors.
Problem Description
The dungeon in ac's world has small rooms, with exactly corridors, and all rooms are connected. In room , at any time there is at most one monster spawned from this room, and its damage is .
To make aura farming easier, ac builds a one-way door on every corridor.
Each second, one of the following events happens:
-
A monster spawns in room . The monster cannot pass through walls and can only move along the direction of one-way doors.
-
The monster spawned in room is killed by ac's servant.
-
ac wants to AFK and farm monsters, so he wants to know: if he has been standing in room from time until the current time, what is the damage taken at the time when the damage was the maximum.
Here, the damage taken at a time is defined as the sum of damage values of all monsters that can reach the room where ac is. “Can reach” means being able to pass through several one-way doors to arrive at ac’s room (the number of doors can be ). ac is very strong and will not be killed by monsters midway.
Of course, ac’s position does not change monster spawning or the servants’ actions.
Simplified statement
A tree: each node has a weight, and each edge has a direction.
There is a set, initially empty, and three operations:
- Add a node to the set.
- Delete a node from the set.
- Given a node , query the historical maximum of the sum of weights of nodes in the set that can reach .
Input Format
The first line contains two integers , representing the number of rooms and the number of events.
The next line contains integers, where the -th integer represents the monster damage in room .
The next lines each contain two integers , meaning there is a corridor between and , and the one-way door direction is .
The next lines each contain two integers , meaning event happens in room .
For operations with , it is guaranteed that there is no monster in room .
For operations with , it is guaranteed that there is a monster in room .
Output Format
For each event with , output one integer as the answer, each on its own line.
5 7
1 2 3 4 5
1 2
3 1
4 3
3 5
1 4
3 1
2 4
1 3
1 5
3 1
3 5
4
4
8
8 7
4 1 3 5 8 6 2 9
1 2
3 1
4 2
5 1
5 6
7 5
6 8
1 1
1 5
3 7
3 1
1 2
2 5
3 5
0
12
8
Hint
Explanation for Sample 1
In the first query, at time the rooms with monsters are . The monster in room can reach room , so the answer is .
In the second query, the time with the maximum damage is time , so the answer is . At time , the rooms with monsters are , but the monster in room cannot reach room , so the damage at this time is , which is not the maximum.
In the third query, the time with the maximum damage is time . The monsters in rooms and can both reach room , so the answer is .
Constraints
"This problem uses bundled testdata."
For of the testdata, .
For another of the testdata, for every corridor , the one-way door satisfies .
For another of the testdata, there are no type 2 events.
For of the testdata, , .
But dungeon ghosts are wall-phasing monsters ( ) ( ) ( ).
Would anyone really fill the dungeon with one-way doors.
Translated by ChatGPT 5
