• POJ 2991 Crane(线段树)


    Crane
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 7687   Accepted: 2075   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

     
    节点i表示的向量是vxi,vyi,角度是angi,两个儿子节点是chl和chr,
     
    vx[i]=vx[chl]+ (cos(angi)*vx[chr]-sin(angi)*vy[chr]);
    vy[i]=vy[chl]+ (sin(angi)*vx[chr]+cos(angi)*vy[chr]);
     
     1 #include <iostream>  
     2 #include <cmath>  
     3 #include <cstdio>  
     4 #define MAX_N 10000  
     5 #define M_PI 3.14159265358979323846  
     6 using namespace std;  
     7   
     8 const int ST_SIZE=(1<<15)-1;  
     9 //输入  
    10 int N,C;  
    11 int L[MAX_N+5];  
    12 int S[MAX_N+5],A[MAX_N+5];  
    13   
    14 //线段树所维护的数据  
    15 double vx[ST_SIZE],vy[ST_SIZE];  
    16 double ang[ST_SIZE];  
    17   
    18 //为了查询角度的变化而保存的当前角度的数组  
    19 double prv[MAX_N];  
    20   
    21 //初始化线段树  
    22 //k是节点的编号,l,r表示当前节点对应的是[l,r]区间  
    23 void init(int k,int l,int r)  
    24 {  
    25     ang[k]=vx[k]=0.0;  
    26     if(r-l==1)  
    27     {  
    28         //叶子节点  
    29         vy[k]=L[l];  
    30     }  
    31     else  
    32     {  
    33         //非叶子节点  
    34         int chl=k*2+1,chr=k*2+2;  
    35         init(chl,l,(l+r)/2);  
    36         init(chr,(l+r)/2,r);  
    37         vy[k]=vy[chl]+vy[chr];  
    38     }  
    39 }  
    40 //把s和s+1的角度变为a  
    41 //v是节点的编号,,l,r表示当前节点对应的是[l,r]区间  
    42 void change(int s,double a,int v,int l,int r)  
    43 {  
    44     if(s<=l)  
    45         return;  
    46     else if(s<r)  
    47     {  
    48         int chl=v*2+1,chr=v*2+2;  
    49         int m=(l+r)/2;  
    50         change(s,a,chl,l,m);  
    51         change(s,a,chr,m,r);  
    52         if(s<=m)  
    53             ang[v]+=a;  
    54         double s=sin(ang[v]),c=cos(ang[v]);     //围绕原点的旋转:  
    55         vx[v]=vx[chl]+(c*vx[chr]-s*vy[chr]);    //x' = x * cos(a) - y * sin(a)  
    56         vy[v]=vy[chl]+(s*vx[chr]+c*vy[chr]);    //y' = x * sin(a) + y * cos(a)  
    57     }  
    58   
    59 }  
    60 void solve()  
    61 {  
    62     //初始化  
    63     init(0,0,N);  
    64     for(int i=1;i<N;i++)  
    65         prv[i]=M_PI;  
    66     //处理操作  
    67     for(int i=0;i<C;i++)  
    68     {  
    69         int s=S[i];  
    70         double a=A[i]/360.0*2*M_PI;     //把角度换算为弧度  
    71         change(s,a-prv[s],0,0,N);  
    72         prv[s]=a;  
    73         printf("%.2f %.2f
    ",vx[0],vy[0]);  
    74   
    75     }  
    76   
    77   
    78 }  
    79 int main()  
    80 {  
    81     while(cin>>N>>C)  
    82     {  
    83         for(int i=0;i<N;i++)  
    84             scanf("%d",&L[i]);  
    85         for(int i=0;i<C;i++)  
    86             scanf("%d%d",&S[i],&A[i]);  
    87         solve();  
    88   
    89     }  
    90   
    91     return 0;  
    92 }  
  • 相关阅读:
    C#中的赋值运算符及其运算规则
    值类型与引用类型
    变量的作用域和生存周期
    Eclipse 和 Vim 协同使用使用Eclim插件开发Python程序
    使用合适的设计模式一步步优化前端代码
    Vue.js 服务端渲染业务入门实践
    翻译连载 | 第 9 章:递归(下)-《JavaScript轻量级函数式编程》 |《你不知道的JS》姊妹篇
    翻译连载 |《你不知道的JS》姊妹篇 |《JavaScript 轻量级函数式编程》 第 7 章: 闭包 vs 对象
    翻译连载 | 第 10 章:异步的函数式(下)-《JavaScript轻量级函数式编程》 |《你不知道的JS》姊妹篇
    微信小程序教学第四章第二节(含视频):小程序中级实战教程:详情视图渲染
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13271161.html
Copyright © 2020-2023  润新知