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., 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

Source

题意:有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;
}


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值