나무 구조로 맞물린 기어들을 관리하며 기어를 떼거나 다시 붙이고, 한 기어를 회전하면 이웃 기어가 반대로 돌아가는 상황에서 각 회전에 쓰인 에너지와 마지막 모든 기어 각도의 합을 구한다.
어려움8트리DFS세그먼트 트리구현아직 제출이 없습니다시간 제한1초메모리 제한512 MBAndi has N gears (numbered from 1 to N) on a board. Each gear has an arrow initially pointing upward. Some pair of gears may touch each other. If we construct a graph where each gear is a node, and we connect each pair of touching gears with an edge, then the structure of this graph is a tree. For example, the following is a possible gear configuration example with N = 4 gears and gear 2 touches all other gears.

Standard gear rotation rule applies: Suppose a gear u touches another gear v, and gear u is rotated α degrees clockwise, then gear v will be rotated α degrees counter-clockwise, and vice-versa.
Andi wants to perform three kinds of operations:
Let δu be the arrow’s degree (clockwise, modulo 360) of gear u after Q operations are done. Andi wants to know the sum of δu for all u. Furthermore, since rotating gears requires a lot of work, Andi also wants to know how much energy he needs for every Type 3 operation (rotating a gear). The amount of energy Andi needs to perform the Type 3 operation is defined as <number of rotating gears> × <rotation done in degrees>.
Input begins with a line containing an integer N (1 ≤ N ≤ 100000) representing the number of gears. The next N − 1 lines, each contains two integers: u v (1 ≤ u, v ≤ N; u ≠ v) representing that gear u and gear v touches each other. It is guaranteed that the structure of the connected gears forms a tree. The next line contains an integer Q (1 ≤ Q ≤ 100000) representing the number of operations. The next Q lines, each representing an operation to be done sequentially. Each operation is given in one of the following formats:
For each Type 3 operation in the same order as input, output in a line the amount of energy needed to rotate the gears. Finally, output in a line the sum of δu for all u.