POJ 2991(线段树经典例题) C++详解

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., 180o. 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).
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.

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

题目大意

因为是poj上的题目,所以题目是英文组成
翻译过来大意就是:
有N条线段,第i条线段长度为Li,开始首尾竖直相连,(1<=L<=100;1<=Si<N,0<=Ai<=359)
有C条指令,每条指令i给出Si和Ai:把Si和Si+1之间的角度变成Ai,起始角度都是180度
第一个点坐标为(0, 0),执行每条指令后,输出第N个点的坐标(保留两位小数)
输入包括多组样例

输入:

2 1
10 5
1 90

3 2
5 5 5
1 270
2 90

输出:

5.00 10.00

-10.00 5.00
-5.00 10.00

数据范围:

1<=C,N<=10000

题解:

这一题要求我们输出最后一条线段顶点的坐标
显然可以看出,这是一道计算几何的题目
我们需要用到公式:
1.角度与弧度转化公式:弧度=角度/360.0*2Π
2.对于一个向量(x,y),它顺时针旋转A度后,得到的新向量为:x’ = x * cos(A) + y * sin(A); y’ = y * cos(A) - x * sin(A)
最为重要的便是第二个公式。
注意到数据范围是1到10000,我们如果一个个的进行坐标的修改,复杂度可以算出为O(CN)=O(108)(单个数据)
显然无法满足题目要求
所以我们使用线段树进行区间修改
要注意的是,因为每一个叶节点是一条线段,我们以一条线段的两个顶点为一个区间作为叶节点,建树的时候到达倒数第二层即可

思路:

首先,我们注意到:
假设一个转了,它后边的就都得跟着转,这就代表,一条线段发生旋转,后面的所有线段都要旋转,但是他们的相对角度不发生改变
所以,我们可以把它转化一下:
当线段树的一个节点发生改变,后面的所有节点也都发生改变,所以包含被修改节点的区间,在被修改节点的后面节点的向量发生了改变,这个区间的向量(第l条到第r条向量)不就发生了改变吗?这不就是线段树的区间修改吗?
ps:这道题真恶心
所以代码如下:

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int MAX_N = 10005;
const int MAX_C = 10005;
const int MAX_SIZE = (1 << 15) - 1;  // 2的15次方-1
const double PI = acos(-1.0);	//Π的值

//输入的数据
int N, C;
int L[MAX_N];
int S[MAX_C], A[MAX_N];

double vx[MAX_SIZE], vy[MAX_SIZE];  //节点的向量(坐标)
double ang[MAX_SIZE];               //节点的角度

double prv[MAX_N];  //用于保留当前角度的数组

//建树,其中k是节点的编号,l是区间的左边界,r是区间的右边界
void build(int k, int l, int r) {
  ang[k] = vx[k] = 0.0;  //初始化节点横坐标的值和角度

  //注意!这不是一个完美二叉树,并没有扩展到最后一层,只扩展到倒数第二层
  //每一个叶节点代表Si与Si+1这一条线段
  if (r == l + 1) {
    vy[k] = L[l];  //叶子节点
  } else {
    int mid = (l + r) / 2;     //计算左右子节点的边界
    build(2 * k + 1, l, mid);		//递归到左儿子
    build(2 * k + 2, mid, r);		//递归到右儿子

    vy[k] = vy[2 * k + 1] + vy[2 * k + 2];  // update(更新该区间的值)
  }
}

//区间修改,修改叶节点的值,并且修改所有包含叶节点区间的相关值
// s是输入的第Si条线段,a是Si与Si+1之间角度转换而来的的弧度
void change(int s, double a, int k, int l, int r) {
  if (s < r && s > l) {
    int mid = (l + r) / 2;
    if (s <= mid) {
      change(s, a, 2 * k + 1, l, mid);

      //如果线段的编号在一个区间左儿子内,那么右儿子便要转动a弧度,便要更新这个区间,通过这种方式更新包含s的区间
      ang[k] += a;
    }
    else
      change(s, a, 2 * k + 2, mid, r);

    //计算第l条线段起点到第r条线段终点的向量
    double n = sin(ang[k]), c = cos(ang[k]);
    vx[k] = vx[2 * k + 1] + (c * vx[2 * k + 2] - n * vy[2 * k + 2]);
    vy[k] = vy[2 * k + 1] + (n * vx[2 * k + 2] + c * vy[2 * k + 2]);
  }
}

int main() {
  while (cin >> N >> C) {
    for (int i = 0; i < N; i++) {
      cin >> L[i];
    }
    for (int i = 0; i < C; i++) {
      cin >> S[i] >> A[i];
    }
    build(0, 0, N);  //初始化,建树
    for (int i = 0; i < N; i++) prv[i] = PI;

    //进行处理
    for (int i = 0; i < C; i++) {
      int s = S[i];
      double a = A[i] / 360.0 * 2 * PI;  //角度转化为弧度
      change(s, a - prv[s], 0, 0, N);
      prv[s] = a;  //将第s条线段的弧度更新为当前相对弧度

      printf("%.2lf %.2lf\n", vx[0], vy[0]);
    }
  }
  return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值