描述
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., 180o. 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 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
解题报告
以(2,3,4,5,6,7,8,9)为例,建立的线段树如下所示:
每个节点维持存储:1)序号;2)权值;3)所管理的范围
更新时,维持下面两个值。
1)把对应的线段集合中的第一条线段转至垂直方向后,第一条线段的起点指向最后一条线段的终点的向量;【某些节点集合中的第一条线段如果不在垂直方向,则对应的vx[],vy[]则不是该节点本身的的向量】
2)两个儿子节点对应的部分连接之后,右儿子需要转动的角度;
向量(x,y)顺时针旋转B度,该向量变成:
x=xcos(B)+ysin(B)
y=ycos(B)-xsin(B)
prv[]:存储全局状态下每条线段与其上一根线段之间的角度(逆时针旋转计算的角度),数组大小和总的线段数量是相等的。
ang[]:存储全局状态下建立的线段树中各节点需要更新时,左右儿子节点对应部分连接时,右儿子需要逆时针转动的角度。
3/2*PI=4.71238898
PI/2=1.57079632
代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
#define M_PI acos(-1.0)
#define N 10000
#define C 100000
#define ST_SIZE 1<<15
int n,c;
int L[N];//线段长度
int S[C],A[N];//指定要转的线段和角度
double vx[ST_SIZE],vy[ST_SIZE];//各节点的向量
double ang[ST_SIZE];//各节点的角度
double prv[N];//为了查询角度的变化而保存的当前角度的数组
void init(int k, int l, int r)//节点编号,左右区间下标
{
ang[k]=vx[k]=0.0;
if (r-l==1)//叶子节点
vy[k]=L[l];//初始状态下都是竖直向上的
else//非叶子节点
{
int chl=k*2+1, chr=k*2+2;
init(chl,l,(l+r)/2);
init(chr,(l+r)/2,r);
vy[k]=vy[chl]+vy[chr];
}
}
void change(int s,double a,int k,int l,int r)//将s和s+1的角度变为a,k是节点编号,lr是左右区间范围
{
if (s<=l) return;
else if (s<r)
{
int chl=k*2+1,chr=k*2+2;
int m=(l+r)/2;//中点
change(s,a,chl,l,m);
change(s,a,chr,m,r);
if (s<=m)
{
ang[k]+=a;
}
double si=sin(ang[k]),co=cos(ang[k]);
vx[k]=vx[chl]+(co*vx[chr]-si*vy[chr]);
vy[k]=vy[chl]+(si*vx[chr]+co*vy[chr]);
}
}
void solve()
{
init(0,0,n);
for (int i=1; i<n; i++)
prv[i]=M_PI;
for (int i=0; i<c; i++)
{
scanf("%d%d",&S[i],&A[i]);
int s=S[i];
double a=A[i]/360.0*2*M_PI;//把角度转换成弧度
change(s,a-prv[s],0,0,n);//a-prv[s]表示要转的角度减去当前的角度
prv[s]=a;//更新当前的角度
printf("%.2f %.2f\n", vx[0],vy[0]);
}
}
int main()
{
while(cin>>n>>c)//输入数量和查询次数
{
for (int i=0; i<n; i++)
scanf("%d",&L[i]);//输入长度
solve();
cout<<endl;
}
return 0;
}