クレーンPOJ - 2991年の更新間隔+計算幾何

まず、コンテンツ

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.

入力

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

出力

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. 

サンプル入力

2 1
10 5
1 90

3 2
5 5 5
1 270
2 90

サンプル出力

5.00 10.00

-10.00 5.00
-5.00 10.00

第二に、アイデア

  • 問題の意味:n個のセグメントが与えられると、各セグメントは、ノードを選択するたびに(すなわち、この後者が回転ノードでなければならない)、初期状態と終了の端に垂直であり、最終的に最後のノードの座標であります
  • 入力は、段落や段落S、S + 1が変更された差分aと度の角度を意味します。
  • したがって、予め記憶された差角一旦、必要な回転角度を算出します。次いで、回転の角度を更新する間隔が必要。
  • 各区間のベクトルである木のラインを保存します。

第三に、コード

#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int N = 1e4 + 5;
const double PI = acos(-1.0);
struct Node {
	double x, y, add;//add懒标记 
} tr[N << 2];
int n, m, len[N], s, a;//pre代表与i关节上一次的度数  题目求的是旋转使i与i+1的度数之差为a 并不是旋转a度 
double pre[N], si, co;
void pushup(int id) { //合并2个区间的向量 
	tr[id].x = tr[id << 1].x + tr[id << 1 | 1].x;
	tr[id].y = tr[id << 1].y + tr[id << 1 | 1].y;
}
void pushdown(int id) {
	if (tr[id].add == 0) return;
	//计算出旋转后的向量坐标 
	Node &l = tr[id << 1], &r = tr[id << 1 | 1]; 
	double x, y;
	double a = tr[id].add, co = cos(a), si = sin(a);
	x = co * l.x - si * l.y;
	y = si * l.x + co * l.y;
	l.x = x, l.y = y;
	x = co * r.x - si * r.y;
	y = si * r.x + co * r.y;
	r.x = x, r.y = y;
	l.add += tr[id].add; r.add += tr[id].add;
	tr[id].add = 0;
}
void build(int id, int l, int r) {
	tr[id].add = tr[id].x = 0; 
	if (l == r) {
		tr[id].y = len[l];
		return; 
	}
	int mid = (l + r) >> 1;
	build(id << 1, l, mid);
	build(id << 1 | 1, mid + 1, r);
	pushup(id); 
}
void update(int id, int l, int r, int x, int y, double a) {
	double xx, yy;
	if (x <= l && r <= y) {
		//旋转a度后这个向量的x,y坐标的变化
		xx = cos(a) * tr[id].x - sin(a) * tr[id].y;
		yy = sin(a) * tr[id].x + cos(a) * tr[id].y;
		tr[id].x = xx, tr[id].y = yy; 
		tr[id].add += a;
		return;
	}
	pushdown(id);
	int mid = (l + r) >> 1;
	if (x <= mid) update(id << 1, l, mid, x, y, a);
	if (y > mid) update(id << 1 | 1, mid + 1, r, x, y, a);
	pushup(id); 
}
int main() {
	while (~scanf("%d%d", &n, &m)) {
		for (int i = 1; i <= n; i++) scanf("%d", &len[i]), pre[i] = PI; //初始i与i+1这2个向量角度之差默认为180度
		build(1, 1, n); 
		for (int i = 1; i <= m; i++) {
			scanf("%d%d", &s, &a);
			double rad = a / 360.0 * 2 * PI; 
			update(1, 1, n, s + 1, n, rad - pre[s]);
			pre[s] = rad; //更新s与s+1之间的角度差 
			printf("%.2f %.2f\n", tr[1].x, tr[1].y); 
		}
		printf("\n");
	}
	return 0;
}
公開された351元の記事 ウォンの賞賛277 ・は 50000 +を見て

おすすめ

転載: blog.csdn.net/qq_41280600/article/details/104081208