A Simple Tree Problem
Time Limit: 3 Seconds Memory Limit: 65536 KB
Given a rooted tree, each node has a boolean (0 or 1) labeled on it. Initially, all the labels are 0.
We define this kind of operation: given a subtree, negate all its labels.
And we want to query the numbers of 1's of a subtree.
Input
Multiple test cases.
First line, two integer N and M, denoting the numbers of nodes and numbers of operations and queries.(1<=N<=100000, 1<=M<=10000)
Then a line with N-1 integers, denoting the parent of node 2..N. Root is node 1.
Then M lines, each line are in the format "o node" or "q node", denoting we want to operate or query on the subtree with root of a certain node.
Output
For each query, output an integer in a line.
Output a blank line after each test case.
Sample Input
3 2
1 1
o 2
q 1
Sample Output
1
题意容易理解不说了。
思路:刚开始不知道怎么把平常的树转化为线段二叉树,唉……弄了几天,自己以前的线段树模板写了又写,对lazy标记自己的结构体线段树模板不好,所以看了别人的线段树代码是用数组写的,就有点晕了……然后对比了自己的线段树模板,发现用数组做处理lazy标记比较直观,用起来也比较爽……哈哈,然后用了一晚上弄这个线段树模板,相当于把以前的线段树常用的结构体给弄翻了,又理解了好久才会……呵呵……
这道题如果用图画一下,就会发现这棵树每个结点会有超过两个的子树,但是又是边修改边查询,所以肯定是用线段树做,所以就得把这棵不平常的树转化为线段树了。
如果用后序遍历这棵树的话,就可以得到线性区间的线性序列,然后就可以用线段树表示了。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include