poj 2991 Crane 线段树 几何

题目

题目链接:http://poj.org/problem?id=2991

题目来源:《挑战》例题。

简要题意:一根机械手,知道每段的长度,可以转动关节,每次转动一个,求顶端的向量。

题解

几何苦手,光为了理解坐标变换公式就弄了好久。

做法就是可以将到顶端的向量当做是每个段的向量的和。

旋转一个关节相当于之后所有的段都进行旋转。

于是可以成段去更新。

维护每一段相对于之前一段的角度,初始化为 180

每次就是对 s+1 的位置去旋转 adeg[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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值