Defense Tower
Time limit6sMemory limit512 MB
Given a tree of cities with tower powers, each upgrade makes a tower the newest, and the task is to print the sum of protector labels after every upgrade.
- Level
Hard9 of 10
- Topics
- Tree, Divide and conquer, Segment tree
- Solved
- No attempts yet
Problem
In ICPCCamp, there are cities labeled , connected by bidirectional roads. There is exactly one path between any two different cities.
Each city has a defense tower with power . The towers were built in the order , and each tower has the same number as its city. So tower is the oldest and tower is the newest. The effect of tower on city is , where is the number of roads between cities and . The protector of city is the tower with the largest effect on . If several towers tie on a city, the oldest one is its protector.
Yuuka issues commands that upgrade defense towers. The -th command adds to the power of tower . After each command, she wants the sum of the protectors' labels over all cities. The upgraded tower becomes the newest tower.
Upgrading is costly. If the tower being upgraded is not the protector of its own city, or if , the command is ignored.
Input
The input has zero or more test cases and ends at end-of-file. Each test case is given as follows.
The first line contains two integers and ().
The second line contains integers ().
Each of the next lines contains two integers and (), meaning there is a road between cities and . There is exactly one path between any two different cities.
Each of the last lines contains two integers and (, ).
The sum of all and the sum of all over the test cases do not exceed each.
Output
For each test case, output integers , where is the sum of the protectors' labels after the -th command.