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

2024-06-05 21:48
文章标签 计算 poj 几何 线段 crane 2991

本文主要是介绍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 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 903 2
5 5 5
1 270
2 90

Sample Output

5.00 10.00-10.00 5.00
-5.00 10.00

题意:有一个n节的机械手,每次让某个节点移动使得第i条边和第i+1条边的夹角是a,初始化夹角都是180度,求经过操作后最后一个节点位置

思路:同样是线段树的区间更新,因为要求的是最后一个点的坐标,我们知道向量是可以相加的,那么我们把每个线段当成向量相加的话,就可以得到最后的坐标了,再者就是旋转的问题了,我们同样知道旋转后,第i+1后的边都会受到影响,这就是线段树的区间更新了;然后有了逆时针向量旋转的公式我们就可以得到解了,注意的地方是:我们每次储存上一次的角度,依次来推出这次需要旋转的角度,还有就是不知道会平白无故W了还多次,还是换个姿势做的

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define lson(x) ((x) << 1)
#define rson(x) ((x) << 1 | 1)
using namespace std;
const int maxn = 10002;int angle[maxn], setv[maxn<<2];
double sumx[maxn<<2], sumy[maxn<<2];void update(int pos) {sumx[pos] = sumx[lson(pos)] + sumx[rson(pos)];sumy[pos] = sumy[lson(pos)] + sumy[rson(pos)];
}void build(int l, int r, int pos) {setv[pos] = 0;if (l == r) {scanf("%lf", &sumy[pos]);sumx[pos] = 0;return;}int m = l + r >> 1;build(l, m, lson(pos));build(m+1, r, rson(pos));update(pos);
}void change(int pos, int d) {double tmp = d * acos(-1.0) / 180;double x = sumx[pos]*cos(tmp) - sumy[pos]*sin(tmp);double y = sumx[pos]*sin(tmp) + sumy[pos]*cos(tmp);sumx[pos] = x;sumy[pos] = y;
}void push(int pos) {if (setv[pos]) {setv[lson(pos)] += setv[pos];setv[rson(pos)] += setv[pos];change(lson(pos), setv[pos]);change(rson(pos), setv[pos]);setv[pos] = 0;}
}void modify(int l, int r, int pos, int x, int y, int z) {if (x <= l && y >= r) {setv[pos] += z;change(pos, z);return;}push(pos);int m = l + r >> 1;if (x <= m)modify(l, m, lson(pos), x, y, z);if (y > m)modify(m+1, r, rson(pos), x, y, z);update(pos);
}int main() {int n, q;int first = 1;while (scanf("%d%d", &n, &q) != EOF) {if (first)first = 0;else printf("\n");build(1, n, 1);for (int i = 1; i <= n; i++)angle[i] = 180;int a, b;while (q--) {scanf("%d%d", &a, &b);modify(1, n, 1, a+1, n, b-angle[a]);angle[a] = b;printf("%.2lf %.2lf\n", sumx[1], sumy[1]);}}return 0;
}



这篇关于POJ - 2991 Crane (线段树+计算几何)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1034255

相关文章

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Java计算经纬度距离的示例代码

《Java计算经纬度距离的示例代码》在Java中计算两个经纬度之间的距离,可以使用多种方法(代码示例均返回米为单位),文中整理了常用的5种方法,感兴趣的小伙伴可以了解一下... 目录1. Haversine公式(中等精度,推荐通用场景)2. 球面余弦定理(简单但精度较低)3. Vincenty公式(高精度,

windows和Linux使用命令行计算文件的MD5值

《windows和Linux使用命令行计算文件的MD5值》在Windows和Linux系统中,您可以使用命令行(终端或命令提示符)来计算文件的MD5值,文章介绍了在Windows和Linux/macO... 目录在Windows上:在linux或MACOS上:总结在Windows上:可以使用certuti

Python如何计算两个不同类型列表的相似度

《Python如何计算两个不同类型列表的相似度》在编程中,经常需要比较两个列表的相似度,尤其是当这两个列表包含不同类型的元素时,下面小编就来讲讲如何使用Python计算两个不同类型列表的相似度吧... 目录摘要引言数字类型相似度欧几里得距离曼哈顿距离字符串类型相似度Levenshtein距离Jaccard相

使用C#代码计算数学表达式实例

《使用C#代码计算数学表达式实例》这段文字主要讲述了如何使用C#语言来计算数学表达式,该程序通过使用Dictionary保存变量,定义了运算符优先级,并实现了EvaluateExpression方法来... 目录C#代码计算数学表达式该方法很长,因此我将分段描述下面的代码片段显示了下一步以下代码显示该方法如

如何用Java结合经纬度位置计算目标点的日出日落时间详解

《如何用Java结合经纬度位置计算目标点的日出日落时间详解》这篇文章主详细讲解了如何基于目标点的经纬度计算日出日落时间,提供了在线API和Java库两种计算方法,并通过实际案例展示了其应用,需要的朋友... 目录前言一、应用示例1、天安门升旗时间2、湖南省日出日落信息二、Java日出日落计算1、在线API2

poj3468(线段树成段更新模板题)

题意:包括两个操作:1、将[a.b]上的数字加上v;2、查询区间[a,b]上的和 下面的介绍是下解题思路: 首先介绍  lazy-tag思想:用一个变量记录每一个线段树节点的变化值,当这部分线段的一致性被破坏我们就将这个变化值传递给子区间,大大增加了线段树的效率。 比如现在需要对[a,b]区间值进行加c操作,那么就从根节点[1,n]开始调用update函数进行操作,如果刚好执行到一个子节点,

hdu1394(线段树点更新的应用)

题意:求一个序列经过一定的操作得到的序列的最小逆序数 这题会用到逆序数的一个性质,在0到n-1这些数字组成的乱序排列,将第一个数字A移到最后一位,得到的逆序数为res-a+(n-a-1) 知道上面的知识点后,可以用暴力来解 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#in

hdu1689(线段树成段更新)

两种操作:1、set区间[a,b]上数字为v;2、查询[ 1 , n ]上的sum 代码如下: #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdl

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO