POJ 2991 Crane 线段树

Crane
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 5091 Accepted: 1368 Special Judge

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

题意:

有一台起重机。我们把起重机看成由n条线段依次收尾相接而成。第i条线段的长度是Li。最开始,所有的线段都笔直连接,指向上方。

有c条操纵起重机的指令。指令i给出两个整数Si和Ai,效果是使线段Si和Si+1之间的角度变成Ai度。其中角度指的是从线段Si开始沿逆时针方向旋转到Si+1所经过的角度。最开始时所有的角度都是180度。

按顺序执行这c条指令。在每条指令执行之后,输出起重机的前端(第n条线段的端点)的坐标。假设起重机的支点的坐标是(0, 0)。

分析:

本题可以使用线段树来解决。每个节点表示一段连续的线段的集合,并且维护下面两个值。

*把对应的线段集合中的第一条线段转至垂直方向之后,从第一条线段的起点指向最后一条线段的终点的向量。

*(如果该节点有儿子节点)两个儿子节点对应的部分连接之后,右儿子需要转动的角度。也就是说,如果节点i表示的向量是vxi,vyi,角度是angi,两个儿子节点是chl和chr,那么就有

VXi = VXchl + (cos(angi) * VXchr –sin(angi) * VYchr)

VYi = VYchl + (sin(angi) * VXchr + cos(angi)* Vychr)

这样,每次更新便可在O(logn)时间内完成,而输出的值就是根节点对应的向量的值。

下面的实现和RMQ的有所不同,线段树的大小并没有扩大2的幂。这个时候,线段树并不是一颗完美二叉树,但在本题中同样可以完成各种操作。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define M_PI acos(-1.0)
using namespace std;
const int ST_SIZE = (1 << 15) - 1;
const int maxn = 10000 + 10;

int N, C;
int L[maxn], S[maxn], A[maxn];

//线段树所维护的数据
double vx[ST_SIZE], vy[ST_SIZE];   //各节点的向量
double ang[ST_SIZE];     //各节点的角度

//为了查询角度的变化而保存的当前角度的数组
double prv[maxn];

//初始化线段树
//k是节点的编号,l, r表示当前节点对应的是[l, r)区间
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];
    }
}

//把s和s + 1的角度变成a
//v是节点的编号,l, r表示当前节点对应的是[l, r)区间
void change(int s, double a, int v, int l, int r)
{
    if (s <= l)
        return;
    else if (s < r){
        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;

        double s = sin(ang[v]), c = cos(ang[v]);
        vx[v] = vx[chl] + (c * vx[chr] - s * vy[chr]);
        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] = M_PI;

    //处理操作
    for (int i = 0; i < C; i++){
        int s = S[i];
        double a = A[i] / 360.0 * 2 * M_PI;   //把角度换算为弧度

        change(s, a - prv[s], 0, 0, N);
        prv[s] = a;

        printf("%.2f %.2f\n", vx[0], vy[0]);
    }
}

int main()
{
    while (scanf("%d%d", &N, &C) != EOF){
        memset(L, 0, sizeof(L));
        memset(S, 0, sizeof(S));
        memset(A, 0, sizeof(A));
        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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值