POJ 2991 Crane(计算几何+线段树)

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
Source

CTU Open 2005

题意:
题意感觉有点难懂。给出每个木棍的长度,然后顺序连接在Y轴。初始木棍的起点为(0,0)
m次询问,每次改变第x和第x+1个木棍的逆时针角度,初始角度为180度。
每次询问输出最后一根木棍终点坐标。调整第x和第x+1根木棍角度时,实际调整的是x+1后面木棍的方向。
思路:
线段树维护两个信息,一个是终点坐标,一个是变换的角度。
实际上终点坐标初始已经确定了,每次调整的角度会影响终点坐标。
得出每次调整的角度为y - degree[x],再加上向量坐标变换的公式
newx = x * cos(rad) - y * sin(rad)
newy = x * sin(rad) + y * cos(rad)
更新的时候,实际是更新 x + 1 ~ n。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

const int maxn = 1e4 + 7;
const double PI = acos(-1);
double len[maxn],degree[maxn];
struct Node
{
    int l,r;
    double d;
    double x,y;
}t[maxn << 2];

void Rotate(int i,int r)
{
    double rad = r / 180.0 * PI;
    double newx = t[i].x * cos(rad) - t[i].y * sin(rad);
    double newy = t[i].x * sin(rad) + t[i].y * cos(rad);
    t[i].x = newx;t[i].y = newy;
}

void pushup(int i)
{
    t[i].x = t[i * 2].x + t[i * 2 + 1].x;
    t[i].y = t[i * 2].y + t[i * 2 + 1].y;
}

void pushdown(int i)
{
    if(t[i].d)
    {
        t[i * 2].d += t[i].d;
        t[i * 2 + 1].d += t[i].d;
        Rotate(i * 2,t[i].d);
        Rotate(i * 2 + 1,t[i].d);
        t[i].d = 0;
    }
}

void build(int i,int l,int r)
{
    t[i].l = l;t[i].r = r;
    t[i].d = 0;
    if(l == r)
    {
        t[i].x = 0;
        t[i].y = len[l];
        return;
    }
    int m = (l + r) >> 1;
    build(i * 2,l,m);
    build(i * 2 + 1,m + 1,r);
    pushup(i);
}

void update(int i,int x,double v)
{
    if(x <= t[i].l)
    {
        Rotate(i,v);
        t[i].d += v;
        return;
    }
    pushdown(i);
    int m = (t[i].l + t[i].r) >> 1;
    if(x <= m)update(i * 2,x,v);
    update(i * 2 + 1,x,v);
    pushup(i);
}

int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        for(int i = 1;i <= n;i++)
        {
            scanf("%lf",&len[i]);
            degree[i] = 180.0;
        }
        build(1,1,n);
        while(m--)
        {
            int x;
            double y;scanf("%d%lf",&x,&y);
            update(1,x + 1,y - degree[x]);
            degree[x] = y;
            double ansx = fabs(t[1].x) < 1e-8 ? 0 : t[1].x;
            double ansy = fabs(t[1].y) < 1e-8 ? 0 : t[1].y;
            printf("%.2f %.2f\n",ansx,ansy);
        }
        printf("\n");
    }
    return 0;
}

发布了676 篇原创文章 · 获赞 18 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/104102330