POJ 2991 - Crane

Advanced Data Structures :: Segment Tree





(有点像《cut the rope》中的机械臂?没错,就是那么个玩意)





Advanced Data Structures :: Segment Tree







要注意,从第 s 节手臂开始到机械手要旋转的角度,和第 s - 1 节手臂的角度有关。

#include <cstdio>
#include <cmath>

#define LSon(x) ((x) << 1)
#define RSon(x) ((x) << 1 | 1)

const int MAXN = 10002;
const int ROOT = 1;
const double PI = acos(-1.0);
const double EPS = 1e-8;

struct Seg {
    double x, y;
    int flag;
    int degree;

void Rotate(Seg& node, int degree);

struct SegTree {
    Seg node[MAXN << 2];
    void Update(int pos) {
        node[pos].x = node[LSon(pos)].x + node[RSon(pos)].x;
        node[pos].y = node[LSon(pos)].y + node[RSon(pos)].y;
    void Build(int l, int r, int pos) {
        node[pos].x = node[pos].y = 0;
        node[pos].flag = 0;
        node[pos].degree = 0;
        if (l == r) {
            scanf("%lf", &node[pos].y);
        int m = l + r >> 1;
        Build(l, m, LSon(pos));
        Build(m + 1, r, RSon(pos));
    void Push(int pos) {
        Seg& father = node[pos];
        Seg& lson = node[LSon(pos)];
        Seg& rson = node[RSon(pos)];
        if (father.flag) {
            Rotate(lson, father.flag);
            Rotate(rson, father.flag);
            lson.flag += father.flag;
            rson.flag += father.flag;
            father.flag = 0;
    void Modify(int l, int r, int pos, int x, int y, int z) {
        if (x <= l && r <= y) {
            Rotate(node[pos], z);
            node[pos].flag += z;
        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);
    int Query(int l, int r, int pos, int x) {
        if (l == r) return node[pos].degree;
        int m = l + r >> 1;
        if (x <= m) return Query(l, m, LSon(pos), x);
        else return Query(m + 1, r, RSon(pos), x);

int n, c;
int s, a;

SegTree tree;

double GetRad(int x);

int main() {
    bool first = true;
    while (scanf("%d%d", &n, &c) != EOF) {
        tree.Build(0, n - 1, ROOT);   
        printf("%s", first ? first = false, "" : "\n");
        for (int i = 0; i < c; i++) {
            scanf("%d%d", &s, &a);   
            int degree = tree.Query(0, n - 1, ROOT, s - 1) + 180 + a -
                tree.Query(0, n - 1, ROOT, s);
            tree.Modify(0, n - 1, ROOT, s, n - 1, degree);
            printf("%.2lf %.2lf\n", tree.node[ROOT].x + EPS, tree.node[ROOT].y + EPS);

    return 0;

double GetRad(int x) {
    return x * PI / 180;

void Rotate(Seg& node, int degree) {
    double rad = GetRad(degree);
    double x = node.x; double y = node.y;
    node.x = x * cos(rad) + y * -sin(rad);
    node.y = x * sin(rad) + y * cos(rad);
    node.degree = (node.degree + degree) % 360;



DescriptionnACM 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. nnYour 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., 180o. The operator issues commands that change the angle in exactly one joint. nnInputnThe input consists of several instances, separated by single empty lines. nnThe 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).nnOutputnThe 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. nnThe outputs for each two consecutive instances must be separated by a single empty line.nnSample Inputnn2 1n10 5n1 90nn3 2n5 5 5n1 270n2 90nnSample Outputnn5.00 10.00nn-10.00 5.00n-5.00 10.00 问答