POJ 2991 Crane 【线段树】【向量旋转】

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
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid +1, r
const int MAX = 1e5 + 7;
int lz[MAX << 2], angle[MAX];
#define db double
#define PI acos(-1.0)
db a[MAX << 2], b[MAX << 2];
void pushup(int rt){
    a[rt] = a[rt << 1] + a[rt << 1 | 1];
    b[rt] = b[rt << 1] + b[rt << 1 | 1];
}
void change(int rt, int v){
    db x = PI * v / 180;
    db X = a[rt] * cos(x) - b[rt] * sin(x);
    db Y = a[rt] * sin(x) + b[rt] * cos(x);
    a[rt] = X, b[rt] = Y;
}
void pushdown(int rt){
    if(lz[rt]){
        lz[rt << 1] += lz[rt];
        lz[rt << 1 | 1] += lz[rt];
        change(rt << 1, lz[rt]);
        change(rt << 1 | 1, lz[rt]);
        lz[rt] = 0;
    }
}
void update(int rt, int l, int r, int x, int y, int v){
    if(x <= l && r <= y){
        lz[rt] += v;
        change(rt, v);
        return;
    }
    int mid = (l + r) >> 1;
    pushdown(rt);
    if(x <= mid) update(lson, x, y, v);
    if(mid < y) update(rson, x, y, v);
    pushup(rt);
}
void build(int rt, int l, int r){
    if(l == r){
        scanf("%lf", &b[rt]);
        return;
    }
    int mid = (l + r) >> 1;
    build(lson), build(rson);
    pushup(rt);
}
int main(){
    int n, m;
    while(scanf("%d%d", &n, &m) != EOF){
        fill(angle + 1, angle + 1 + n, 180);
        memset(lz, 0, sizeof lz);
        memset(a, 0, sizeof a);
        build(1, 1, n);
        for(int i = 0, x, y; i < m; i++){
            scanf("%d%d", &x, &y);
            update(1, 1, n, x + 1, n, y - angle[x + 1]);
            angle[x + 1] = y;
            printf("%.2f %.2f\n", a[1], b[1]);
        }
        puts("");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Head_Hard/article/details/82119519