POJ 2991 Crane (+ calculated geometric segment tree)

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

Meaning of the questions:
the meaning of problems feeling a little hard to understand. It gives the length of each stick, and then sequentially attached to the Y-axis. The initial starting point is the stick (0,0)
m interrogation times, each time changing the x and x + 1 a counterclockwise angle of the stick, the initial angle of 180 degrees.
Always ask a stick the end of the final output coordinates. When adjusting the x and x + 1 stick angle, the actual adjustment of the direction of x + 1 behind the stick.
Ideas:
segment tree maintains two information, one is the end point coordinate, a is the angle transformation.
In fact the end of the initial coordinates have been identified, each adjustment will affect the angle of the end point coordinates.
Each adjustment of the angle obtained y - degree [x], together with the formula of coordinate transformation vector
newx = X * COS (RAD) - Y * SiN (RAD)
newy, SiN = X * (RAD) + Y * COS (rad)
to update, the update is actually 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;
}

Published 676 original articles · won praise 18 · views 30000 +

Guess you like

Origin blog.csdn.net/tomjobs/article/details/104102330