Description
ACM has bought a new crane (Crane -- Je? Áb ). the crane consists of N segments of various lengths, connected by flexible joints. the end of the I-th segment is joined to the beginning of the I + 1-th one, for 1 ≤ I <n. the beginning of the first segment is fixed at point with coordinates (0, 0) and its end at point with coordinates (0, W), where W is the length of the first segment. all of the segments lie always in one plane, and the joints allow arbitrary rotation in that plane. after series of unpleasant accidents, it was decided that software that controls the Crane must contain a piece of code that constantly checks the position of the end of Crane, and stops the crane if a collision shoshould happen.
Your task is to write a part of this software that determines the position of the end of the n-th segment after each command. the state of the crane is determined by the angles between consecutive segments. initially, all of the angles are straight, I. E ., 180 o. the operator issues commands that change the angle in exactly one joint.
Input
The input consists of several instances, separated by Single Empty lines.
The first line of each instance consists of two integers 1 ≤ n ≤ 10 000 and C 0 separated by a single space -- the number of segments of the crane and the number of commands. the second line consists of N integers L1 ,..., ln (1 Li 100) separated by single spaces. the length of the I-th segment of the crane is Li. the following C lines specify the commands of the operator. each line describing the command consists of two integers s and a (1 ≤ S <n, 0 ≤ A ≤359) separated by a single space -- the order to change the angle between the S-th and the S + 1-th segment to a degrees (the angle is measured counterclockwise from the S-th to the S + 1-th segment ).
Output
The output for each instance consists of C lines. the I-th of the lines consists of two rational numbers x and y separated by a single space -- the coordinates of the end of the n-th segment after the I-th command, rounded to two digits after the decimal point.
The outputs for each two consecutive instances must be separated by a single empty line.
Sample Input
2 110 51 903 25 5 51 2702 90
Sample output
5.00 10.00-10.00 5.00-5.00 10.00
Solution:
This involves geometric issues. First, we know that the coordinates after a point (x0, y0) rotates n degrees counterclockwise around the origin point are: x = x0 * cosn-y0 * Sinn, y = x0 * Sinn + y0 * cosn. This does not prove.
When each rotating joint of a crane is regarded as the origin, the coordinates of each arm in the initial state of the crane are (0, Li) and the length of each segment is Li. You only need to add the coordinates of each arm in the final state to get the real coordinates at the end of the arm.
When updating a line segment tree, you must note that when a rotating joint is rotated, each arm behind it must be rotated at the same angle, that is, the rotation angle and coordinate interval must be updated.
AC code:
# Include <iostream> # include <cstdio> # include <cmath> # include <cstring> using namespace STD; # define lson l, m, RT <1 # define rson m + 1, R, RT <1 | 1 # define PI ACOs (-1.0) const int maxn = 10010; double Len [maxn]; struct node {Double X, Y; // use a struct to record the coordinates and rotation angles of each arm, int angl;} p [maxn <2]; void func (int rt, double rad) // returns the coordinate after rotation {double S = P [RT]. x, t = P [RT]. y; P [RT]. X = S * Cos (RAD)-T * sin (RAD); P [RT]. Y = S * sin (RAD) + T * Cos (RAD);} void pushup (int rt) // coordinate addition {P [RT]. X = P [RT <1]. X + P [RT <1 | 1]. x; P [RT]. y = P [RT <1]. Y + P [RT <1 | 1]. y;} void Pushdown (int rt) // downward update the coordinate and Rotation Angle of the slave node {If (P [RT]. angl) {P [RT <1]. angl + = P [RT]. angl; P [RT <1 | 1]. angl + = P [RT]. angl; double rad = P [RT]. angl * PI/180; P [RT]. angl = 0; func (RT <1, rad); func (RT <1 | 1, rad) ;}} void build (int l, int R, int R T) {P [RT]. angl = 0; If (L = r) {P [RT]. y = Len [l]; P [RT]. X = 0; return;} int M = (L + r)> 1; build (lson); Build (rson); pushup (RT );} void Update (INT cur, int A, int L, int R, int RT) // update the Rotation Angle and coordinates of the vehicle arm {If (L = r) {double rad = A * PI/180; func (RT, rad); return;} Pushdown (RT); int M = (L + r)> 1; if (cur <= m) {double rad = A * PI/180; Update (cur, A, lson); func (RT <1 | 1, rad ); // right child The leaf coordinates must be updated P [RT <1 | 1]. angl + = A; // The Rotation Angle of the right leaf must be updated} else Update (cur, A, rson); pushup (RT);} int main () {int N, C, S, A, degree [maxn]; bool first = true; while (scanf ("% d", & N, & C )! = EOF) {memset (degree, 0, sizeof (degree); If (first) {First = false;} else printf ("\ n "); for (INT I = 1; I <= N; I ++) scanf ("% lf", & Len [I]); Build (1, n, 1 ); while (c --) {scanf ("% d", & S, & A); int Delta = A-180-degree [S + 1]; // obtain the multiple rotation angles. degree [S + 1] = A-180; // obtain the Rotation Angle Update (S + 1, Delta, 1, n, 1 ); printf ("%. 2f %. 2f \ n ", P [1]. x, p [1]. y) ;}} return 0 ;}