POJ2991-Crane-线段树+计算几何

来源:互联网 发布:mysql原理 编辑:程序博客网 时间:2024/06/14 09:20

原题链接
Crane
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6253 Accepted: 1673 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., 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
题意:n个棍子原来排成一列竖直向上,然后修改某处棍子与棍子之间的角度,每次修改输出最后一根棍子的尾端的坐标
思路:每次修改都会影响到该棍子和该棍子之后所有棍子的位置,求最后一个棍子的位置,这就有些类似于线段树更新区间和这种问题,所以问题就是找到区间和的计算方法(即计算棍子坐标的快速的方法)。而我们注意到这里线段树的一个区间可以表示成一个向量,每一个单独的线段其实也可以表示成一个向量,而向量V0(x1,y1)转过角µ成V1(x2,y2)后坐标的计算有
这里写图片描述
然后需要一个数组记录线段与线段之间的夹角,而对于不同的向量还要记录其左子向量与右子向量的夹角并向上更新

#include <cstdio>#include <cmath>#include <cstring>#include <iostream>#include <algorithm>using namespace std;const double PI = acos(-1.0);const int maxn = 10000 + 10;const int max_size = (1<<15) - 1;typedef struct vect{    double x,y,angle;//angle是左孩子向量与右孩子向量之间的角度,这与线段与线段之间的角度是不同的}vect;vect v[max_size];int n,c;int len[maxn];double nowangle[maxn];//nowangle[i]代表第i与第i+1条线段之间现在的夹角,初始值为PIvoid init(int k,int l,int r){    v[k].angle=v[k].x=0;//起初所有的向量之间的夹角都是0    if(l==r){        v[k].y=len[l];    }    else{        int lch=k*2,rch=k*2+1;        int mid=(l+r)/2;        init(lch,l,mid);        init(rch,mid+1,r);        v[k].y = v[lch].y + v[rch].y;    }    return;}void change(int s,double a,int vloc,int l,int r){//这里的a是一个相对的变化角度    //printf("s:%d a:%.2f vloc:%d l:%d r:%d\n",s,a,vloc,l,r);    if(s<l) return;    else if(s>=r) return;//凡是需要修改的节点与该向量代表的区间没有关系时就不需要计算了    else if(s<r){        int lch=vloc*2,rch=vloc*2+1;        int mid=(l+r)/2;        change(s,a,lch,l,mid);        change(s,a,rch,mid+1,r);        if(s<=mid) v[vloc].angle+=a;//当给出的变化发生在当前左孩子与右孩子向量的夹角上或者左孩子中时,我们需要把这个变化记录下来,并且向上传递        double si = sin(v[vloc].angle),co = cos(v[vloc].angle);        v[vloc].x = v[lch].x + (co*v[rch].x - si*v[rch].y);        v[vloc].y = v[lch].y + (si*v[rch].x + co*v[rch].y);    }    return;}int main(){    while(scanf("%d%d",&n,&c)==2){        for(int i=0;i<maxn;i++) nowangle[i]=PI;        for(int i=1;i<=n;i++) scanf("%d",&len[i]);        init(1,1,n);        while(c--){            int s,a;            scanf("%d%d",&s,&a);            double ta = a/180.0*PI;            change(s,ta-nowangle[s],1,1,n);            nowangle[s]=ta;            for(int i=1;i<(1<<n);i++) printf("%.2f ",v[i].angle);            cout << endl;            printf("%.2f %.2f\n",v[1].x,v[1].y);        }        printf("\n");    }    return 0;}
0 0
原创粉丝点击