POJ 2991

Crane
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 8238   Accepted: 2217   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

题意:给你n段线段,初始时是竖直的,有C次修改,每次修改使得线段Si和Si+1之间的角度变成A[i]度,每次修改后输出最后那条线段的坐标,初始时为(0,0)
题解:用线段树进行维护    1.第一条线段的起点指向最后一条线段的终点的向量
               2.每个节点的对应部分链接后,右儿子需要转动的角度
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1e5+5;
#define M_PI acos(-1.0)
int N,C;
int L[maxn];
int S[maxn],A[maxn];
double vx[maxn],vy[maxn];
double ang[maxn];
double pre[maxn];
void init(int k,int l,int r){
    ang[k]=vx[k]=0.0;
    if(r-l==1) vy[k]=L[l];
    else{
        int chl=k*2+1;
        int chr=k*2+2;
        init(chl,l,(l+r)/2);
        init(chr,(l+r)/2,r);
        vy[k]=vy[chl]+vy[chr];
    }
}
void update(int s,double a,int v,int l,int r){
    if(s<=l) return;
    else if(s<r){
        int chl=v*2+1;
        int chr=v*2+2;
        int m=(l+r)/2;
        update(s,a,chl,l,m);
        update(s,a,chr,m,r);
        if(s<=m){
            ang[v]+=a;
        }
        double s=sin(ang[v]),c=cos(ang[v]);
        vx[v]=vx[chl]+(c*vx[chr]-s*vy[chr]);
        vy[v]=vy[chl]+(s*vx[chr]+c*vy[chr]);
    }
}
void solve(){
    init(0,0,N);
    for(int i=1;i<N;i++){
        pre[i]=M_PI;
    }
    for(int i=0;i<C;i++){
        int s=S[i];
        double a=A[i]/360.0*2*M_PI;
        update(s,a-pre[s],0,0,N);
        pre[s]=a;
        printf("%.2f %.2f\n",vx[0],vy[0]);
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    while(cin>>N>>C){
    memset(L,0,sizeof(L));
    memset(S,0,sizeof(S));
    memset(A,0,sizeof(A));
    for(int i=0;i<N;i++){
        cin>>L[i];
    }
    for(int i=0;i<C;i++){
        cin>>S[i]>>A[i];
    }
        solve();
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/buerdepepeqi/p/9363633.html
POJ