北大ACM2991——Crane~~线段树
来源:互联网 发布:音乐壁纸动态软件 编辑:程序博客网 时间:2024/06/05 03:40
最近看到了线段树,对于线段树也是有了初步的了解,还是需要时间继续研究,加深理解。
感觉线段树,个人觉得最主要的是递归过程的理解。
这一题,给定一段绳子,“ 分成 ”N段,起初,每段绳子都是垂直的。然后有C个命令,每个命令包含两个数 i , j,i 是第几段绳子,j 是 i 段绳子旋转到 i + 1 段绳子多经过的角度。也就是 i 和 i + 1 之间的角度是 j。
可以理解成向量,起初每个向量方向向上。
如果结点 i 表示的向量是vx1, vy1。角度是angle,两个儿子节点是chl,chr,那么:
vx1 = vx(chl)+ (cos(angle)* vx(chr)- sin(angle)* vy(chr));
vy1 = vy(chl)+ (sin(angle)* vx(chr)+ cos(angle)* vy(chr));
下面是AC的代码:
#include <iostream>#include <cmath>#include <cstdio>using namespace std;const double PI = acos(-1.0); //PI,直接用3.1415927过不了。const int MAX_SIZE = (1 << 15) - 1; //线段树的最大长度const int MAX_N = 10005; //N的最大值const int MAX_C = 10005; //C的最大值int N, C;int L[MAX_N], S[MAX_C], A[MAX_C]; //L是每段长度,S是每个命令中的第几段的数组,A是角度数组double vx[MAX_SIZE], vy[MAX_SIZE]; //各个向量的数组double angle[MAX_SIZE]; //各个向量的角度数组double temp[MAX_SIZE]; //角度变化而保存的当前角度的数组//k是结点编号,l,r是区间[l,r]void init(int k, int l, int r) //线段树的初始化{angle[k] = vx[k] = 0.0; //角度都为0,x坐标为0.if(r - l == 1) //叶子结点的y坐标{vy[k] = L[l];}else //非叶子结点{int chl = k * 2 + 1; int chr = k * 2 + 2;init(chl, l, (l + r) / 2);init(chr, (l + r) / 2, r);vy[k] = vy[chl] + vy[chr];}}//将s和s+1段的角度变成a//v是结点编号void change(int s, double a, int v, int l, int r){if(s <= l)return;else if(s < r) //s>l & s<r{int chl = 2 * v + 1; int chr = 2 * v + 2;int m = (l + r) / 2;change(s, a, chl, l, m);change(s, a, chr, m, r);if(s <= m)angle[v] += a;double s = sin(angle[v]), c = cos(angle[v]);vx[v] = vx[chl] + (c * vx[chr] - s * vy[chr]); //改变向量vy[v] = vy[chl] + (s * vx[chr] + c * vy[chr]);}}void solve(){init(0, 0, N); //初始化for(int i = 0; i < N; i++) //起始每个向量的角度为180度,也就是πtemp[i] = PI;for(int j = 0; j < C; j++){int s = S[j];double a = A[j] / 360.0 * 2 * PI;change(s, a - temp[s], 0, 0, N);temp[s] = a; //角度改变printf("%.2lf %.2lf\n\n", vx[0], vy[0]); //输出}}int main(){while(scanf("%d%d", &N, &C) != EOF) //输入{for(int i = 0; i < N; i++)scanf("%d", &L[i]);for(int j = 0; j < C; j++)scanf("%d%d", &S[j], &A[j]);solve();}return 0;}
0 0
- 北大ACM2991——Crane~~线段树
- POJ 2991 Crane(线段树+计算几何—更新向量)
- POJ_2991 Crane 线段树
- Crane(线段树)
- Crane(线段树)
- poj2991 Crane (线段树)
- POJ 2991 Crane 线段树
- 线段树 POJ 2991Crane
- POJ2991:Crane(线段树+几何)
- poj 2991 Crane(线段树)
- POJ 2991 Crane (线段树)
- POJ 2991 Crane 线段树
- 【POJ 2991 Crane】+ 线段树
- poj Crane 线段树变种
- poj2991 Crane 线段树+计算几何
- poj 2991--Crane(线段树)
- POJ2991 Crane 【线段树+计算几何】
- poj2991 Crane 向量旋转+线段树
- java反射
- 线程安全类对比
- 利用CAS操作(Compare & Set)实现无锁队列
- CentOs开放自己需要的端口
- OVER(PARTITION BY OVER (ORDER BY
- 北大ACM2991——Crane~~线段树
- 如何限制对象只能建立在堆上或者栈上
- Lucene入门
- Hadoop本地库警告处理(WARN util.NativeCodeLoader: Unable to load native-hadoop library)
- fstream的使用和打开时存在中文中文路径可能失败的问题,其他中文问题都可以尝试采用如下方法
- LNMP环境下php-fpm启动脚本
- AsyncTask的使用方法
- python在window中显示内存对象关系图objgraph出错的应对办法
- 【error】LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value