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

来源:互联网 发布:阳江淘宝贝幼儿园 编辑:程序博客网 时间:2024/05/21 07:09
Crane
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 4757 Accepted: 1287 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 110 51 903 25 5 51 2702 90

Sample Output

5.00 10.00-10.00 5.00-5.00 10.00

Source

CTU Open 2005

题目大意:一个n m,n代表有n个线段,开始依次按序号首位相连在y轴上,第一条线段的起点固定在(0,0),m次操作,每次操作将一个s a,就是把第s个线段和第s+1个线段夹角变成a(逆时针),求第n个坐标末尾坐标

思路:把每条线段看成一个向量,最后的坐标就是所有向量的和,对s和s+1操作,s+1到第n个都受影响所以用线段树区间的更新

ac代码

#include<stdio.h>#include<string.h>#include<algorithm>#include<stdlib.h>#include<math.h>using namespace std;double sum[10100],deg[10010];const double Pi=acos(-1.0);struct{    double x,y;    int flag;}node[10010<<2];void pushup(int tr){    node[tr].x=node[tr<<1].x+node[tr<<1|1].x;    node[tr].y=node[tr<<1].y+node[tr<<1|1].y;}void build(int l,int r,int tr){    node[tr].x=0;    node[tr].y=sum[r]-sum[l-1];    node[tr].flag=0;    if(l==r)        return;    int mid=(l+r)>>1;    build(l,mid,tr<<1);    build(mid+1,r,tr<<1|1);}double getrad(double x){    return x*Pi/180;}void Rotate(double &x,double &y,double rad){    double xx=x,yy=y;    x=xx*cos(rad)-yy*sin(rad);    y=xx*sin(rad)+yy*cos(rad);}void pushdown(int tr){    if(node[tr].flag)    {        double rad=getrad(node[tr].flag);        node[tr<<1].flag+=node[tr].flag;        node[tr<<1|1].flag+=node[tr].flag;        Rotate(node[tr<<1].x,node[tr<<1].y,rad);        Rotate(node[tr<<1|1].x,node[tr<<1|1].y,rad);        node[tr].flag=0;    }}void update(int L,int R,double ra,int l,int r,int tr){    if(L<=l&&r<=R)    {        double rad=getrad(ra);        Rotate(node[tr].x,node[tr].y,rad);        node[tr].flag+=ra;        return;    }    pushdown(tr);    int mid=(l+r)>>1;    if(L<=mid)        update(L,R,ra,l,mid,tr<<1);    if(R>mid)        update(L,R,ra,mid+1,r,tr<<1|1);    pushup(tr);}int main(){    int n,m;    while(scanf("%d%d",&n,&m)!=EOF)    {        int i;        memset(sum,0,sizeof(sum));        for(i=1;i<=n;i++)        {            double len;            scanf("%lf",&len);            sum[i]=sum[i-1]+len;            deg[i]=0;        }        build(1,n,1);        while(m--)        {            int k;            double d;            scanf("%d%lf",&k,&d);            k++;            d=d-180;            double dd=d-deg[k];            deg[k]=d;            update(k,n,dd,1,n,1);            printf("%.2f %.2f\n",node[1].x,node[1].y);        }    }}


0 0