一、内容
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个段,每个段初始状态都是垂直的且首尾相连,每次选择某个节点(即这个节点后面的都要进行旋转),最后求最后一个节点的坐标
- 输入的度数是指将第s段与第s+1段之间的角度差修改为a.
- 所以用pre保存上一次的角度差,计算出需要旋转的角度。然后区间更新需要旋转的角度即可。
- 线段树里面保存的是每个区间的向量。
三、代码
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int N = 1e4 + 5;
const double PI = acos(-1.0);
struct Node {
double x, y, add;//add懒标记
} tr[N << 2];
int n, m, len[N], s, a;//pre代表与i关节上一次的度数 题目求的是旋转使i与i+1的度数之差为a 并不是旋转a度
double pre[N], si, co;
void pushup(int id) { //合并2个区间的向量
tr[id].x = tr[id << 1].x + tr[id << 1 | 1].x;
tr[id].y = tr[id << 1].y + tr[id << 1 | 1].y;
}
void pushdown(int id) {
if (tr[id].add == 0) return;
//计算出旋转后的向量坐标
Node &l = tr[id << 1], &r = tr[id << 1 | 1];
double x, y;
double a = tr[id].add, co = cos(a), si = sin(a);
x = co * l.x - si * l.y;
y = si * l.x + co * l.y;
l.x = x, l.y = y;
x = co * r.x - si * r.y;
y = si * r.x + co * r.y;
r.x = x, r.y = y;
l.add += tr[id].add; r.add += tr[id].add;
tr[id].add = 0;
}
void build(int id, int l, int r) {
tr[id].add = tr[id].x = 0;
if (l == r) {
tr[id].y = len[l];
return;
}
int mid = (l + r) >> 1;
build(id << 1, l, mid);
build(id << 1 | 1, mid + 1, r);
pushup(id);
}
void update(int id, int l, int r, int x, int y, double a) {
double xx, yy;
if (x <= l && r <= y) {
//旋转a度后这个向量的x,y坐标的变化
xx = cos(a) * tr[id].x - sin(a) * tr[id].y;
yy = sin(a) * tr[id].x + cos(a) * tr[id].y;
tr[id].x = xx, tr[id].y = yy;
tr[id].add += a;
return;
}
pushdown(id);
int mid = (l + r) >> 1;
if (x <= mid) update(id << 1, l, mid, x, y, a);
if (y > mid) update(id << 1 | 1, mid + 1, r, x, y, a);
pushup(id);
}
int main() {
while (~scanf("%d%d", &n, &m)) {
for (int i = 1; i <= n; i++) scanf("%d", &len[i]), pre[i] = PI; //初始i与i+1这2个向量角度之差默认为180度
build(1, 1, n);
for (int i = 1; i <= m; i++) {
scanf("%d%d", &s, &a);
double rad = a / 360.0 * 2 * PI;
update(1, 1, n, s + 1, n, rad - pre[s]);
pre[s] = rad; //更新s与s+1之间的角度差
printf("%.2f %.2f\n", tr[1].x, tr[1].y);
}
printf("\n");
}
return 0;
}
嘿呀!
发布了351 篇原创文章 · 获赞 277 · 访问量 5万+
私信
关注