JOJ 2445: Tree 线段树

 2445: Tree


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
2s16384K38440Standard

in this problem , i will give you a n-vertex-01-tree with the vertex numbered with 1 as root , as it states,the tree has n vertexes, each node is marked from 1 to n, the node marked with 1 is the root of tree. each node has a value either 1 or 0, in the initial state, each node has the value 0;

in the process,there are two operations on the tree:
1. Q x: return the sum of the value on the subtree with vertext x as root ( include the value of on the vertex x );
2. C x: inverse all the value on the subtree with vertex x as root ( include vertex x ), inverse means turn 0 to 1 ,1 to 0;

Input

the input has multicases, in each case , the first line is n, the number vertex of tree; following will be n - 1 lines, each line has two integers u, v (1 <= u,v <= n) which means there is a side between u and v; following is a integer m which means there are m operations, the following m lines is either "Q x" or "C x" ( quotation for clarification ) , 1<=n,m<=20000;

Output

for each "Q x" operation return the sum of the value on the subtree with vertex x as root

Sample Input

5
1 2
1 3
2 4
2 5
5
Q 1
C 1
Q 1
C 2
Q 1

Sample Output

0
5
2

Hint

the tree looks as

                                   1
                                  / /
                                 2   3
                                / /
                               4   5
the first "Q 1" return 0 becaus all the node with value 0 ( the initial state ), the second return 5 because we have a operation "C 1" on the tree which inverse all the value on the tree ( turn all 0 to 1, turn all 1 to 0 ( in this state no 1) ) include the value on the node 1, so return 5, the third turn the value on the node 2, 4, 5 from 1 to 0 so return 2;

 

 

Problem Source: kscinow

 


This problem is used for contest: 95 


Submit / Problem List / Status / Discuss


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值