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节的机械手,每次让某个节点移动使得第i条边和第i+1条边的夹角是a,初始化夹角都是180度,求经过操作后最后一个节点位置

思路:相同是线段树的区间更新,由于要求的是最后一个点的坐标,我们知道向量是能够相加的,那么我们把每一个线段当成向量相加的话,就能够得到最后的坐标了,再者就是旋转的问题了,我们相同知道旋转后,第i+1后的边都会受到影响,这就是线段树的区间更新了;然后有了逆时针向量旋转的公式我们就能够得到解了。注意的地方是:我们每次储存上一次的角度,依次来推出这次须要旋转的角度,还有就是不知道会平白无故W了还多次,还是换个姿势做的

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define lson(x) ((x) << 1)
#define rson(x) ((x) << 1 | 1)
using namespace std;
const int maxn = 10002; int angle[maxn], setv[maxn<<2];
double sumx[maxn<<2], sumy[maxn<<2]; void update(int pos) {
sumx[pos] = sumx[lson(pos)] + sumx[rson(pos)];
sumy[pos] = sumy[lson(pos)] + sumy[rson(pos)];
} void build(int l, int r, int pos) {
setv[pos] = 0;
if (l == r) {
scanf("%lf", &sumy[pos]);
sumx[pos] = 0;
return;
}
int m = l + r >> 1;
build(l, m, lson(pos));
build(m+1, r, rson(pos));
update(pos);
} void change(int pos, int d) {
double tmp = d * acos(-1.0) / 180;
double x = sumx[pos]*cos(tmp) - sumy[pos]*sin(tmp);
double y = sumx[pos]*sin(tmp) + sumy[pos]*cos(tmp);
sumx[pos] = x;
sumy[pos] = y;
} void push(int pos) {
if (setv[pos]) {
setv[lson(pos)] += setv[pos];
setv[rson(pos)] += setv[pos];
change(lson(pos), setv[pos]);
change(rson(pos), setv[pos]);
setv[pos] = 0;
}
} void modify(int l, int r, int pos, int x, int y, int z) {
if (x <= l && y >= r) {
setv[pos] += z;
change(pos, z);
return;
}
push(pos);
int m = l + r >> 1;
if (x <= m)
modify(l, m, lson(pos), x, y, z);
if (y > m)
modify(m+1, r, rson(pos), x, y, z);
update(pos);
} int main() {
int n, q;
int first = 1;
while (scanf("%d%d", &n, &q) != EOF) {
if (first)
first = 0;
else printf("\n");
build(1, n, 1);
for (int i = 1; i <= n; i++)
angle[i] = 180;
int a, b;
while (q--) {
scanf("%d%d", &a, &b);
modify(1, n, 1, a+1, n, b-angle[a]);
angle[a] = b;
printf("%.2lf %.2lf\n", sumx[1], sumy[1]);
}
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

POJ - 2991 Crane (段树+计算几何)的更多相关文章

  1. POJ 2991 Crane(线段树+计算几何)

    POJ 2991 Crane 题目链接 题意:给定一个垂直的挖掘机臂.有n段,如今每次操作能够旋转一个位置,把[s, s + 1]专程a度,每次旋转后要输出第n个位置的坐标 思路:线段树.把每一段当成 ...

  2. POJ 2991 Crane

    线段树+计算几何,区间更新,区间求和,向量旋转. /* *********************************************** Author :Zhou Zhentao Ema ...

  3. POJ 2991 Crane (线段树)

    题目链接 Description ACM has bought a new crane (crane -- jeřáb) . The crane consists of n segments of v ...

  4. POJ 2991–Crane【线段树+几何】

    题意: 把手臂都各自看成一个向量,则机械手的位置正好是手臂向量之和.旋转某个关节,其实就是把关节到机械手之间的手臂向量统统旋转. 由于手臂很多,要每个向量做相同的旋转操作很费时间.这时就可以想到用线段 ...

  5. (中等) POJ 2991 Crane , 几何+线段树。

    Description ACM has bought a new crane (crane -- jeřáb) . The crane consists of n segments of variou ...

  6. POJ 2991 Crane(线段树)

    Crane Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7687   Accepted: 2075   Special J ...

  7. [poj 2991]Crane[线段树表示向量之和,而非数量]

    题意: 起重机的机械臂, 由n段组成, 对某一些连接点进行旋转, 询问每次操作后的末端坐标. 思路: 由于旋转会影响到该点之后所有线段的角度, 因此容易想到用线段树记录角度, 成段更新. (但是不是每 ...

  8. POJ 2528 QAQ段树+分离

    Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u Submitcid=58236#statu ...

  9. AC日记——Crane poj 2991

    POJ - 2991 思路: 向量旋转: 代码: #include <cmath> #include <cstdio> #include <cstring> #in ...

随机推荐

  1. php压缩

    php压缩的一个demo,随便测试了一下,可以用 <?php class PHPZip { private $ctrl_dir = array(); private $datasec = arr ...

  2. 在vue中使用nprogress

    NProgress的官网:http://ricostacruz.com/nprogress/        源码地址:https://github.com/rstacruz/nprogress 1.安 ...

  3. 7 Best jQuery & JavaScript PDF Viewer plugin with examples

    In this Post we are providing best jQuery PDF viewer plugin & tutorial with examples.Due to popu ...

  4. ios开发swift学习第三天:逻辑分支

    一. 分支的介绍 分支即if/switch/三目运算符等判断语句 通过分支语句可以控制程序的执行流程 二. if分支语句 和OC中if语句有一定的区别 判断句可以不加() 在Swift的判断句中必须有 ...

  5. ios开发网络学习AFN三:AFN的序列化

    #import "ViewController.h" #import "AFNetworking.h" @interface ViewController () ...

  6. 撸代码--类QQ聊天实现(基于linux 管道 信号 共享内存)

    一:任务描写叙述 A,B两个进程通过管道通信,像曾经的互相聊天一样,然后A进程每次接收到的数据通过A1进程显示(一个新进程,用于显示A接收到的信息),A和A1间的数据传递採用共享内存,相应的有一个B1 ...

  7. dot-- 资源汇总

    http://graphviz.org/doc/info/attrs.htmlhttp://graphs.grevian.org/http://graphs.grevian.org/exampleht ...

  8. mui列表跳转到详情页优化方案

    原理 因为列表页到详情页是多对一的形式,即列表页的多条数据列表对应的是一个详情页,只是数据不同而:因此,可以在加载列表页时预加载详情页,即创建一个详情页的webview,但是不显示出来,点击列表的时候 ...

  9. ios UI控件的简单整理

    把该文件拷贝到.m文件中就能够方便的查找 /** 匿名类目:能够声明方法和变量,属性为private(不同意在外部调用,且不能被继承 */ /** 发送数据的托付方,接收数据的时代理发(即代理的反向传 ...

  10. C++ 快速入门笔记:基本语法

    数据类型 枚举类型 enum color { red, green, blue } colors; colors = blue; 默认情况下,第一个名称的值是 0,后面的依次加 1.也可以自定初始值: ...