poj 2991【线段树-lazy+向量】

Crane
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 1300 Accepted: 329 Special Judge

Description

ACM has bought a new crane (crane -- jeřáb) . The crane consists of n segments of various lengths, connected by flexible joints. The end of the i-th segment is joined to the beginning of the i + 1-th one, for 1 ≤ i < n. The beginning of the first segment is fixed at point with coordinates (0, 0) and its end at point with coordinates (0, w), where w is the length of the first segment. All of the segments lie always in one plane, and the joints allow arbitrary rotation in that plane. After series of unpleasant accidents, it was decided that software that controls the crane must contain a piece of code that constantly checks the position of the end of crane, and stops the crane if a collision should happen.

Your task is to write a part of this software that determines the position of the end of the n-th segment after each command. The state of the crane is determined by the angles between consecutive segments. Initially, all of the angles are straight, i.e., 180 o. The operator issues commands that change the angle in exactly one joint.

Input

The input consists of several instances, separated by single empty lines.

The first line of each instance consists of two integers 1 ≤ n ≤10 000 and c 0 separated by a single space -- the number of segments of the crane and the number of commands. The second line consists of n integers l1,..., ln (1 li 100) separated by single spaces. The length of the i-th segment of the crane is li. The following c lines specify the commands of the operator. Each line describing the command consists of two integers s and a (1 ≤ s < n, 0 ≤ a ≤ 359) separated by a single space -- the order to change the angle between the s-th and the s + 1-th segment to a degrees (the angle is measured counterclockwise from the s-th to the s + 1-th segment).

Output

The output for each instance consists of c lines. The i-th of the lines consists of two rational numbers x and y separated by a single space -- the coordinates of the end of the n-th segment after the i-th command, rounded to two digits after the decimal point.

The outputs for each two consecutive instances must be separated by a single empty line.

Sample Input

2 1
10 5
1 90

3 2
5 5 5
1 270
2 90

Sample Output

5.00 10.00

-10.00 5.00
-5.00 10.00

Source

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
#include<iostream>
#include<cmath>
using namespace std;

const int MAXN=10002;
const double PI=acos(double(-1));
const double eps=1e-6;

inline double Angle(int add)
{
	//cout<<"PI="<<PI<<endl;
	return double(add)/180*PI;
}

struct 
{
	double x,y;
	int turn;
}tree[MAXN<<2];

void PushDown(int rt)
{
	if(tree[rt].turn==0) return;
	tree[rt<<1].turn+=tree[rt].turn;
	tree[rt<<1].turn%=360;
	tree[rt<<1|1].turn+=tree[rt].turn;
	tree[rt<<1|1].turn%=360;

	double tx=tree[rt<<1].x,ty=tree[rt<<1].y;
	int turn=tree[rt].turn;

	tree[rt<<1].x=cos(Angle(turn))*tx-sin(Angle(turn))*ty;
	tree[rt<<1].y=sin(Angle(turn))*tx+cos(Angle(turn))*ty;

	tx=tree[rt<<1|1].x;ty=tree[rt<<1|1].y;
	tree[rt<<1|1].x=cos(Angle(turn))*tx-sin(Angle(turn))*ty;
	tree[rt<<1|1].y=sin(Angle(turn))*tx+cos(Angle(turn))*ty;

	tree[rt].turn=0;
}

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

void Build(int l,int r,int rt)
{
	if(l==r-1) 
	{
		scanf("%lf",&tree[rt].y);
		tree[rt].x=0;
		tree[rt].turn=0;
		return;
	}

	Build(l,(r+l)>>1,rt<<1);
	Build((l+r)>>1,r,rt<<1|1);

	tree[rt].turn=0;
	PushUp(rt);
}

void Update(int L,int R,int l,int r,int rt,int add)//把角度增量add加到L段到R段上
{
	if(l==L&&r==R) 
	{
		
		double tx=tree[rt].x,ty=tree[rt].y;
		//cout<<"add="<<add<<"Angle(add)="<<Angle(add)<<endl;
		//printf("x=%lf,y=%lf,rt=%d\n",tree[rt].x,tree[rt].y,rt);
		tree[rt].x=cos(Angle(add))*tx-sin(Angle(add))*ty;
		tree[rt].y=sin(Angle(add))*tx+cos(Angle(add))*ty;

		tree[rt].turn+=add;
		if(tree[rt].turn>=360) tree[rt].turn-=360;
		//printf("l=%d,r=%d,x=%lf,y=%lf,rt=%d\n",L,R,tree[rt].x,tree[rt].y,rt);
		return;
	}

	PushDown(rt);

	int mid=(l+r)>>1;
	/*
	if(R<=mid) Update(L,R,l,mid,rt<<1,add);
	else if(mid<=L) Update(L,R,mid,r,rt<<1|1,add);
	else
	{
		Update(L,mid,l,mid,rt<<1,add);
		Update(mid,R,mid,r,rt<<1|1,add);
	}
	*/
	//printf("x=%lf,y=%lf,rt=%d\n",tree[rt].x,tree[rt].y,rt);
	if(L<mid) Update(L,min(mid,R),l,mid,rt<<1,add);
	if(mid<R) Update(max(mid,L),R,mid,r,rt<<1|1,add);
	//cout<<"rt="<<rt<<endl;
	PushUp(rt);
	//printf("x=%lf,y=%lf,rt=%d\n",tree[rt].x,tree[rt].y,rt);
}

int QueryAngle(int L,int R,int l,int r,int rt)
{
	//printf("L=%d,R=%d,l=%d,r=%d,rt=%d\n",L,R,l,r,rt);
	if(l==L&&R==r) return tree[rt].turn;
	
	int mid=(l+r)>>1;
	if(R<=mid) return tree[rt].turn+QueryAngle(L,R,l,mid,rt<<1);
	else return tree[rt].turn+QueryAngle(L,R,mid,r,rt<<1|1);
}

int main()
{
	int n,i,j,k,c,pos,turn;

	while(~scanf("%d%d",&n,&c))
	{
		Build(1,n+1,1);

		while(c--)
		{
			scanf("%d%d",&pos,&turn);
		//	cout<<pos<<' '<<turn<<"sjfk"<<endl;
			//system("pause");

			int d1=QueryAngle(pos,pos+1,1,n+1,1);
			int d2=QueryAngle(pos+1,pos+2,1,n+1,1);
			Update(pos+1,n+1,1,n+1,1,turn-180-d2+d1);
			printf("%.2f %.2f\n",tree[1].x,tree[1].y);

		}
	}
	system("pause");
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值