(线段树)poj2991-Crane

Crane
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6363 Accepted: 1723 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., 180 o. 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

 
 1 #include <iostream>
 2 //#include<bits/stdc++.h>
 3 #include <stack>
 4 #include <queue>
 5 #include <map>
 6 #include <set>
 7 #include <cstdio>
 8 #include <cstring>
 9 #include <algorithm>
10 #include <math.h>
11 using namespace std;
12 typedef long long ll;
13 typedef unsigned long long ull;
14 #define M_PI 3.14159265358979323846
15 const int MAX=(1<<15)-1;
16 int N,C;
17 int L[MAX];//各部分长度
18 int S[MAX],A[MAX];//改变的部分编号,角度
19 double vx[MAX],vy[MAX];//各个结点向量的水平、竖直分量
20 double ang[MAX];//各个结点向量与竖直方向的夹角
21 double prv[MAX];
22 void init(int k,int l,int r)
23 {
24     ang[k]=vx[k]=0.0;
25     if(l+1==r)
26         vy[k]=L[l];
27     else{
28         init(2*k+1,l,(l+r)/2);
29         init(2*k+2,(l+r)/2,r);
30         vy[k]=vy[2*k+2]+vy[2*k+1];
31     }
32 }
33 void change(int s,double a,int v,int l,int r)//将s和s+1角度改为a
34 {
35     if(s<=l)
36         return;
37     if(s<r)
38     {
39         change(s,a,2*v+1,l,(l+r)/2);
40         change(s,a,2*v+2,(l+r)/2,r);
41         if(s<=(l+r)/2)//why??
42             ang[v]+=a;
43         double s=sin(ang[v]),c=cos(ang[v]);
44         vx[v]=vx[2*v+1]+(c*vx[2*v+2]-s*vy[2*v+2]);
45         vy[v]=vy[2*v+1]+(s*vx[2*v+2]+c*vy[2*v+2]);
46     }
47 }
48 void solve()
49 {
50     for(int i=0;i<N;i++)
51         scanf("%d",&L[i]);
52     for(int i=0;i<C;i++)
53     {
54         scanf("%d %d",&S[i],&A[i]);}
55     init(0,0,N);
56     for(int i=1;i<N;i++)
57         prv[i]=M_PI;
58     for(int i=0;i<C;i++)
59        {
60         int s=S[i];
61         double a=A[i]/360.0*2*M_PI;
62         change(s,a-prv[s],0,0,N);
63         prv[s]=a;
64         printf("%.2f %.2f\n",vx[0],vy[0]);
65     }
66 }
67 int main()
68 {
69     bool st=false;
70     while(scanf("%d %d",&N,&C)==2)
71     {
72         if(st)
73             printf("\n");
74         st=true;
75         solve();
76     }
77 }

 

转载于:https://www.cnblogs.com/quintessence/p/6426562.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值