2014多校7(1008)hdu4942(树的中序遍历+线段树)
来源:互联网 发布:商业拓展 数据分析 编辑:程序博客网 时间:2024/05/15 23:52
Game on S♂play
Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 215 Accepted Submission(s): 36
Problem Description
Uncle Fang is learning Splay. When he heard about the rotate operation, he would like to play a game with Captain Chen. There is a tree with n nodes initially, and the node 1 is the root. The i-th nodes gets a weight of wi, and an attribute of interesting value defined as the sum of the weight of it’s descendants. Following are two operations in this tree:
1.do a rotation on a node x. The rotation is the common rotation in splay tree, it can be right-rotation or left-rotation, as demonstrated in the following picture.If it's impossible to rotate, just ignore it!
2.ask Captain Chen the product of interesting values of the nodes in a subtree rooted at node x.
1.do a rotation on a node x. The rotation is the common rotation in splay tree, it can be right-rotation or left-rotation, as demonstrated in the following picture.If it's impossible to rotate, just ignore it!
2.ask Captain Chen the product of interesting values of the nodes in a subtree rooted at node x.
Input
There are several cases.The first line of the input contains a single integer T (T <= 30) which is the number of test cases.Then comes the T test cases .
For each case, the first line contains two integer numbers n and m(1<=n, m<=100000).
The following n lines describe the tree. In the i-th line, there was three number wi, xi, yi,(0<=w<=100000, 0<=x, y<=n) indicate the weight of node i, the left child of node i, and the right child of node i. If xi or yi equals to 0, it means node i has no such child.
And the following m lines describe the operations. There are two numbers p x in each line. If p is 0, it means do right-rotation on node x; If p is 1, it means do left-rotation on node x; if p is 2, it means ask the product of interesting values of the nodes in a subtree rooted at node x.
For each case, the first line contains two integer numbers n and m(1<=n, m<=100000).
The following n lines describe the tree. In the i-th line, there was three number wi, xi, yi,(0<=w<=100000, 0<=x, y<=n) indicate the weight of node i, the left child of node i, and the right child of node i. If xi or yi equals to 0, it means node i has no such child.
And the following m lines describe the operations. There are two numbers p x in each line. If p is 0, it means do right-rotation on node x; If p is 1, it means do left-rotation on node x; if p is 2, it means ask the product of interesting values of the nodes in a subtree rooted at node x.
Output
For the k-th test case, first output “Case #k:” in a separate line.Then for each query,output a single num in each line after mod 1000000007.
Sample Input
13 41 2 31 0 01 0 02 10 12 22 1
Sample Output
Case #1:362HintAs the input data may cause the stack overflow if you use some recursion solution.Add #pragma comment (linker,"/STACK:102400000,102400000") in C++ language.
题意:给你一棵二叉树,支持左旋右旋操作,然后查询一个点的有趣值(子树节点权值的乘积)
思路:由于只支持左旋右旋,可以注意到这两种操作是不会改变整棵树的中序遍历的,由于每次旋转只会改变两个节点的信息,故可以直接用数组维护各个节点的信息,包括左
右孩子和父亲(我就是因为在旋转以后只换了孩子而没有换父亲就无限WA了 sad~),而每个节点的子树都是整棵树中序序列上的一段连续的区间,故可以用线段树点更新来
维护一段区间的乘积,突然觉得这题挺简单的= =。。
0 0
- 2014多校7(1008)hdu4942(树的中序遍历+线段树)
- hdu4942(线段树的巧用)
- 【线段树+Splay】HDU4942 Game on S♂play
- [线段树模拟平衡树] HDU4942. Game on S♂play
- 【平衡树->线段树】HDU4942[Game on S♂play]题解
- 树的前序遍历,中序遍历,后序遍历,层次(广度)遍历
- 二叉树的遍历(1)--先序遍历,中序遍历,后序遍历
- 二叉树的遍历(前序遍历、中序遍历、后序遍历)
- 二叉树的遍历代码(先序遍历,中序遍历,后序遍历)
- 二叉树的建立及遍历(先序遍历、中序遍历、后续遍历、层次遍历)
- 二叉树的遍历(2):中序遍历
- 已知树的前序遍历和中序遍历,求后序遍历的方法(转)
- 二叉树的遍历,二叉树的创建、前序遍历、中序遍历、后序遍历 (转)
- 二叉树的前序遍历,中序遍历和后序遍历(c#)
- 二叉树T 的先序遍历、中序遍历、后序遍历(递归实现)
- 二叉树的遍历(2)--先序遍历,中序遍历,后序遍历(非递归)
- 算法学习 - 树的三种遍历(递归实现)先序遍历,中序遍历,后序遍历
- 【原创】二叉树的建立与遍历(前序遍历、中序遍历、后序遍历)
- PX最新解析文件
- C++ 利用<cstdlib> 头文件 , 产生一个random number generator
- 人脸检测
- py条件语句
- 数据结构习题集答案1
- 2014多校7(1008)hdu4942(树的中序遍历+线段树)
- OpenCv截取图像
- Python条件语句Python条件语句是通过一条或多条语句的执行结果
- 华为机试整理[2014年8月15日上午机试准备]
- 本方法使用于V9.7.06以上版本
- ao硬件加速的一个接口和使用(IGlobalScreenDisplaySettings)
- leetcode Populating Next Right Pointers in Each Node(*)
- Spring 3之MVC & Security简单整合开发(一)
- poj 3026 Borg Maze BFS加上最短路