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 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节的机械手,每次让某个节点移动使得第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;
}



版权声明:本文博客原创文章,博客,未经同意,不得转载。

Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
项目描述:建立购物小商城平台. 实现了前台页面系统。 技术描述:通过Spring 主框架来管理Struts2和Hibernate 框架搭建的电商小平台,用MySQL数据库并创建了表有用户表,订单表,商品表,商品分类表,商品内容表,购物车表等来存储数据。用到hibernate….zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值