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., 180o. The operator issues commands that change the angle in exactly one joint.
题意就是一个挖掘机,饿。。。吊车,的机械臂,有n节,可以转动,问每次转动之后最后一个位置。
刚开始的时候想到是用数组表示每一个点,然后用线段树更新某一个区间,但是超时了,因为每次更新都要让上一次更新全部更新彻底,其实就是直接全部更新,反而还不如直接模拟。后来看了看题解,提到向量。。。就是用数组记录区间内所有向量的和,那么所有区间的和就是答案,而且,每次旋转d度的话对这个区间也是旋转d度,没有区别。。。
另外被这个题坑了两个小时,还是今年的头两个小时。。。建树的时候没有更新彻底,看来以后自己测试一定要多组数据。。。。
代码如下:
#include<iostream>
#include<cstdio>
#include<cmath> #define lson L,M,po*2
#define rson M+1,R,po*2+1 using namespace std; struct state
{
double x,y;
}; const double PI=atan2(1.0,1.0)*; state BIT[*];
int COL[*];
int rang[]; void change(int po,int ct)
{
double tang;
double len; tang=atan2(BIT[po].y,BIT[po].x);
len=sqrt(BIT[po].x*BIT[po].x+BIT[po].y*BIT[po].y);
tang+=ct*PI/180.0;
BIT[po].x=len*cos(tang);
BIT[po].y=len*sin(tang);
} void pushUP(int po)
{
BIT[po].x=BIT[po*].x+BIT[po*+].x;
BIT[po].y=BIT[po*].y+BIT[po*+].y;
} void pushDown(int po)
{
if(COL[po])
{
change(po*,COL[po]);
change(po*+,COL[po]); COL[po*]+=COL[po];
COL[po*+]+=COL[po]; COL[po]=;
}
} void build_tree(int L,int R,int po)
{
COL[po]=; //DON'T FORGET !!! if(L==R)
{
int temp; BIT[po].x=;
COL[po]=;
scanf("%d",&temp);
BIT[po].y=temp; return;
} int M=(L+R)/; build_tree(lson);
build_tree(rson); pushUP(po);
} void update(int ul,int ur,int ut,int L,int R,int po)
{
if(ul<=L&&ur>=R)
{
change(po,ut);
COL[po]+=ut;
COL[po]%=;
return;
} pushDown(po); int M=(L+R)/; if(ul<=M)
update(ul,ur,ut,lson);
if(ur>M)
update(ul,ur,ut,rson); pushUP(po);
} int main()
{
int n,c;
int a,b;
int cas=; while(~scanf("%d %d",&n,&c))
{
if(cas++)
printf("\n"); build_tree(,n,); for(int i=;i<=n;++i)
rang[i]=; for(int i=;i<=c;++i)
{
scanf("%d %d",&a,&b);
b-=;
if(b==-)
b=;
update(a+,n,b-rang[a],,n,);
rang[a]=b; printf("%.2lf %.2lf\n",BIT[].x,BIT[].y);
}
} return ;
}