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 should 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.
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).
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.
The outputs for each two consecutive instances must be separated by a single empty line.
Sample Input
2 1 10 5 1 90 3 2 5 5 5 1 270 2 90
Sample Output
5.00 10.00 -10.00 5.00 -5.00 10.00
Source
CTU Open 2005
/**
题意:有一个机械手臂,由n节相连组成,给定每节的长度,
一开始均为竖直方向。有m次操作 x d,表示将x节与x+1节之间角度变为d度,
并且输出手臂末端的坐标。
分析:初始每相邻两节夹角180度,注意:x节与x+1节间的角度为:
将x节逆时钟旋转到x+1节位置的角度!!!
利用向量坐标的可加性,每节维护它相对与(和上一节构成)结点的坐标,
那么每次答案即为区间的坐标和。
对于每个操作x d求出x+1节需要旋转的角度,区间更新坐标即可
*/
#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = 10100;
const double eps = 1e-5;
int len[N];
double x[N<<2],y[N<<2];
int mak[N<<2];
int curr[N];
void rotate(double&x, double&y, int an)
{
double a = x, b = y;
double ang = an/180.0*acos(-1.0);
x = a*cos(ang)-b*sin(ang);
y = a*sin(ang)+b*cos(ang);
}
void build(int l, int r, int rt)
{
mak[rt] = 0;
if(l==r)
{
x[rt] = 0;
y[rt] =1.0*len[l];
return ;
}
int m = l+r>>1;
build(l,m,rt<<1);
build(m+1,r,rt<<1|1);
x[rt] = x[rt<<1]+x[rt<<1|1];
y[rt] = y[rt<<1]+y[rt<<1|1];
}
void update(int L, int R, int a, int l, int r,int rt)
{
if(L<=l && R>=r)
{
mak[rt] = (mak[rt]+a)%360;
rotate(x[rt],y[rt],a);
return ;
}
int m = l+r>>1;
if(mak[rt])
{
mak[rt<<1] = (mak[rt<<1]+mak[rt])%360;
mak[rt<<1|1] = (mak[rt<<1|1]+mak[rt])%360;
rotate(x[rt<<1],y[rt<<1],mak[rt]);
rotate(x[rt<<1|1],y[rt<<1|1],mak[rt]);
mak[rt] = 0;
}
if(L<=m) update(L,R,a,l,m,rt<<1);
if(R>m) update(L,R,a,m+1,r,rt<<1|1);
x[rt] = x[rt<<1]+x[rt<<1|1];
y[rt] = y[rt<<1]+y[rt<<1|1];
}
int main()
{
//freopen("date.in","r",stdin);
int i,j,k,m,n;
bool blank = false;
while(scanf("%d %d",&n,&m)==2)
{
if(blank) puts("");
blank = true;
for(i=1; i<=n; i++) scanf("%d",len+i);
build(1,n,1);
for(int i=1; i<=n; i++) curr[i]=180;
while(m--)
{
scanf("%d %d",&i,&j);
i++;
int ang = curr[i];
curr[i] = j;
ang = (360+j-ang)%360;
update(i,n,ang,1,n,1);
printf("%.2f %.2f\n",x[1]+eps,y[1]+eps);
}
}
return 0;
}