There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree.

The tree has N forks which are connected by branches. Kaka numbers the forks by 1 to N and the root is always numbered by 1. Apples will grow on the forks and two apple won't grow on the same fork. kaka wants to know how many apples are there in a sub-tree, for his study of the produce ability of the apple tree.

The trouble is that a new apple may grow on an empty fork some time and kaka may pick an apple from the tree for his dessert. Can you help kaka?

Input

The first line contains an integer N (N ≤ 100,000) , which is the number of the forks in the tree.
The following N - 1 lines each contain two integers u and v, which means fork u and fork v are connected by a branch.
The next line contains an integer M (M ≤ 100,000).
The following M lines each contain a message which is either
"C x" which means the existence of the apple on fork x has been changed. i.e. if there is an apple on the fork, then Kaka pick it; otherwise a new apple has grown on the empty fork.
or
"Q x" which means an inquiry for the number of apples in the sub-tree above the fork x, including the apple (if exists) on the fork x
Note the tree is full of apples at the beginning

Output

For every inquiry, output the correspond answer per line.

Sample Input

3
1 2
1 3
3
Q 1
C 2
Q 1

Sample Output

3
2

题解:题目有两种操作:1.更改某节点值(1~0变换)。2.询问某节点和其所有子节点所有权值之和。
我们可以从1开始进行DFS重新编号,对于每一个原来的节点,记录下这个节点的编号Start[i]和子节点中最大编号End[i],这样我们查询一个节点和其所有字节点的权值之和时,便可运用树状数组的求法:
sum[End[i]]-sum(Start[i]-1).
#include<iostream>
#include<cstring>
#include<vector>
#include<string>
#include<stdio.h>
using namespace std;
typedef long long ll;
int lowbit(int x){return x&-x;}
const int maxn=;
int n,c[maxn];
void update(int x,int v)
{
for(int i=x;i<=n;i+=lowbit(i))
c[i]+=v;
}
int sum(int x)
{
int ans=;
for(int i=x;i>=;i-=lowbit(i))
ans+=c[i];
return ans;
}
struct node
{
int v,next;
}e[maxn*];
int cnt=;
int head[maxn];
void add(int u,int v){
e[cnt].v=v;
e[cnt].next=head[u];
head[u]=cnt++;
}
int tot=;
int Start[maxn],End[maxn];
int viss[maxn];
void DFS(int x)
{
Start[x]=++tot;
for(int i=head[x];i!=-;i=e[i].next){
DFS(e[i].v);
}
End[x]=tot;
}
int main()
{
memset(head,-,sizeof(head));
scanf("%d",&n);
for(int i=;i<=n-;i++){
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
}
DFS();
memset(viss,,sizeof(viss));
for(int i=;i<=n;i++)c[i]=lowbit(i);
int q;
scanf("%d",&q);
while(q--){
char s[];
int x;
scanf("%s%d",s,&x);
if(s[]=='Q'){
printf("%d\n",sum(End[x])-sum(Start[x]-));
}
else{
if(!viss[x]){
viss[x]=;
update(Start[x],);
}
else{
viss[x]=;
update(Start[x],-);
}
}
}
return ;
}

E - Apple Tree(树状数组+DFS序)的更多相关文章

  1. POJ 3321 Apple Tree (树状数组+dfs序)

    题目链接:http://poj.org/problem?id=3321 给你n个点,n-1条边,1为根节点.给你m条操作,C操作是将x点变反(1变0,0变1),Q操作是询问x节点以及它子树的值之和.初 ...

  2. POJ--3321 Apple Tree(树状数组+dfs(序列))

    Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22613 Accepted: 6875 Descripti ...

  3. POJ 3321 Apple Tree 树状数组+DFS

    题意:一棵苹果树有n个结点,编号从1到n,根结点永远是1.该树有n-1条树枝,每条树枝连接两个结点.已知苹果只会结在树的结点处,而且每个结点最多只能结1个苹果.初始时每个结点处都有1个苹果.树的主人接 ...

  4. 【BZOJ】2434: [Noi2011]阿狸的打字机 AC自动机+树状数组+DFS序

    [题意]阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的: l 输入小写 ...

  5. HDU5293(SummerTrainingDay13-B Tree DP + 树状数组 + dfs序)

    Tree chain problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  6. 【BZOJ-3881】Divljak AC自动机fail树 + 树链剖分+ 树状数组 + DFS序

    3881: [Coci2015]Divljak Time Limit: 20 Sec  Memory Limit: 768 MBSubmit: 508  Solved: 158[Submit][Sta ...

  7. 【BZOJ-1103】大都市meg 树状数组 + DFS序

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2009  Solved: 1056[Submit][Sta ...

  8. POJ 3321 Apple Tree(树状数组)

                                                              Apple Tree Time Limit: 2000MS   Memory Lim ...

  9. [luogu P3787][新创无际夏日公开赛] 冰精冻西瓜 [树状数组][dfs序]

    题目背景 盛夏,冰之妖精琪露诺发现了一大片西瓜地,终于可以吃到美味的冻西瓜啦. 题目描述 琪露诺是拥有操纵冷气程度的能力的妖精,一天她发现了一片西瓜地.这里有n个西瓜,由n-1条西瓜蔓连接,形成一个有 ...

随机推荐

  1. Git--记一次丢失本地记录但是代码已提交到gerrit

    参考 https://blog.csdn.net/yucendulang/article/details/76199913 https://stackoverflow.com/questions/28 ...

  2. 遍历数组提取List[Int]

    def toFlatMap(input:List[Any],result:List[Int]):List[Int]=input match{ case h::t=>h match {case e ...

  3. python中的单元测试模块unittest

    unittest的属性: 该文以思维导图的形式描述unittest的重要属性. 其中前四个是unittest最核心的三个属性. testcase:测试用例: testsuite:测试套件,多个测试用例 ...

  4. CTF -攻防世界-crypto新手区(5~11)

    easy_RSA 首先如果你没有密码学基础是得去恶补一下的 然后步骤是先算出欧拉函数 之后提交注意是cyberpeace{********}这样的 ,博主以为是flag{}耽误了很长时间  明明没算错 ...

  5. idea拉取git项目并创建为maven项目(新创建github项目)

    0 环境 系统环境:win10 编辑器:idea 1 正文 1 clone项目 跟着提示yes 下一步 2 在根节点添加pom.xml(maven) <?xml version="1. ...

  6. 1-3.监督学习(supervised learning)

    定义:监督学习指的就是我们给学习算法一个数据集,这个数据集由“正确答案”组成,然后运用学习算法,算出更多的正确答案.术语叫做回归问题 [监督学习可分为]:回归问题.分类问题.两种 例:一个学生从波特兰 ...

  7. windows和ubuntu双系统设置开机默认系统

    1.记住grub界面中windows的位置 我的界面如下:windows在第3行 2.选择进入ubuntu系统 3.打开终端,输入如下命令 sudo vim /etc/default/grub 4.看 ...

  8. MySQL--SQL分类

    SQL语句主要可以划分为以下3个类别: DDL(Data Definition Languages)语句:数据定义语言,这些语句定义了不同的数据段.数据库.表.列.索引等数据库对象. 常用的语句关键字 ...

  9. JavaSE--for each

    参考:http://blog.csdn.net/yasi_xi/article/details/25482173 学习多线程的时候实例化线程数组而挖掘出来的一直以来的理解误区 之前一直以为for ea ...

  10. runlevel 运行级别

    linux启动过程 关于Ubuntu 12.04修改默认运行级别,启动字符界面的个人理解   网上通常的做法是:(亲自试验,不管用),如果想直接操作请看绿色字体部分 (1)第一种方法:   由于Red ...