POJ 2991 Crane 线段树

题目链接:http://poj.org/problem?id=2991

挑战程序设计竞赛书上的例题。书上的思路还是蛮巧妙的,第一遍看真的没怎么看太懂,今天算是终于弄懂了。

稍微思考即可想到如果都用向量表示的话实际上就是从s+1到n的区间更新了,每个向量都旋转一定角度最后终点坐标实际上是全部向量的和。因此可以使用线段树进行区间更新。

书上更新的时候实际上是----对于包含了s的区间更新坐标,不包含s的区间只设置一个标志表示这一段区间已经旋转过了一定角度,每次需要更新的时候直接用这个角度计算即可。

另一方面就是旋转后坐标计算公式,没啥难度。。。。

代码:

 1 const int maxn = 1e4 + 5;
 2 const double PI = acos(-1);
 3 struct node{
 4     double x, y;
 5     double ang;
 6 };
 7 node tree[maxn * 4];
 8 int n, m;
 9 int len[maxn], sum[maxn];
10 double angles[maxn];
11 
12 void build(int l, int r, int k){
13     tree[k].x = 0; tree[k].y = sum[r] - sum[l - 1];
14     tree[k].ang = 0;
15     if(l == r) return;
16     int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1;
17     build(l, mid, lc);
18     build(mid + 1, r, rc);
19 }
20 void update(int s, double ang, int l, int r, int k){
21     if(s < l || s > r) return;
22     if(l == r) return;
23     int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1;
24     update(s, ang, l, mid, lc);
25     update(s, ang, mid + 1, r, rc);
26     if(s <= mid) tree[k].ang += ang;
27     ang = tree[k].ang;
28     tree[k].x = tree[lc].x + tree[rc].x * cos(ang) - tree[rc].y * sin(ang);
29     tree[k].y = tree[lc].y + tree[rc].x * sin(ang) + tree[rc].y * cos(ang); 
30 }
31 
32 int main(){
33     bool first = true;
34     while(scanf("%d %d", &n, &m) != EOF){
35         if(!first)    puts("");
36         first = false;
37         memset(len, 0, sizeof(len));
38         sum[0] = 0;
39         for(int i = 1; i <= n; i++) {
40             scanf("%d", &len[i]);
41             sum[i] = sum[i - 1] + len[i]; 
42             angles[i] = PI;
43         }
44         build(1, n, 1);
45         while(m--){
46             int s;
47             double ang;
48             scanf("%d %lf", &s, &ang);
49             ang = ang / 180 * PI;
50             double x = 0, y = 0;
51             update(s, ang - angles[s], 1, n, 1);
52             angles[s] = ang;
53             x = tree[1].x; y = tree[1].y;
54             printf("%.2f %.2f\n", x, y);
55         } 
56     }
57 }

 

题目:

Crane
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 6807 Accepted: 1822 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值