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
 
 
题目大意:
poj2991 Crane(线段树)

poj2991 Crane(线段树)

题解:

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

书上的方法:

poj2991 Crane(线段树)

我的代码:

 program rrr(input,output);
const
eps=1e-10;
type
treetype=record
l,r:longint;
x,y,d:double;
end;
var
a:array[..]of treetype;
c:array[..]of double;
b:array[..]of longint;
n,m,i,x:longint;
y,t,xx,yy:double;
procedure build(k,l,r:longint);
var
mid,i:longint;
begin
a[k].l:=l;a[k].r:=r;a[k].x:=;a[k].d:=;
if l=r then begin a[k].y:=b[l];exit; end;
mid:=(l+r)>>;i:=k+k;
build(i,l,mid);build(i+,mid+,r);
a[k].y:=a[i].y+a[i+].y;
end;
procedure pushdown(k:longint);
var
i:longint;
begin
if a[k].l=a[k].r then a[k].d:=;
if abs(a[k].d)<eps then exit;
i:=k+k;
xx:=a[i].x*cos(a[k].d)-a[i].y*sin(a[k].d);
yy:=a[i].x*sin(a[k].d)+a[i].y*cos(a[k].d);
a[i].x:=xx;a[i].y:=yy;a[i].d:=a[i].d+a[k].d;
inc(i);
xx:=a[i].x*cos(a[k].d)-a[i].y*sin(a[k].d);
yy:=a[i].x*sin(a[k].d)+a[i].y*cos(a[k].d);
a[i].x:=xx;a[i].y:=yy;a[i].d:=a[i].d+a[k].d;
a[k].d:=;
end;
procedure change(k:longint);
var
mid,i:longint;
begin
pushdown(k);
if x<a[k].l then
begin
xx:=a[k].x*cos(t)-a[k].y*sin(t);
yy:=a[k].x*sin(t)+a[k].y*cos(t);
a[k].x:=xx;a[k].y:=yy;
a[k].d:=t;
exit;
end;
mid:=(a[k].l+a[k].r)>>;i:=k+k;
if x<mid then change(i);
change(i+);
a[k].x:=a[i].x+a[i+].x;a[k].y:=a[i].y+a[i+].y;
end;
begin
assign(input,'r.in');assign(output,'r.out');reset(input);rewrite(output);
while not eof do
begin
readln(n,m);if (n=) and (m=) then break;
for i:= to n do begin read(b[i]);c[i]:=pi; end;
build(,,n);
for i:= to m do
begin
readln(x,y);t:=y/*pi-c[x];c[x]:=y/*pi;
change();
writeln(a[].x::,' ',a[].y::);
end;
writeln;
end;
close(input);close(output);
end.
上一篇:kepware http接口 c语言 ruby


下一篇:cisco 交换机设置时区、时间、同步日志本地时间