POJ-2991-Crane -线段树

Crane
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.
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

这个题是《挑战程序设计竞赛》的例题,看了一上午,终于看懂了(●ˇ∀ˇ●)
题意:有n条线段首尾相连,且已知每条线段的长度,执行C条指令,第i条指令给出Si和Ai,使线段Si和Si+1之间的角度变为Ai度,最开始所有角度都为180度。按顺序输出每条指令执行后线段末端的坐标
解题思路:用线段树维护每个节点的向量和角度

下面大部分是书上的代码…

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cctype>
#include<cmath>
#include<cstring>
#include<sstream>
#include<set>
#include<map>
#include<queue>
#define maxn 10005
typedef long long ll;
using namespace std;
typedef pair<int,int> P;
const double PI=acos(-1.0);  //Π的精确值
const int ST_size = (1<<15)-1;
int n,c,L[maxn],S[maxn],A[maxn];  //S和A保存指令
double vx[ST_size],vy[ST_size];   //保存节点的向量
double ang[ST_size];   //保存节点变化的角度
double prv[maxn];   //保存线段s和si当前的角度
void init(int k,int l,int r)  //k为节点的编号,l,r表示当前节点对应的区间为[l,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 v,int l,int r) //参数s表示变s和s+1的角度,参数a表示变化的角度,v表示节点的编号
{                                                             
    if(s<=l) return ;
    else if(s<r)  //保证s在区间内
    {
        int chl=v*2+1,chr=v*2+2;
        int m=(l+r)/2;
        change(s,a,chl,l,m);
        change(s,a,chr,m,r);
        if(s<=m) ang[v]+=a;  //只改变大于s部分的角度
        double s=sin(ang[v]),c=cos(ang[v]);  //一个向量(x,y),顺时针旋转a度后,得到的新向量为x2=x*cos(a)+y*sin(a);
        vx[v]=vx[chl]+(c*vx[chr]-s*vy[chr]);     y2=y*cos(a)-x*sin(a);该函数的参数a为逆时针旋转的角度,所以sin值要变为负数
        vy[v]=vy[chl]+(s*vx[chr]+c*vy[chr]);
    }
}
void solve()
{
    init(0,0,n);
    for(int i=1;i<n;i++) prv[i]=PI;
    for(int i=0;i<c;i++)
    {
        int s=S[i];
        double a=A[i]/360.0*2*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(scanf("%d%d",&n,&c)==2)
    {
        for(int i=0;i<n;i++)
            scanf("%d",&L[i]);
        for(int i=0;i<c;i++)
            scanf("%d%d",&S[i],&A[i]);
        solve();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Starry~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值