Crane
Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 10058 | Accepted: 2641 | 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., 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).
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
思路:
刚开始读题的时候,怎么也想不明白到底是怎么旋转的......
题中的意思是,旋转后,si与si+1的角度是a,并不是旋转a度,
因为可能会多次旋转某条边,所以我们设ang[i]表示第i条边与第i-1条边的夹角,记录每次旋转后的状态,下次从该状态可得出旋 转的角度。
下面来模拟一下样例:
每次旋转的角度=a-ang[s+1]
每次旋转完后,可见,[s+1,n]的坐标都需要更新,所以可用线段树
代码如下:
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cstring>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ll long long
using namespace std;
const int N=10005;
const int INF=0x3f3f3f3f;
int add[N<<2],ang[N];
double px[N<<2],py[N<<2];
double pi=3.1415926535;
void push_up(int rt){
px[rt]=px[rt<<1]+px[rt<<1|1];
py[rt]=py[rt<<1]+py[rt<<1|1];
}
void build(int l,int r,int rt){
if(l==r){
px[rt]=0.0;
scanf("%lf",&py[rt]);
return ;
}
int m=(l+r)>>1;
build(lson);
build(rson);
push_up(rt);
}
void revo(int rt,int t){
double a=t*pi/180;
double x=px[rt],y=py[rt];
px[rt]=x*cos(a)-y*sin(a);
py[rt]=y*cos(a)+x*sin(a);
}
void push_down(int rt){
if(add[rt]){
int t=add[rt];
add[rt<<1]+=t;
add[rt<<1|1]+=t;
revo(rt<<1,t);
revo(rt<<1|1,t);
add[rt]=0;
}
}
void update(int L,int R,int l,int r,int rt,int t){
if(l==L&&R==r){
revo(rt,t);
add[rt]+=t;
return ;
}
push_down(rt);
int m=(l+r)>>1;
if(m<L){
update(L,R,rson,t);
}
else{
update(L,m,lson,t);
update(m+1,R,rson,t);
}
push_up(rt);
}
int main(){
int n,c;
while(scanf("%d%d",&n,&c)!=EOF){
memset(add,0,sizeof(add));
for(int i=1;i<=n;i++){
ang[i]=180;
}
build(1,n,1);
int s,a;
while(c--){
scanf("%d%d",&s,&a);
s++;
int t=a-ang[s];
ang[s]=a;
update(s,n,1,n,1,t);
printf("%.2lf %.2lf\n",px[1],py[1]);
}
}
}