POJ 2991

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

Source

CTU Open 2005

神题!完全颠覆了我对线段树小清新的认识 , 线段树跟计算几何的结合,哇咔咔

题意:给你n段相连的线段,从原点向y轴正半轴延伸,有m个操作,“a b"——把第a个结点旋转b度,输出每次旋转后第n个结点的坐标。

科普一下计算几何 的知识:

1.向量(x,y)沿逆时针旋转a度后得到的向量为(x * cos a  - y * sin ay , x * sin a + y * cos a);

2.旋转的角度对该线段的后代都有效。

构建一棵线段树来维护向量旋转的时间和向量尾的坐标。先旋转再加延时标记。

小心有个trick是输出-0.00

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , m  , rt << 1
#define rson m + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 11000;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e-8
const int MOD = (int)1e9 + 7;
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pi;
///#pragma comment(linker, "/STACK:102400000,102400000")
int sd[MAXN << 2] , angle[MAXN];
D sx[MAXN << 2] , sy[MAXN << 2];
void rotate(int rt , int sd)
{
    D d = sd * asin(1.0) / 90.0;
    D x = sx[rt] * cos(d) - sy[rt] * sin(d);
    D y = sx[rt] * sin(d) + sy[rt] * cos(d);
    sx[rt] = x;
    sy[rt] = y;
}
void PushUp(int rt)
{
    sx[rt] = sx[rt << 1] + sx[rt << 1 | 1];
    sy[rt] = sy[rt << 1] + sy[rt << 1 | 1];
}
void PushDown(int rt)
{
    rotate(rt << 1 , sd[rt]);
    rotate(rt << 1 | 1 , sd[rt]);
    sd[rt << 1] += sd[rt];
    sd[rt << 1 | 1] += sd[rt];
    sd[rt] = 0;
}
void update(int p , int d ,int l , int r , int rt)
{
    if(p < l)
    {
        rotate(rt , d);
        sd[rt] += d;
        return;
    }
    if(sd[rt])PushDown(rt);
    int m = (l + r) >> 1;
    if(p < m)update(p , d , lson);
    update(p ,d , rson);
    PushUp(rt);
}
void build(int l , int r , int rt)
{
    sd[rt] =0;
    if(l == r)
    {
        scanf("%lf" , &sy[rt]);
        sx[rt] = 0;
        return;
    }
    int m = (l + r) >> 1;
    build(lson);
    build(rson);
    PushUp(rt);
}
int main()
{
//    freopen("out.text" , "w" , stdout);
    int n , m , flag = 0;
    while(~scanf("%d%d" ,&n , &m))
    {
        if(flag)puts("");
        else flag = 1;
        build(1 , n , 1);
        FORR(i ,0 , n)angle[i] = 180;
        int aa , bb;
        while(m--)
        {
            scanf("%d%d" , &aa , &bb);
            update(aa , bb - angle[aa] , 1 , n ,1);
            angle[aa]  = bb;
            printf("%.2lf %.2lf\n" , fabs(sx[1]) < eps ? 0 : sx[1] , fabs(sy[1]) < eps ? 0 : sy[1]);
        }
    }
    return 0;
}


你可能感兴趣的:(线段树,ACM,计算几何)