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