Crane(POJ 2991)

  • 原题如下:
    Crane
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 8504   Accepted: 2297   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
  • 题解:本题可以使用线段树来解决,每个节点表示一段连续的线段的集合,并且维护下面两个值:①把对应的线段集合的第一条线段转至垂直方向之后,从第一条线段的起点指向最后一条线段的终点的向量,②如果该节点有儿子节点,两个儿子节点对应的部分连接之后,右儿子需要转动的角度。如果节点i表示的向量是vxi,vyi,角度是angi,两个儿子节点是chl和chr,那么就有,vxi=vxchl+(cos(angi)*vxchr-sin(angi)*vychr),vyi=vychl+(sin(angi)*vxchr+cos(angi)*vychr)。这样,每次更新就可以在O(logn)时间内完成,而输出的值就是根节点对应的向量的值。
  • 代码:
     1 #include <cstdio>
     2 #include <cctype>
     3 #include <cmath>
     4 #define number s-'0'
     5 
     6 using namespace std;
     7 
     8 const double M_PI=acos(-1.0);
     9 const int ST_SIZE=(1<<15)-1;
    10 const int MAX_N=20000;
    11 const int MAX_C=20000; 
    12 int N,C;
    13 int L[MAX_N];
    14 int S[MAX_C],A[MAX_C];
    15 double vx[ST_SIZE],vy[ST_SIZE];
    16 double ang[ST_SIZE];
    17 double prv[MAX_N]; 
    18 
    19 void read(int &x){
    20     char s;
    21     x=0;
    22     bool flag=0;
    23     while(!isdigit(s=getchar()))
    24         (s=='-')&&(flag=true);
    25     for(x=number;isdigit(s=getchar());x=x*10+number);
    26     (flag)&&(x=-x);
    27 }
    28 
    29 void write(int x)
    30 {
    31     if(x<0)
    32     {
    33         putchar('-');
    34         x=-x;
    35     }
    36     if(x>9)
    37         write(x/10);
    38     putchar(x%10+'0');
    39 }
    40 
    41 void init(int k, int l, int r);
    42 void change(int s, double a, int v, int l, int r);
    43 
    44 int main()
    45 {
    46     while (scanf("%d %d", &N, &C)==2)
    47     {
    48         for (int i=0; i<N; i++) scanf("%d", &L[i]);
    49         for (int i=0; i<C; i++)    scanf("%d %d", &S[i], &A[i]);
    50         init(0, 0, N);
    51         for (int i=0; i<N; i++) prv[i]=M_PI;
    52         for (int i=0; i<C; i++)
    53         {
    54             int s=S[i];
    55             double a=A[i]/360.0*2*M_PI;
    56             change(s, a-prv[s], 0, 0, N);
    57             prv[s]=a;
    58             printf("%.2f %.2f\n", vx[0], vy[0]);            
    59         }
    60 
    61     }
    62 }
    63 
    64 void init(int k, int l, int r)
    65 {
    66     vx[k]=ang[k]=0;
    67     if (r-l==1) vy[k]=L[l];
    68     else
    69     {
    70         int chl=2*k+1, chr=2*k+2, m=(l+r)/2;
    71         init(chl, l, m);
    72         init(chr, m, r);
    73         vy[k]=vy[chl]+vy[chr];
    74     }
    75 }
    76 
    77 void change(int s, double a, int v, int l, int r)
    78 {
    79     if (s<=l) return;
    80     else if (s<r)
    81     {
    82         int chl=v*2+1, chr=v*2+2, m=(l+r)/2;
    83         change(s, a, chl, l, m);
    84         change(s, a, chr, m, r);
    85         if (s<=m) ang[v]+=a;
    86         double s=sin(ang[v]), c=cos(ang[v]);
    87         vx[v]=vx[chl]+vx[chr]*c-vy[chr]*s;
    88         vy[v]=vy[chl]+vx[chr]*s+vy[chr]*c;
    89     }
    90 }

     

posted @ 2018-08-27 21:07  Umine  阅读(493)  评论(0编辑  收藏  举报