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

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

题意:给你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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值