Crane(线段树)

来源:互联网 发布:淘宝卖家怎么提高销量 编辑:程序博客网 时间:2024/06/05 16:22

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 110 51 903 25 5 51 2702 90

Sample Output

5.00 10.00-10.00 5.00-5.00 10.00

解题思路

http://blog.csdn.net/iyundi/article/details/10947035找个清晰易懂的真不容易,,,就照着先敲了一遍



AC代码

#include <cstdio>#include <cmath>#define lson l, m, rt << 1#define rson m + 1, r, rt << 1 | 1using namespace std;const int mm = 11111;int sd[mm << 2], degree[mm];double sx[mm << 2], sy[mm << 2];void rotate(int rt, int sd){    double d = sd * asin(1.0) / 90.0;    double x = cos(d) * sx[rt] - sin(d) * sy[rt];    double y = sin(d) * sx[rt] + cos(d) * sy[rt];    sx[rt] = x, sy[rt] = y;}void pushdown(int rt){    rotate(rt << 1, sd[rt]);    rotate(rt << 1 | 1, sd[rt]);    sd[rt << 1] += sd[rt];    sd[rt << 1 | 1] += sd[rt];    sd[rt] = 0;}void pushup(int rt){    sx[rt] = sx[rt << 1] + sx[rt << 1 | 1];    sy[rt] = sy[rt << 1] + sy[rt << 1 | 1];}void build(int l, int r, int rt){    sd[rt] = 0;    if(l == r)    {        scanf("%lf", &sy[rt]);        sx[rt] = 0;        return ;    }    int m = (l + r) >> 1;    build(lson);    build(rson);    pushup(rt);}void update(int p, int d, int l, int r, int rt){    if(p < l)    {        rotate(rt, d);        sd[rt] += d;        return ;    }    if(sd[rt])        pushdown(rt);    int m = (l + r) >> 1;    if(p < m)        update(p, d, lson);    update(p, d, rson);    pushup(rt);}int main(){    int i, j, n, m, flag = 0;    while(~scanf("%d%d", &n, &m))    {        if(flag)            puts("");        else            flag = 1;        build(1, n, 1);        for(i = 0; i < n; i++)            degree[i] = 180;        while(m--)        {            scanf("%d%d", &i, &j);            update(i, j - degree[i], 1, n, 1);            degree[i] = j;            printf("%.2lf %.2lf\n", fabs(sx[1]) < 1e-8 ? 0 : sx[1], fabs(sy[1]) < 1e-8 ? 0 : sy[1]);        }    }    return 0;}



0 0
原创粉丝点击