白皮书上的例题。。。
题目如下?
有n根长度不尽相同的棍子,初始时它们首尾垂直相连,标号为1--n,第一根棍子的下端坐标为(0,0),上端坐标为(0,len[1]),其余棍子依次类推。接下来执行C此旋转,每次输入一个编号num和角度rad,使得第num根棍子和第num+1跟棍子间的逆时针角度变为rad度,求每次旋转后第n跟棍子端点的坐标。
那么如果我们每次改动一根,很显然对最终结果都会产生影响,所以每次改动,需要改动的范围都是?起点 num---n ,我们最终要输出的是末端坐标,我们可以根据向量来解决这个问题,
对于一个向量 (x,y)?转动 (逆时针)A?之后,那么新向量 x' = x*cos(A) + y*sin(A) ;? y' =? y*cos(A) -x*sin(A)?
根据白皮书上和网上思路推了一下。。。
但是问题来了,假设一个转了,它后边的就都得跟着转,那这不就非常的像是区间更新了吗,我们对于每个节点维护的是一个向量,对于叶子节点,是当前棍子的向量,而非叶子节点,就是管理的全部棍子向量的合向量,运用区间更新的思路?,每次更新的都是更新一下当前 num ---- n,那么只要知道这些向量,我们不也就知道末端位置了吗。
以下是 AC 代码(很有意思的一道题)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid,r,rt<<1|1
const double PI = acos(-1.0);
const int maxn = 1e4+5;
struct node
{
double x,y,arg;
}ed[maxn<<2];
double ang[maxn];
void build(int l,int r,int rt)
{
ed[rt].x = 0.0;
ed[rt].arg = 0.0;
if(r - l == 1)
{
scanf("%lf", &ed[rt].y);
return;
}
int mid = l + r >> 1;
build(lson);
build(rson);
ed[rt].y = ed[rt << 1].y + ed[rt << 1 | 1].y;
}
void update(int L,int R,double rad,int l,int r,int rt)
{
if(L == l && R == r)
{
ed[rt].x = ed[rt].x * cos(rad) + ed[rt].y * sin(rad);
ed[rt].y = ed[rt].y * cos(rad) - ed[rt].x * sin(rad);
ed[rt].arg += rad;
return;
}
int mid = l + r >> 1;
if(ed[rt].arg != 0)
{
ed[rt<<1].x = ed[rt<<1].x * cos(ed[rt].arg) + ed[rt<<1].y * sin(ed[rt].arg);
ed[rt<<1].y = ed[rt<<1].y * cos(ed[rt].arg) - ed[rt<<1].x * sin(ed[rt].arg);
ed[rt<<1].arg += ed[rt].arg;
ed[rt<<1|1].x = ed[rt<<1|1].x * cos(ed[rt].arg) + ed[rt<<1|1].y * sin(ed[rt].arg);
ed[rt<<1|1].y = ed[rt<<1|1].y * cos(ed[rt].arg) - ed[rt<<1|1].x * sin(ed[rt].arg);
ed[rt<<1|1].arg += ed[rt].arg;
ed[rt].arg = 0.0;
}
if(R <= mid)
update(L, R, rad, lson);
else if(L >= mid)
update(L, R, rad, rson);
else
{
update(L, mid, rad, lson);
update(mid, R, rad, rson);
}
ed[rt].x = ed[rt << 1].x + ed[rt << 1 | 1].x;
ed[rt].y = ed[rt << 1].y + ed[rt << 1 | 1].y;
}
int main()
{
double cas;
int n,c;
int x,y;
int cnt = 0;
while(~scanf("%d%d",&n,&c))
{
build(0,n,1);
if(cnt ++)
printf("\n");
for(int i=1;i<=n;i++)
ang[i] = PI;
while(c--)
{
scanf("%d%d", &x, &y);
cas = y / 360.0 * 2 * PI;
update(x, n, ang[x] - cas, 0, n, 1);
ang[x] = cas;
printf("%.2lf %.2lf\n", ed[1].x, ed[1].y);
}
}
return 0;
}