poj2991 Crane(线段树)

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

CTU Open 2005

题目大意:

题解:

我是没想到是线段树,看了是线段树后自己yy了一个建线段树的方法,好像跟书上的不一样。

书上的方法:

我的代码:

 1 program rrr(input,output);
 2 const
 3   eps=1e-10;
 4 type
 5   treetype=record
 6      l,r:longint;
 7      x,y,d:double;
 8   end;
 9 var
10   a:array[0..40040]of treetype;
11   c:array[0..10010]of double;
12   b:array[0..10010]of longint;
13   n,m,i,x:longint;
14   y,t,xx,yy:double;
15 procedure build(k,l,r:longint);
16 var
17   mid,i:longint;
18 begin
19    a[k].l:=l;a[k].r:=r;a[k].x:=0;a[k].d:=0;
20    if l=r then begin a[k].y:=b[l];exit; end;
21    mid:=(l+r)>>1;i:=k+k;
22    build(i,l,mid);build(i+1,mid+1,r);
23    a[k].y:=a[i].y+a[i+1].y;
24 end;
25 procedure pushdown(k:longint);
26 var
27   i:longint;
28 begin
29    if a[k].l=a[k].r then a[k].d:=0;
30    if abs(a[k].d)<eps then exit;
31    i:=k+k;
32    xx:=a[i].x*cos(a[k].d)-a[i].y*sin(a[k].d);
33    yy:=a[i].x*sin(a[k].d)+a[i].y*cos(a[k].d);
34    a[i].x:=xx;a[i].y:=yy;a[i].d:=a[i].d+a[k].d;
35    inc(i);
36    xx:=a[i].x*cos(a[k].d)-a[i].y*sin(a[k].d);
37    yy:=a[i].x*sin(a[k].d)+a[i].y*cos(a[k].d);
38    a[i].x:=xx;a[i].y:=yy;a[i].d:=a[i].d+a[k].d;
39    a[k].d:=0;
40 end;
41 procedure change(k:longint);
42 var
43   mid,i:longint;
44 begin
45    pushdown(k);
46    if x<a[k].l then
47       begin
48          xx:=a[k].x*cos(t)-a[k].y*sin(t);
49          yy:=a[k].x*sin(t)+a[k].y*cos(t);
50          a[k].x:=xx;a[k].y:=yy;
51          a[k].d:=t;
52          exit;
53       end;
54    mid:=(a[k].l+a[k].r)>>1;i:=k+k;
55    if x<mid then change(i);
56    change(i+1);
57    a[k].x:=a[i].x+a[i+1].x;a[k].y:=a[i].y+a[i+1].y;
58 end;
59 begin
60    assign(input,‘r.in‘);assign(output,‘r.out‘);reset(input);rewrite(output);
61    while not eof do
62       begin
63          readln(n,m);if (n=0) and (m=0) then break;
64          for i:=1 to n do begin read(b[i]);c[i]:=pi; end;
65          build(1,1,n);
66          for i:=1 to m do
67             begin
68                readln(x,y);t:=y/180*pi-c[x];c[x]:=y/180*pi;
69                change(1);
70                writeln(a[1].x:0:2,‘ ‘,a[1].y:0:2);
71             end;
72          writeln;
73       end;
74    close(input);close(output);
75 end.
时间: 2024-10-13 04:11:00

poj2991 Crane(线段树)的相关文章

poj 2991 Crane(线段树)

题目链接:poj 2991 Crane 题目大意:就是有一个机械手臂,有n结,给定每节的长度,一开始为垂直的.有m次操作,每次将x关节变成角度d,并且输出手臂末端的坐标. 解题思路:点的旋转公式(r为逆时针的角度): x′=x?cos(r)?y?sin(r) y′=x?sin(r)+y?cos(r) 没有做过类似的题目,线段树每个节点记录的为每节旋转的角度以及单节末端的位置.每次旋转新的角度,需要根据前一个节点的角度和当前节点的角度来计算(因为它不是再旋转d,而是变成角度d #include <

北大ACM2991——Crane~~线段树

最近看到了线段树,对于线段树也是有了初步的了解,还是需要时间继续研究,加深理解. 感觉线段树,个人觉得最主要的是递归过程的理解. 这一题,给定一段绳子," 分成 "N段,起初,每段绳子都是垂直的.然后有C个命令,每个命令包含两个数 i  ,  j,i  是第几段绳子,j 是 i 段绳子旋转到  i + 1 段绳子多经过的角度.也就是  i  和 i + 1 之间的角度是  j. 可以理解成向量,起初每个向量方向向上. 如果结点 i 表示的向量是vx1, vy1.角度是angle,两个儿

(中等) POJ 2991 Crane , 几何+线段树。

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 t

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

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 t

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

POJ 2991 Crane 题目链接 题意:给定一个垂直的挖掘机臂,有n段,现在每次操作可以旋转一个位置,把[s, s + 1]专程a度,每次旋转后要输出第n个位置的坐标 思路:线段树,把每一段当成一个向量,这样每一段的坐标就等于前几段的坐标和,然后每次旋转的时候,相当于把当前到最后位置全部加上一个角度,这样就需要区间修改了,然后每次还需要查询s,和s + 1当前的角度,所以需要单点查询,这样用线段树去维护即可 代码: #include <cstdio> #include <cstri

POJ 2991 Crane(线段树&#183;向量旋转)

题意  有一个Crane由n条线段连接组成  每个连接点处均可以任意旋转  给你n条线段的长度  然后又m次旋转操作  给你p和r  将第p和第p+1条线段之间的角度旋转为r  即第p条线段绕p的终点逆时针旋转r度后能够与第p+1条重合  问每次旋转后最后一条线段的终点坐标 可以发现  旋转第p+1条线段时  p+1后面的所有线段也一起旋转了  可以把Crane分解为n个向量  这些向量的和也就是Crane终点的坐标  用rad[i]保存第i个向量与第i+1个向量之间的夹角  每次旋转操作时  

Crane(线段树)

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 t

线段树题目总结

一.单点更新 1.hdu1166 敌兵布阵:有N个兵营,每个兵营都给出了人数ai(下标从1开始),有四种命令,(1)"Addij",表示第i个营地增加j人.(2)"Sub i j",表示第i个营地减少j人.(3)"Query ij",查询第i个营地到第j个营地的总人数.(4)"End",表示命令结束.解题报告Here. 2.hdu1754 I Hate It:给你N个数,M个操作,操作分两类.(1)"QAB"

数据结构---线段树

线段树 转载请注明出处,谢谢!http://blog.csdn.net/metalseed/article/details/8039326  持续更新中···   一:线段树基本概念 1:概述 线段树,类似区间树,是一个完全二叉树,它在各个节点保存一条线段(数组中的一段子数组),主要用于高效解决连续区间的动态查询问题,由于二叉结构的特性,它基本能保持每个操作的复杂度为O(lgN)! 性质:父亲的区间是[a,b],(c=(a+b)/2)左儿子的区间是[a,c],右儿子的区间是[c+1,b],线段树