Segment tree Interval Update + vector knowledge -- poj 2991

Source: Internet
Author: User
Tags cos

Corresponding poj question: Click to open the link


Crane
Time limit:2000 ms   Memory limit:65536kb   64bit Io format:% I64d & % i64u

Submit status

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

Source


A crane is composed of multiple lines of different lengths. At the beginning, it is a long straight line starting from (0, 0), and the tail node is at (0, sum [N]). the angle between each line segment is an initial value of 180 degrees. Then, some operations A and B turn the angle between the line segment A and A + 1 into a degree of B. After each operation, the coordinates of the end node are required.



Idea: the segment tree interval is updated. The Node value stores the vector and Rotation Angle of the interval (note that it does not provide the rotation angle) vector Value of one interval = vector of the Left subinterval + vector of the right subinterval.

There is a formula for finding a vector (x0, y0) that rotates degrees B counterclockwise:

X1 = x0 * CoSb-y0 * sinb

Y1 = x0 * sinb + y0 * CoSb

Clockwise-B is substituted:

X1 = x0 * CoSb + y0 * sinb

Y1 =-x0 * sinb + y0 * CoSb


There is also a little disgusting, is the calculation of the rotation angle; using a angle [I] array to represent the angle between the I segment and the I-1 segment, I here is the clockwise rotation, I initialized angle [] to 180. Therefore, when each update is made, the rotation angle t = angle [I]-B; angle [I] = B;








# Include <cstdio> # include <cstdlib> # include <cmath> # include <map> # include <queue> # include <stack> # include <vector> # include <algorithm> # include <cstring> # include <string> # include <iostream> # define MS (X, y) memset (X, Y, sizeof (x) # define EPS 1e-6const int maxn = 10000 + 10; const int INF = 1 <30; using namespace STD; double PX [maxn <2]; double py [maxn <2]; int turn [maxn <2]; int angle [maxn]; // The angle between the I-th line segment and the I-1-th line segment // const double pi = ACOs (-1.0); const double Pi = 3.141592653; void down (int rt) {If (turn [RT]) {int T = turn [RT]; turn [RT <1] + = T; turn [RT <1 | 1] + = T; // The left vector rotates the t degree double X = px [RT <1], y = py [RT <1]; px [RT <1] = x * Cos (T * PI/180) + y * sin (T * PI/180 ); PY [RT <1] =-x * sin (T * PI/180) + y * Cos (T * PI/180 ); // right vector rotation t degree x = px [RT <1 | 1], y = py [RT <1 | 1]; px [RT <1 | 1] = x * Cos (T * PI/180) + y * sin (T * PI/180 ); PY [RT <1 | 1] =-x * sin (T * PI/180) + y * Cos (T * PI/180 ); turn [RT] = 0 ;}} void Buildline (int rt, int left, int right) {If (Left = right) {PX [RT] = 0.0; scanf ("% lf ", & py [RT]); return;} int mid = (left + right)> 1; buildline (RT <1, left, mid ); buildline (RT <1 | 1, Mid + 1, right); PX [RT] = px [RT <1] + px [RT <1 | 1]; PY [RT] = py [RT <1] + py [RT <1 | 1];} void updata (int rt, int left, int right, int l, int R, int t) {If (Left = L & Right = r) {Double X = px [RT], y = py [RT]; px [RT] = x * Cos (T * PI/180) + y * sin (T * PI/180); py [RT] =-x * sin (T * PI/180) + y * Cos (T * PI/180); turn [RT] + = T; return ;} down (RT); int mid = (left + right)> 1; if (mid <L) updata (RT <1 | 1, Mid + 1, right, l, r, T); else {updata (RT <1, left, mid, L, mid, T); updata (RT <1 | 1, Mid + 1, right, mid + 1, R, T);} PX [RT] = px [RT <1] + px [RT <1 | 1]; PY [RT] = py [RT <1] + py [RT <1 | 1];} int main () {// freopen ("in.txt ", "r", stdin); int N, O, CNT = 0; while (~ Scanf ("% d", & N, & O) {MS (turn, 0); For (INT I = 2; I <= N; I ++) angle [I] = 180; If (CNT ++) printf ("\ n"); buildline (, n); int A, B; while (o --) {scanf ("% d", & A, & B); A ++; int T = angle [a]-B; angle [a] = B; updata (1, 1, n, A, N, T); printf ("%. 2lf %. 2lf \ n ", PX [1], Py [1]) ;}} return 0 ;}




Segment tree Interval Update + vector knowledge -- poj 2991

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.