POJ 2991 Crane(線段樹+計算幾何)

weixin_34377065發表於2016-01-12

POJ 2991 Crane

題目連結

題意:給定一個垂直的挖掘機臂。有n段,如今每次操作能夠旋轉一個位置,把[s, s + 1]專程a度,每次旋轉後要輸出第n個位置的座標

思路:線段樹。把每一段當成一個向量,這樣每一段的座標就等於前幾段的座標和,然後每次旋轉的時候,相當於把當前到最後位置所有加上一個角度,這樣就須要區間改動了。然後每次還須要查詢s,和s + 1當前的角度,所以須要單點查詢,這樣用線段樹去維護就可以

程式碼:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

const int N = 10005;
const double PI = acos(-1.0);

#define lson(x) ((x<<1)+1)
#define rson(x) ((x<<1)+2)

int n, m;
double COS[721], SIN[721];

struct Node {
	int l, r, lazy, R;
	double x, y;
	void gao(int ang) {
		R = (R + ang) % 360;
		lazy = (lazy + ang) % 360;
		double tmpx = x, tmpy = y;
		x = COS[ang + 360] * tmpx - SIN[ang + 360] * tmpy;
		y = SIN[ang + 360] * tmpx + COS[ang + 360] * tmpy;
	}
} node[N * 4];

void pushup(int x) {
	node[x].x = node[lson(x)].x + node[rson(x)].x;
	node[x].y = node[lson(x)].y + node[rson(x)].y;
}

void pushdown(int x) {
	if (node[x].lazy) {
		node[lson(x)].gao(node[x].lazy);
		node[rson(x)].gao(node[x].lazy);
		node[x].lazy = 0;
	}
}

void build(int l, int r, int x = 0) {
	node[x].l = l; node[x].r = r; node[x].lazy = node[x].R = 0;
	if (l == r) {
		double tmp;
		scanf("%lf", &tmp);
		node[x].x = 0.0;
		node[x].y = tmp;
		return;
	}
	int mid = (l + r) / 2;
	build(l, mid, lson(x));
	build(mid + 1, r, rson(x));
	pushup(x);
}

void add(int l, int r, int v, int x = 0) {
	if (node[x].l >= l && node[x].r <= r) {
		node[x].gao(v);
		return;
	}
	int mid = (node[x].l + node[x].r) / 2;
	pushdown(x);
	if (l <= mid) add(l, r, v, lson(x));
	if (r > mid) add(l, r, v, rson(x));
	pushup(x);
}

int query(int v, int x = 0) {
	if (node[x].l == node[x].r)
		return node[x].R;
	int mid = (node[x].l + node[x].r) / 2;
	int ans = 0;
	pushdown(x);
	if (v <= mid) ans += query(v, lson(x));
	if (v > mid) ans += query(v, rson(x));
	pushup(x);
	return ans;
}

int main() {
	int bo = 0;
	for (int i = -360; i <= 360; i++) {
		COS[i + 360] = cos(i / 180.0 * PI);
		SIN[i + 360] = sin(i / 180.0 * PI);
	}
	while (~scanf("%d%d", &n, &m)) {
		if (bo) printf("\n");
		else bo = 1;
		build(1, n);
		int s, a;
		while (m--) {
			scanf("%d%d", &s, &a);
			int a1 = query(s);
			int a2 = query(s + 1);
			int ang = ((a1 - a2 + a + 180) % 360 + 360) % 360;
			add(s + 1, n, ang);
			printf("%.2lf %.2lf\n", node[0].x, node[0].y);
		}
	}
	return 0;
}


相關文章