Poj 2991 crane
Question Link
Given a vertical excavator arm with N segments, now each operation can rotate at a position to make a special trip to [S, S + 1, output the coordinates at the nth position after each rotation
Idea: The line segment tree treats each segment as a vector, so that the coordinates of each segment are equal to the coordinates of the first several segments, and then each rotation, this is equivalent to adding an angle to all the current to the last position. In this way, you need to modify the interval. Then, you need to query the current angle of S and S + 1 each time. Therefore, you need to perform a single point query, in this way, use the line segment tree for maintenance.
Code:
#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int N = 10005;const double PI = acos(-1.0);#define lson(x) ((x<<1)+1)#define rson(x) ((x<<1)+2)int n, m;double COS[721], SIN[721];struct Node {int l, r, lazy, R;double x, y;void gao(int ang) {R = (R + ang) % 360;lazy = (lazy + ang) % 360;double tmpx = x, tmpy = y;x = COS[ang + 360] * tmpx - SIN[ang + 360] * tmpy;y = SIN[ang + 360] * tmpx + COS[ang + 360] * tmpy;}} node[N * 4];void pushup(int x) {node[x].x = node[lson(x)].x + node[rson(x)].x;node[x].y = node[lson(x)].y + node[rson(x)].y;}void pushdown(int x) {if (node[x].lazy) {node[lson(x)].gao(node[x].lazy);node[rson(x)].gao(node[x].lazy);node[x].lazy = 0;}}void build(int l, int r, int x = 0) {node[x].l = l; node[x].r = r; node[x].lazy = node[x].R = 0;if (l == r) {double tmp;scanf("%lf", &tmp);node[x].x = 0.0;node[x].y = tmp;return;}int mid = (l + r) / 2;build(l, mid, lson(x));build(mid + 1, r, rson(x));pushup(x);}void add(int l, int r, int v, int x = 0) {if (node[x].l >= l && node[x].r <= r) {node[x].gao(v);return;}int mid = (node[x].l + node[x].r) / 2;pushdown(x);if (l <= mid) add(l, r, v, lson(x));if (r > mid) add(l, r, v, rson(x));pushup(x);}int query(int v, int x = 0) {if (node[x].l == node[x].r)return node[x].R;int mid = (node[x].l + node[x].r) / 2;int ans = 0;pushdown(x);if (v <= mid) ans += query(v, lson(x));if (v > mid) ans += query(v, rson(x));pushup(x);return ans;}int main() {int bo = 0;for (int i = -360; i <= 360; i++) {COS[i + 360] = cos(i / 180.0 * PI);SIN[i + 360] = sin(i / 180.0 * PI);}while (~scanf("%d%d", &n, &m)) {if (bo) printf("\n");else bo = 1;build(1, n);int s, a;while (m--) {scanf("%d%d", &s, &a);int a1 = query(s);int a2 = query(s + 1);int ang = ((a1 - a2 + a + 180) % 360 + 360) % 360;add(s + 1, n, ang);printf("%.2lf %.2lf\n", node[0].x, node[0].y);}}return 0;}
Poj 2991 Crane (line segment tree + computational ry)