码迷,mamicode.com
首页 > 其他好文 > 详细

Crane(线段树)

时间:2014-08-14 16:50:28      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:线段树

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. 

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

解题思路:

这题涉及到了几何问题。首先得知道一个点(x0,y0)绕原点逆时针旋转n角度后的坐标为:x = x0 * cosn - y0 * sinn, y = x0 * sinn + y0 * cosn。在此不证明。

把吊车的每个旋转关节都当做是原点,则吊车最初状态的每节车臂坐标为(0,li),li为每节的长度。只需要将最终状态的每节车臂的坐标加起来,就得到了车臂末尾真正的坐标。

线段树进行更新的时候需要注意:当一个旋转关节旋转,后面的每节车臂都要旋转相同的角度,也就是要进行旋转角度和坐标的区间更新。

AC代码:

#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;  //用结构体记录每节车臂的坐标和旋转角度
    int angl;
}p[maxn << 2];
void Func(int rt, double rad)  // 求旋转后的坐标
{
    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) //坐标相加
{
    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)  // 向下更新子叶的坐标与旋转角度
{
    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 rt)
{
    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)   // 对车臂的旋转角度和坐标进行更新
{
    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);  //右子叶的坐标都要进行更新
        p[rt << 1 | 1].angl += a; //右子叶的旋转角度都要进行更新
    }
    else
        update(cur, a, rson);
    PushUp(rt);
}
int main()
{
    int n, c, s, a, degree[maxn];
    bool First = true;
    while(scanf("%d%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%d", &s, &a);
            int delta = a - 180 - degree[s + 1]; // 求得是多旋转的角度
            degree[s + 1] = a - 180;  // 求得是旋转的角度
            update(s + 1, delta, 1, n, 1);
            printf("%.2f %.2f\n", p[1].x, p[1].y);
        }
    }
    return 0;
}


Crane(线段树),布布扣,bubuko.com

Crane(线段树)

标签:线段树

原文地址:http://blog.csdn.net/userluoxuan/article/details/38558963

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!