Crane
Time Limit: 2000MS | Memory Limit: 65536K | |||
Total Submissions: 4814 | Accepted: 1294 | 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.
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).
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.
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
题意:有一个吊车由很多个不同长度的线段组成,一开始是一条长直线起点在(0,0),尾节点在(0,sum[n]),每条线段之间的夹角的初始值是180度。然后有一些操作a、 b将第a条线段和a+1之间的夹角变成b度,经过每一次操作都要求出尾节点的坐标。
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
struct node
{
int l, r, add;
double x, y;
}tree[100001 * 4];
int angle[100001];
void pushup(int id)
{
tree[id].x = tree[2 * id].x + tree[2 * id + 1].x;
tree[id].y = tree[id * 2].y + tree[2 * id + 1].y;
}
void build(int id, int l, int r)
{
tree[id].l = l;tree[id].r = r;tree[id].add = 0;
if (tree[id].l == tree[id].r)
{
scanf("%lf", &tree[id].y);
tree[id].x = 0;
return;
}
int mid = (tree[id].l + tree[id].r) / 2;
build(2 * id, l, mid);
build(2 * id + 1, mid + 1, r);
pushup(id);
}
void change(int id, int a)
{
double t = a*acos(-1) / 180;
double x = tree[id].x*cos(t) - tree[id].y*sin(t);
double y = tree[id].x*sin(t) + tree[id].y*cos(t);
tree[id].x = x;tree[id].y = y;
}
void pushdown(int id)
{
if (tree[id].add)
{
tree[2 * id].add += tree[id].add;
tree[2 * id + 1].add += tree[id].add;
change(2 * id, tree[id].add);
change(2 * id + 1, tree[id].add);
tree[id].add = 0;
}
}
void update(int id, int x, int y, int z)
{
if (x <= tree[id].l&&y >= tree[id].r)
{
tree[id].add += z;
change(id, z);
return;
}
pushdown(id);
int mid = (tree[id].l + tree[id].r) / 2;
if (x <= mid) update(2 * id, x, y, z);
if (y>mid) update(2 * id + 1, x, y, z);
pushup(id);
}
int main()
{
int m, n, i, x, y;
while (scanf("%d%d", &n, &m) != EOF)
{
build(1, 1, n);
for (i = 1;i <= n;i++)
angle[i] = 180;
while (m--)
{
scanf("%d%d", &x, &y);
update(1, x + 1, n, y - angle[x]);
angle[x] = y;
printf("%.2f %.2f\n", tree[1].x, tree[1].y);
}
}
return 0;
}