Yonsei University Point Game
Time limit5sMemory limit128 MB
Players paint tree nodes blue and queries ask for the sum of weighted distances from a node to all painted nodes.
- Level
Medium7 of 10
- Topics
- Divide and conquer, Tree, DFS
- Solved
- No attempts yet
Problem
Yeondori is in his second year, and he is in charge of the campus point game for the new students' orientation. In a point game, players walk to facilities all over campus and finish a mission at each spot, which is how they learn where everything is. The usual format sends a player from one spot to the next and never back. Yeondori played last year and found that he barely memorized a single location that way.
So he wrote new rules.
The game runs over spots on campus, numbered through . The spots are joined by roads that form a tree. Every road is bidirectional, there is no cycle, and every spot is reachable from every other spot. Roads can have different lengths. The distance between two spots is the total length of the roads on the unique path between them.
A player performs missions in the order they are given. There are two kinds of mission.
- Visit spot and paint a blue locker there. If that spot is already painted, nothing changes.
- Compute the sum of the distances from spot to every spot whose locker is painted blue.
No spot is painted when the game starts. If a mission of the second kind comes while no locker is painted, its answer is .
Yeondori cannot work out the answers to the second kind of mission. Compute them for him.
Input
The first line contains the number of spots ().
Each of the next lines, the -th of them for , contains two integers and (, ). Spot and spot are joined by a road of length .
The next line contains the number of missions ().
Each of the next lines contains two integers and ( is or , ), written in the order the missions are performed. If is , paint a blue locker at spot . If is , find the sum of the distances from spot to every painted spot.
Output
For each mission with , print the sum of the distances on its own line, in the order the missions are given.