poj2991 Crane 向量旋转+线段树

Crane
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3962   Accepted: 1072   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截,最开始都在y轴上,M个操作,把s+1以上部分旋转,使s和s+1角度为a,每次操作后输出最高一截顶部的坐标。

   线段树x,y维护当前区间从头连到尾的向量,degree为相对于坐标轴的旋转角度,setv为要pushdown的角度。每次操作先询问当前第s+1截和s截的角度,做差,再用a减去这个角度就是要旋转的角度。然后在[s+1,N]区间update这个角度。

  逆时针旋转公式:x'=cosa*x-sina*y,y'=sina*x+cosa*y。

  每次操作后输出tree.x[1],tree.y[1]。

#include<iostream>
#include<queue>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<algorithm>
#define pi (4*atan(1.0))
typedef long long LL;
using namespace std;

const int MAXN=10010;
const int MAXNODE=4*MAXN;

int N,M,Y[MAXN];

struct SegmentTree{
    double x[MAXNODE],y[MAXNODE];
    int setv[MAXNODE],degree[MAXNODE];

    void maintain(int o){
        x[o]=x[o<<1]+x[o<<1|1];
        y[o]=y[o<<1]+y[o<<1|1];
    }

    void build(int o,int L,int R){
        setv[o]=0;
        if(L>=R){
            y[o]=Y[L];
            x[o]=0;
            degree[L]=0;
            return;
        }
        int mid=L+(R-L)/2;
        build(o<<1,L,mid);
        build(o<<1|1,mid+1,R);
        maintain(o);
    }

    void rotate(double& x,double& y,int deg){
        double tmpx=x,tmpy=y,a=deg/180.0*pi;
        x=tmpx*cos(a)-tmpy*sin(a);
        y=tmpx*sin(a)+tmpy*cos(a);
    }

    void pushdown(int o,int L,int R){
        if(setv[o]>0){
            setv[o<<1]=(setv[o<<1]+setv[o])%360;
            setv[o<<1|1]=(setv[o<<1|1]+setv[o])%360;
            int mid=L+(R-L)/2;
            if(L>=mid) degree[L]=(degree[L]+setv[o])%360;
            if(mid+1>=R) degree[R]=(degree[R]+setv[o])%360;
            rotate(x[o<<1],y[o<<1],setv[o]);
            rotate(x[o<<1|1],y[o<<1|1],setv[o]);
            setv[o]=0;
        }
    }

    void update(int o,int L,int R,int ql,int qr,int deg){
        if(ql<=L&&qr>=R){
            setv[o]=(setv[o]+deg)%360;
            if(L>=R) degree[L]=(degree[L]+deg)%360;
            rotate(x[o],y[o],deg);
            return;
        }
        pushdown(o,L,R);
        int mid=L+(R-L)/2;
        if(ql<=mid) update(o<<1,L,mid,ql,qr,deg);
        if(qr>mid) update(o<<1|1,mid+1,R,ql,qr,deg);
        maintain(o);
    }

    int query(int o,int L,int R,int pos){
        if(L>=R) return degree[L];
        pushdown(o,L,R);
        int mid=L+(R-L)/2;
        if(pos<=mid) return query(o<<1,L,mid,pos);
        else return query(o<<1|1,mid+1,R,pos);
    }
}tree;

int main(){
    freopen("in.txt","r",stdin);
    int cas=0;
    while(scanf("%d%d",&N,&M)!=EOF){
        if(cas++) puts("");
        for(int i=1;i<=N;i++) scanf("%d",&Y[i]);
        tree.build(1,1,N);
        int s,deg;
        while(M--){
            scanf("%d%d",&s,°);
            int tmp=(180+tree.query(1,1,N,s+1)-tree.query(1,1,N,s)+360)%360;
            tree.update(1,1,N,s+1,N,(deg-tmp+360)%360);
            printf("%.2lf %.2lf\n",tree.x[1],tree.y[1]);
        }
    }
    return 0;
}


你可能感兴趣的:(poj2991 Crane 向量旋转+线段树)