#P15952. [ICPC 2018 Jakarta R] Rotating Gears

[ICPC 2018 Jakarta R] Rotating Gears

题目描述

Andi has NN gears (numbered from 11 to NN) 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=4N = 4 gears and gear 22 touches all other gears.

:::align{center} :::

Standard gear rotation rule applies: Suppose a gear uu touches another gear vv, and gear uu is rotated α\alpha degrees clockwise, then gear vv will be rotated α\alpha degrees counter-clockwise, and vice-versa.

Andi wants to perform three kinds of operations:

  1. Take out a gear from its position on the board.
  2. Place a gear back to its original position on the board. When doing this, Andi places a gear back in a way such that the arrow points to the same degree as when it was removed. Assume that it is always possible to do that, i.e. you do not need to concern with the mechanical aspect of the gear.
  3. Rotate a gear α\alpha degrees clockwise.

Let δu\delta_u be the arrow’s degree (clockwise, modulo 360360) of gear uu after QQ operations are done. Andi wants to know the sum of δu\delta_u for all uu. 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 $\langle \text{number of rotating gears} \rangle \times \langle \text{rotation done in degrees} \rangle$

输入格式

Input begins with a line containing an integer NN (1N1000001 \leq N \leq 100000) representing the number of gears. The next N1N - 1 lines, each contains two integers: u vu\ v (1u,vN;uv1 \leq u, v \leq N; u \neq v) representing that gear uu and gear vv touches each other. It is guaranteed that the structure of the connected gears forms a tree. The next line contains an integer QQ (1Q1000001 \leq Q \leq 100000) representing the number of operations. The next QQ lines, each representing an operation to be done sequentially. Each operation is given in one of the following formats:

  • Two integers: 1 x1\ x (1xN1 \leq x \leq N). This means that this operation takes out gear xx from its position on the board. It is guaranteed that gear xx is currently on the board.
  • Two integers: 2 x2\ x (1xN1 \leq x \leq N). This means that this operation places gear xx back to its original position on the board. It is guaranteed that gear xx is currently not on the board.
  • Three integers: 3 x α3\ x\ \alpha (1xN;0α3591 \leq x \leq N; 0 \leq \alpha \leq 359). This means that this operation rotates gear xx α\alpha degrees clockwise. It is guaranteed that gear xx is currently on the board.

输出格式

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\delta_u for all uu.

4
1 2
2 3
2 4
8
3 2 160
1 2
3 1 10
3 3 10
3 4 10
2 2
1 1
3 3 15
640
10
10
10
45
805

提示

Explanation for the sample input/output

The structure of the gears for this sample is illustrated by the figure in the problem description. The following table illustrates the state of each gear after each operation.

After operation δu\delta_u <
Gear 1 Gear 2 Gear 3 Gear 4
Initially 00
1 200200 160160 200200
2 200200 REMOVED 200200 200200
3 210210
4 210210 210210
5 210210 210210
6 160160 210210
7 REMOVED
8 145145 225225

Therefore, the sum of δu\delta_u for all uu is 210+145+225+225=805210 + 145 + 225 + 225 = 805.