题目
题目链接:http://poj.org/problem?id=2991
题目来源:《挑战》例题。
简要题意:一根机械手,知道每段的长度,可以转动关节,每次转动一个,求顶端的向量。
题解
几何苦手,光为了理解坐标变换公式就弄了好久。
做法就是可以将到顶端的向量当做是每个段的向量的和。
旋转一个关节相当于之后所有的段都进行旋转。
于是可以成段去更新。
维护每一段相对于之前一段的角度,初始化为 180∘ 。
每次就是对 s+1 的位置去旋转 a−deg[s+1] 度。
错了很多次,出了较为致命的错误。
代码
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
// head
const int N = 1e5+5;
const double PI = acos(-1.0);
double degToR(int deg) {
return deg * PI / 180.0;
}
struct SegmentTree {
#define lson (rt<<1)
#define rson ((rt<<1)|1)
#define MID ((L+R)>>1)
#define lsonPara lson, L, MID
#define rsonPara rson, MID+1, R
const static int TN = N << 2;
double vx[TN], vy[TN];
int lazy[TN];
void spin(int rt, int ang) {
double a = degToR(ang);
double x = vx[rt], y = vy[rt];
double s = sin(a), c = cos(a);
vx[rt] = x * c - y * s;
vy[rt] = y * c + x * s;
}
void pushUp(int rt) {
vx[rt] = vx[lson] + vx[rson];
vy[rt] = vy[lson] + vy[rson];
}
void pushDown(int rt, int L, int R) {
if (lazy[rt]) {
spin(lson, lazy[rt]); // 应当对儿子旋转lazy[rt],致命逻辑错误导致多次wa
spin(rson, lazy[rt]);
lazy[lson] += lazy[rt];
lazy[rson] += lazy[rt];
lazy[rt] = 0;
}
}
void build(int rt, int L, int R) {
lazy[rt] = 0;
if (L == R) {
vx[rt] = 0;
scanf("%lf", vy+rt);
} else {
build(lsonPara);
build(rsonPara);
pushUp(rt);
}
}
void modify(int rt, int L, int R, int l, int r, int v) {
if (l > R || r < L) return;
if (l <= L && r >= R) {
lazy[rt] += v;
spin(rt, v);
} else {
pushDown(rt, L, R);
modify(lsonPara, l, r, v);
modify(rsonPara, l, r, v);
pushUp(rt);
}
}
};
SegmentTree st;
int deg[N];
int main() {
int n, m, s, a, cas = 1;
while (scanf("%d%d", &n, &m) == 2) {
for (int i = 1; i <= n; i++) {
deg[i] = 180;
}
st.build(1, 1, n);
if (cas++ > 1) puts("");
for (int i = 0; i < m; i++) {
scanf("%d%d", &s, &a);
st.modify(1, 1, n, s+1, n, a - deg[s+1]);
deg[s+1] = a;
printf("%.2f %.2f\n", st.vx[1], st.vy[1]);
}
}
return 0;
}