POJ2991 Crane 【线段树+计算几何】

Crane

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3777   Accepted: 1031   Special Judge

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

题意:有n根长度不尽相同的棍子,初始时它们首尾垂直相连,标号为1--n,第一根棍子的下端坐标为(0,0),上端坐标为(0,len[1]),其余棍子依次类推。接下来执行C此旋转,每次输入一个编号num和角度rad,使得第num根棍子和第num+1跟棍子间的逆时针角度变为rad度(开始因为这里WA了好几次,因为我理解成第num+1棍子相对于num顺时针旋转了rad度..),求每次旋转后第n跟棍子端点的坐标。

题解:记录第num根棍子和第num+1跟棍子间的逆时针角度pre[num],这样每次输入rad时就能确定第num+1根及以后棍子的绝对旋转角度,然后用这个角度维护区间值。对于一个向量(x,y),它顺时针旋转A度后,得到的新向量为:x‘
= x * cos(A) + y * sin(A); y‘ = y * cos(A) - x * sin(A);

#include <stdio.h>
#include <string.h>
#include <math.h>

#define maxn 10002
#define esp 1e-9
#define lson l, mid, rt << 1
#define rson mid, r, rt << 1 | 1

const double PI = acos(-1.0);

struct Node {
	double x, y, lazy;
} T[maxn << 2];
int N, C;
double pre[maxn], degree[maxn];

void build(int l, int r, int rt) {
	T[rt].x = T[rt].lazy = 0.0;
	if(r - l == 1) {
		scanf("%lf", &T[rt].y);
		return;
	}
	int mid = l + r >> 1;
	build(lson);
	build(rson);
	T[rt].y = T[rt << 1].y + T[rt << 1 | 1].y;
}

void update(int L, int R, double rad, int l, int r, int rt) {
	double x, y;
	if(L == l && R == r) {
		x = T[rt].x * cos(rad) + T[rt].y * sin(rad);
		y = T[rt].y * cos(rad) - T[rt].x * sin(rad);
		T[rt].x = x; T[rt].y = y;
		T[rt].lazy += rad;
		return;
	}
	int mid = l + r >> 1;
	if(T[rt].lazy) {
		x = T[rt<<1].x * cos(T[rt].lazy) + T[rt<<1].y * sin(T[rt].lazy);
		y = T[rt<<1].y * cos(T[rt].lazy) - T[rt<<1].x * sin(T[rt].lazy);
		T[rt<<1].x = x; T[rt<<1].y = y;
		T[rt<<1].lazy += T[rt].lazy;
		x = T[rt<<1|1].x * cos(T[rt].lazy) + T[rt<<1|1].y * sin(T[rt].lazy);
		y = T[rt<<1|1].y * cos(T[rt].lazy) - T[rt<<1|1].x * sin(T[rt].lazy);
		T[rt<<1|1].x = x; T[rt<<1|1].y = y;
		T[rt<<1|1].lazy += T[rt].lazy;
		T[rt].lazy = 0.0;
	}
	if(R <= mid) update(L, R, rad, lson);
	else if(L >= mid) update(L, R, rad, rson);
	else {
		update(L, mid, rad, lson);
		update(mid, R, rad, rson);
	}
	T[rt].y = T[rt << 1].y + T[rt << 1 | 1].y;
	T[rt].x = T[rt << 1].x + T[rt << 1 | 1].x;
}

int main() {
	//freopen("stdin.txt", "r", stdin);
	int i, j, x, y, cas = 0;
	double rad, tmp;
	while(scanf("%d%d", &N, &C) == 2) {
		build(0, N, 1);
		if(cas++) printf("\n");
		for(i = 1; i <= N; ++i)
			pre[i] = PI; 

		while(C--) {
			scanf("%d%d", &x, &y);
			rad = y / 360.0 * 2 * PI;
			update(x, N, pre[x] - rad, 0, N, 1);
			pre[x] = rad;
			printf("%.2lf %.2lf\n", T[1].x, T[1].y);
		}
	}
	return 0;
}
时间: 2024-10-17 08:41:39

POJ2991 Crane 【线段树+计算几何】的相关文章

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 t

转载::POJ 2991 线段树+计算几何(有c++结构体操作)

POJ 2991 线段树+计算几何 (2011-02-27 21:13:44) 转载▼ 标签: 杂谈 分类: OI 话说这一题真的是很恶心很恶心,不过确实改变了我对线段树的一些看法,算是很经典的题目. 题意:有一个吊车由很多个不同长度的线段组成,一开始是一条长直线起点在(0,0),尾节点在(0,sum[n]),每条线段之间的夹角的初始值是180度.然后有一些操作a. b将第a条线段和a+1之间的夹角变成b度,经过每一次操作都要求出尾节点的坐标. 首先要用到一个计算几何的知识(没学过..请教而来)

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

POJ 2991 Crane 题目链接 题意:给定一个垂直的挖掘机臂,有n段,现在每次操作可以旋转一个位置,把[s, s + 1]专程a度,每次旋转后要输出第n个位置的坐标 思路:线段树,把每一段当成一个向量,这样每一段的坐标就等于前几段的坐标和,然后每次旋转的时候,相当于把当前到最后位置全部加上一个角度,这样就需要区间修改了,然后每次还需要查询s,和s + 1当前的角度,所以需要单点查询,这样用线段树去维护即可 代码: #include <cstdio> #include <cstri

poj 2991 Crane(线段树)

题目链接:poj 2991 Crane 题目大意:就是有一个机械手臂,有n结,给定每节的长度,一开始为垂直的.有m次操作,每次将x关节变成角度d,并且输出手臂末端的坐标. 解题思路:点的旋转公式(r为逆时针的角度): x′=x?cos(r)?y?sin(r) y′=x?sin(r)+y?cos(r) 没有做过类似的题目,线段树每个节点记录的为每节旋转的角度以及单节末端的位置.每次旋转新的角度,需要根据前一个节点的角度和当前节点的角度来计算(因为它不是再旋转d,而是变成角度d #include <

北大ACM2991——Crane~~线段树

最近看到了线段树,对于线段树也是有了初步的了解,还是需要时间继续研究,加深理解. 感觉线段树,个人觉得最主要的是递归过程的理解. 这一题,给定一段绳子," 分成 "N段,起初,每段绳子都是垂直的.然后有C个命令,每个命令包含两个数 i  ,  j,i  是第几段绳子,j 是 i 段绳子旋转到  i + 1 段绳子多经过的角度.也就是  i  和 i + 1 之间的角度是  j. 可以理解成向量,起初每个向量方向向上. 如果结点 i 表示的向量是vx1, vy1.角度是angle,两个儿

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 t

(中等) 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 t

POJ 2991 Crane(线段树&#183;向量旋转)

题意  有一个Crane由n条线段连接组成  每个连接点处均可以任意旋转  给你n条线段的长度  然后又m次旋转操作  给你p和r  将第p和第p+1条线段之间的角度旋转为r  即第p条线段绕p的终点逆时针旋转r度后能够与第p+1条重合  问每次旋转后最后一条线段的终点坐标 可以发现  旋转第p+1条线段时  p+1后面的所有线段也一起旋转了  可以把Crane分解为n个向量  这些向量的和也就是Crane终点的坐标  用rad[i]保存第i个向量与第i+1个向量之间的夹角  每次旋转操作时  

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 t