poj2991 Crane(线段树)
Posted Currier
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.
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).
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.
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.
以上是关于poj2991 Crane(线段树)的主要内容,如果未能解决你的问题,请参考以下文章