poj2991【线段树维护向量】

我们知道当第i+1根棍子旋转的时候,i+1~n根棍子都是相对不动的,只是角度变了而已。
考虑维护每个区间内线段从起点指向终点的向量投影,那么我们要的答案就是整个区间的向量投影。
每次更新的时候只需要知道第i+1与前一个线段的夹角改变了多少,然后区间更新i+1~n的整体转角向量投影,最后别忘把更新的向量pushup上来。

#pragma GCC optimize(2)
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long ll;
const int maxn = 1e4+5;
const int mod = 1e9+9;
int Case = 1;
int n, m;
struct  node{
	int l, r;
	double x, y, lazy;
	int mid() {
		return (l+r)/2;
	}
}tr[maxn<<2];

void pushup(int rt) {
	tr[rt].x = tr[rt<<1].x + tr[rt<<1|1].x;
	tr[rt].y = tr[rt<<1].y + tr[rt<<1|1].y;
}

void build(int rt, int l, int r) {
	tr[rt].l = l;tr[rt].r = r;
	tr[rt].lazy = tr[rt].x = 0;
	if(l == r) {
		scanf("%lf", &tr[rt].y);
		return;
	}
	int mid = tr[rt].mid();
	build(rt<<1, l, mid);
	build(rt<<1|1, mid+1, r);
	pushup(rt);
}

void pushdown(int rt) {
	if(tr[rt].lazy) {
		double &arc = tr[rt].lazy;
		double x = tr[rt<<1].x*cos(arc) + tr[rt<<1].y*sin(arc);
		double y = tr[rt<<1].y*cos(arc) - tr[rt<<1].x*sin(arc);
		tr[rt<<1].x = x;tr[rt<<1].y = y;
		tr[rt<<1].lazy += arc;
		x = tr[rt<<1|1].x*cos(arc) + tr[rt<<1|1].y*sin(arc);
		y = tr[rt<<1|1].y*cos(arc) - tr[rt<<1|1].x*sin(arc);
		tr[rt<<1|1].x = x; tr[rt<<1|1].y = y;
		tr[rt<<1|1].lazy += arc;
		arc = 0;
	}
}

void update(int rt, int l, int r, double arc) {
	if(tr[rt].l == l && tr[rt].r == r) {
		double x = tr[rt].x*cos(arc) + tr[rt].y*sin(arc);
		double y = tr[rt].y*cos(arc) - tr[rt].x*sin(arc);
		tr[rt].x = x;tr[rt].y = y;
		tr[rt].lazy += arc;
		return;
	}
	int mid = tr[rt].mid();
	pushdown(rt);
	if(r <= mid) update(rt<<1, l, r, arc);
	else if(l > mid) update(rt<<1|1, l, r, arc);
	else {
		update(rt<<1, l, mid, arc);update(rt<<1|1, mid+1, r, arc);
	}
	pushup(rt);
} 
const double pi = acos(-1.0);
double ang[maxn];
void solve() {
	build(1, 1, n);
	static int cnt = 0;
	if(cnt++) printf("\n");
	for(int i = 1; i <= n; i++) ang[i] = pi;
	for(int i = 1; i <= m; i++) {
		int p, c;scanf("%d%d", &p, &c);
		double arc = c/360.0*2*pi;
		update(1, p+1, n, ang[p]-arc);
		ang[p] = arc;
		printf("%.2f %.2f\n", tr[1].x, tr[1].y);
	}
    return;
}

int main() {
    while(scanf("%d%d", &n, &m) == 2) {
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值