POJ-2991-Crane -线段树
发布日期:2022-02-10 08:11:09 浏览次数:10 分类:技术文章

本文共 3169 字,大约阅读时间需要 10 分钟。

Crane

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

这个题是《挑战程序设计竞赛》的例题,看了一上午,终于看懂了(●ˇ∀ˇ●)

题意:有n条线段首尾相连,且已知每条线段的长度,执行C条指令,第i条指令给出Si和Ai,使线段Si和Si+1之间的角度变为Ai度,最开始所有角度都为180度。按顺序输出每条指令执行后线段末端的坐标
解题思路:用线段树维护每个节点的向量和角度

下面大部分是书上的代码…

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define maxn 10005typedef long long ll;using namespace std;typedef pair
P;const double PI=acos(-1.0); //Π的精确值const int ST_size = (1<<15)-1;int n,c,L[maxn],S[maxn],A[maxn]; //S和A保存指令double vx[ST_size],vy[ST_size]; //保存节点的向量double ang[ST_size]; //保存节点变化的角度double prv[maxn]; //保存线段s和si当前的角度void init(int k,int l,int r) //k为节点的编号,l,r表示当前节点对应的区间为[l,r){ ang[k]=vx[k]=0.0; if(r-l==1) vy[k]=L[l]; else { int chl=k*2+1,chr=k*2+2; init(chl,l,(l+r)/2); init(chr,(l+r)/2,r); vy[k]=vy[chl]+vy[chr]; }}void change(int s,double a,int v,int l,int r) //参数s表示变s和s+1的角度,参数a表示变化的角度,v表示节点的编号{ if(s<=l) return ; else if(s

转载地址:https://blog.csdn.net/qq_44632981/article/details/97895691 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:POJ-3126-Prime Path-素数-BFS
下一篇:Aizu - 0558-Cheese-BFS

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月17日 01时55分35秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章