POJ2991 线段树 区间更新 计算几何

来源:互联网 发布:小米平板刷windows包 编辑:程序博客网 时间:2024/06/04 00:43

题意:吊车由n条不同长度的线段组成,每条线段首尾相接。初始状态:每条线段垂直与x轴。每次操作改变第s条和(s+1)条的逆时针角度为a,询问每次操作后第n段末尾的坐标。
思路:
1、将每段线段都视为向量,则每次询问的结果实质上是向量和。
2、每次改变第s段和第(s+1)段的相对角度,实际上是改变了从第(s+1)段至第n段的绝对角度,则可以通过线段树进行区间更新。然而,题目给定的是第s段和第(s+1)段之间的逆时针角度,并非顺时针旋转了的角度,如何解决?通过新的degree数组解决,degree[i]代表第i段和第(i-1)段的逆时针角度,维护这个数组。
3、向量旋转公式:
http://blog.csdn.net/hjq376247328/article/details/45113563
(若知道这个公式能省不少代码量)
反思:
1、抽象能力不足。刚开始没有将线段化为向量,则必须通过 上一段末尾坐标 和 本段的绝对角度 递归计算出答案。
2、数学能力不足。刚开始不知道向量旋转公式,也没有想要自己推导,直接在向量结构体里不仅定义了坐标,还定义了长度和角度,导致代码量倍增,最后也没能成功debug。
3、思维能力不足。还没写之前也考虑到了思路2中的问题,但竟然没能想出这种简单有效的方法,化未知为已知。
4、若用G++提交,记得double对应%f,否则会TLE。

虽然耗时很长,但收获了不少新知和技巧,以及最后A出来的满足感。继续加油!

#include <iostream>#include <cmath>#include <iomanip>#include <cstdio>#include <cstring>using namespace std;const double PI = acos(-1.0);const int MAXN = 10000 + 10;int n, c;double degree[MAXN];struct Vec{    double x, y;}vec[MAXN], sum[MAXN << 2];double lazy[MAXN << 2];void PushUp(int rt){    sum[rt].x = sum[rt << 1].x + sum[rt << 1 | 1].x;    sum[rt].y = sum[rt << 1].y + sum[rt << 1 | 1].y;    return;}void PushDown(int rt){    if(lazy[rt])    {        lazy[rt << 1]  += lazy[rt];        lazy[rt << 1 | 1]  += lazy[rt];        double a = lazy[rt], xl = sum[rt << 1].x, yl = sum[rt << 1].y, xr = sum[rt << 1 | 1].x, yr = sum[rt << 1 | 1].y;        sum[rt << 1].x = xl * cos(a) + yl * sin(a);        sum[rt << 1].y = yl * cos(a) - xl * sin(a);        sum[rt << 1 | 1].x = xr * cos(a) + yr * sin(a);        sum[rt << 1 | 1].y = yr * cos(a) - xr * sin(a);        lazy[rt] = 0;    }    return;}void Build(int l, int r, int rt){    if(l == r)    {        sum[rt] = vec[l];        return;    }    int m = (l + r) >> 1;    Build(l, m, rt << 1);    Build(m + 1, r, rt << 1 | 1);    PushUp(rt);    return;}void Update(int L, int R, double c, int l, int r, int rt){    if(L <= l && r <= R)    {        double x = sum[rt].x * cos(c) + sum[rt].y * sin(c);        double y = sum[rt].y * cos(c) - sum[rt].x * sin(c);        sum[rt].x = x; sum[rt].y = y;        lazy[rt] += c;        return;    }    PushDown(rt);    int m = (l + r) >> 1;    if(L <= m) Update(L, R, c, l, m, rt << 1);    if(R > m) Update(L, R, c, m + 1, r, rt << 1 | 1);    PushUp(rt);    return;}void init(){    memset(lazy, 0, sizeof(lazy));    for(int i = 2; i <= n; i++)    {        degree[i] = PI;    }    Build(1, n, 1);    return;}int main(){    int kase = 0;    while(~scanf("%d%d", &n, &c))    {        if(kase++) printf("\n");        for(int i = 1; i <= n; i++)        {            scanf("%lf", &vec[i].y);            vec[i].x = 0;        }        init();        while(c--)        {            int s, a; scanf("%d%d", &s, &a);            double rad = (a * 1.0 / 180.0) * PI;            Update(s + 1, n, degree[s + 1] - rad, 1, n, 1);            degree[s + 1] = rad;            printf("%.2f %.2f\n", sum[1].x, sum[1].y);        }    }    return 0;}
原创粉丝点击