Crane POJ2991——线段树+几何

由于向量的可叠加性,这道题可以用线段树维护,配合一些几何的知识,代码还是挺好写的。

然而因为一个括号忘了去卡了好几个小时 ((٩(//̀Д/́/)۶)) ,果然应了那句话,改越久的BUG往往越ZZ

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 1e4 + 10;
const double PI = acos(-1.0);
int n, c;
struct Node {
    int angle;
    double x, y;
}node[maxn<<2];
int lazy[maxn<<2];
void change(int root, int a) {
    double angle = a * PI / 180;
    double x = node[root].x, y = node[root].y;
    node[root].x = x * cos(angle) - y * sin(angle);
    node[root].y = y * cos(angle) + x * sin(angle);
    node[root].angle = (node[root].angle + a) % 360;
}
void pushup(int root) {
    node[root].x = node[root<<1].x + node[root<<1|1].x;
    node[root].y = node[root<<1].y + node[root<<1|1].y;
}
void pushdown(int root) {
    if (!lazy[root]) return;
    change(root<<1, lazy[root]);
    change(root<<1|1, lazy[root]);
    lazy[root<<1] += lazy[root];
    lazy[root<<1|1] += lazy[root];
    lazy[root] = 0;
}
void build(int l, int r, int root) {
    node[root].angle = 0; node[root].x = 0; node[root].y = 0; lazy[root] = 0;
    if (l == r) {
        scanf("%lf", &node[root].y);
        return;
    }
    int mid = (l + r)>>1;
    build(l, mid, root<<1);
    build(mid + 1, r, root<<1|1);
    pushup(root);
}
void update(int l, int r, int root, int ll, int rr, int val) {
    if (ll <= l && r <= rr) {
        change(root, val);
        lazy[root] += val;
        return;
    }
    pushdown(root);
    int mid = (l + r)>>1;
    if (ll <= mid) update(l, mid, root<<1, ll, rr, val);
    if (mid < rr) update(mid + 1, r, root<<1|1, ll, rr, val);
    pushup(root);
}
int query(int l, int r, int root, int pos) {
    if (l == r) return node[root].angle;
    int mid = (l + r)>>1;
    pushdown(root);
    if (pos <= mid) return query(l, mid, root<<1, pos);
    else return query(mid + 1, r, root<<1|1, pos);
}

int main() {
    int flag = 0;
    while (~scanf("%d %d", &n, &c)) {
        if (flag++) printf("\n");
        build(1, n, 1);
        int s, a;
        while (c--) {
            scanf("%d %d", &s, &a);
            int angle = 180+a-query(1, n, 1, s+1)+query(1, n, 1, s);
            update(1, n, 1, s + 1, n, angle);
            printf("%.2lf %.2lf\n", node[1].x, node[1].y);
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值