【线段树+向量】POJ 2991 Crane
【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.
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.
【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).
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.
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
【Analysis】
将有终点的线段记为向量,操作全部由向量运算来完成。
将向量(x0,y0)逆时针旋转角度B得到向量(x,y):
x=x0*cosB-y0*sinB;
y=x0*sinB+y0*cosB;
而题给的角度是旋转后的角度α,于是新建ang[i]记第i条线段与第i+1条线段之间的夹角,每次的(逆时针)旋转角change=α-ang[i],ang[i]=α;
另外值得一提的是update操作中,递归操作有小小的改变。
【Code】
1 #include<cmath> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 7 const int sm = 10000+5; 8 9 int n,c,s,a; 10 double t[sm<<2],xx[sm<<2],yy[sm<<2],ang[sm]; 11 double PI = acos(-1.0); 12 13 void pushup(int now) { 14 xx[now] = xx[now<<1] + xx[now<<1|1]; 15 yy[now] = yy[now<<1] + yy[now<<1|1]; 16 } 17 18 void build(int now,int l,int r) { 19 if(l==r) { 20 scanf("%lf",&yy[now]); 21 xx[now]=0.0;t[now]=0; 22 return; 23 } 24 int mid=(l+r)>>1; 25 build(now<<1,l,mid); 26 build(now<<1|1,mid+1,r); 27 pushup(now); 28 t[now]=0; 29 } 30 31 void pushdown(int now) { 32 double t1=xx[now<<1],t2=yy[now<<1],c=t[now]; 33 xx[now<<1]=t1*cos(c*PI/180)-t2*sin(c*PI/180); 34 yy[now<<1]=t1*sin(c*PI/180)+t2*cos(c*PI/180); 35 36 t1=xx[now<<1|1],t2=yy[now<<1|1]; 37 xx[now<<1|1]=t1*cos(c*PI/180)-t2*sin(c*PI/180); 38 yy[now<<1|1]=t1*sin(c*PI/180)+t2*cos(c*PI/180); 39 40 t[now<<1]+=c; 41 t[now<<1|1]+=c; 42 t[now]=0.0; 43 } 44 45 void update(int a,int b,int now,int l,int r,double c) { 46 if(l==a&&r==b) { 47 double t1=xx[now],t2=yy[now]; 48 xx[now]=t1*cos(c*PI/180)-t2*sin(c*PI/180); 49 yy[now]=t1*sin(c*PI/180)+t2*cos(c*PI/180); 50 t[now]+=c; 51 return; 52 } 53 if(l==r)return; 54 if(t[now]!=0)pushdown(now); 55 int mid=(l+r)>>1; 56 if(mid<a)update(a,b,now<<1|1,mid+1,r,c); 57 else { 58 update(a,mid,now<<1,l,mid,c); 59 update(mid+1,b,now<<1|1,mid+1,r,c); 60 } 61 pushup(now); 62 } 63 64 int main() { 65 while(scanf("%d%d",&n,&c)!=EOF) { 66 build(1,1,n); 67 for(int i=1;i<=n;++i) ang[i]=180.0; 68 for(int i=1;i<=c;++i) { 69 scanf("%d%d",&s,&a); 70 double change=a-ang[s]; 71 ang[s]=a; 72 if(s+1<=n)update(s+1,n,1,1,n,change); 73 printf("%.2lf %.2lf\n",xx[1],yy[1]); 74 } 75 printf("\n"); 76 } 77 return 0; 78 }