Players paint tree nodes blue and queries ask for the sum of weighted distances from a node to all painted nodes.
Medium7Divide and conquerTreeDFSNo attempts yetTime limit5sMemory limit128 MBYeondori 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 N spots on campus, numbered 0 through N−1. The N spots are joined by N−1 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 Q missions in the order they are given. There are two kinds of mission.
No spot is painted when the game starts. If a mission of the second kind comes while no locker is painted, its answer is 0.
Yeondori cannot work out the answers to the second kind of mission. Compute them for him.
The first line contains the number of spots N (1≤N≤100000).
Each of the next N−1 lines, the i-th of them for 1≤i≤N−1, contains two integers Ai and Bi (0≤Ai<i, 1≤Bi≤100). Spot i and spot Ai are joined by a road of length Bi.
The next line contains the number of missions Q (1≤Q≤100000).
Each of the next Q lines contains two integers Ci and Di (Ci is 1 or 2, 0≤Di<N), written in the order the missions are performed. If Ci is 1, paint a blue locker at spot Di. If Ci is 2, find the sum of the distances from spot Di to every painted spot.
For each mission with Ci=2, print the sum of the distances on its own line, in the order the missions are given.