Apple Tree
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 25711   Accepted: 7624

Description

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
"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
"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
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=;
struct Edge{
int to,net;
}es[MAXN+MAXN];
int head[MAXN],tot;
int n,m;
int bit[MAXN],apple[MAXN],l[MAXN],r[MAXN],key;
void add(int i,int x)
{
while(i<MAXN)
{
bit[i]+=x;
i+=(i&-i);
}
}
int sum(int i)
{
int s=;
while(i>)
{
s+=bit[i];
i-=(i&-i);
}
return s;
}
void addedge(int u,int v)
{
es[tot].to=v;
es[tot].net=head[u];
head[u]=tot++;
}
void dfs(int u,int fa)
{
l[u]=++key;
for(int i=head[u];i!=-;i=es[i].net)
{
int v=es[i].to;
if(v!=fa)
{
dfs(v,u);
}
}
r[u]=key;//不需++
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(head,-,sizeof(head));
tot=;
key=;
for(int i=;i<MAXN;i++)
{
add(i,);
apple[i]=;
}
for(int i=;i<n-;i++)
{
int u,v;
scanf("%d%d",&u,&v);
addedge(u,v);
addedge(v,u);
}
scanf("%d",&m);
dfs(,-);
for(int i=;i<m;i++)
{
scanf("%*c");
char op;
int x;
scanf("%c %d",&op,&x);
if(op=='Q')
{
int res=sum(r[x])-sum(l[x]-);
printf("%d\n",res);
}
else
{
if(apple[x]) add(l[x],-);
else add(l[x],);
apple[x]=!apple[x];
}
}
}
return ;
}

POJ3321(dfs序列+树状数组)的更多相关文章

  1. HDU3887(树dfs序列+树状数组)

    Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  2. poj3321 dfs序+树状数组单点更新 好题!

    当初听郭炜老师讲时不是很懂,几个月内每次复习树状数组必看的题 树的dfs序映射在树状数组上进行单点修改,区间查询. /* 树状数组: lowbit[i] = i&-i C[i] = a[i-l ...

  3. 2016-2017 ACM-ICPC Southwestern European Regional Programming Contest (SWERC 2016) F dfs序+树状数组

    Performance ReviewEmployee performance reviews are a necessary evil in any company. In a performance ...

  4. [BZOJ1103][POI2007]大都市meg dfs序+树状数组

    Description 在经济全球化浪潮的影响下,习惯于漫步在清晨的乡间小路的邮递员Blue Mary也开始骑着摩托车传递邮件了.不过,她经常回忆起以前在乡间漫步的情景.昔日,乡下有依次编号为1..n ...

  5. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  6. HDU 3887:Counting Offspring(DFS序+树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样 ...

  7. HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...

  8. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  9. BZOJ 2434: [Noi2011]阿狸的打字机( AC自动机 + DFS序 + 树状数组 )

    一个串a在b中出现, 那么a是b的某些前缀的后缀, 所以搞出AC自动机, 按fail反向建树, 然后查询(x, y)就是y的子树中有多少是x的前缀. 离线, 对AC自动机DFS一遍, 用dfs序+树状 ...

随机推荐

  1. 20145240 《Java程序设计》第六周学习总结

    20145240 <Java程序设计>第六周学习总结 教材学习内容总结 InputStream与OutputStream 10.1.1串流设计的概念 Java将输入/输出抽象化为串流,数据 ...

  2. 发送ajax请求实现上传图片显示在网页上

    <?php // 1,通过超全局变量来获取files[上传的图片名称] $file = $_FILES["files"] // 2,在通过strrchr来获取图片的格式 $e ...

  3. Oracle配置文件

    在oracle安装目录$HOME/network/admin下,,经常看到sqlnet.ora tnsnames.ora listener.ora这三个文件,除了tnsnames.ora,其他两个文件 ...

  4. 利用paramiko获取上传下载远程服务器的资源信息

    # -*- coding: utf-8 -*- import paramikohostname='192.168.76.10'username='root'password='123456'param ...

  5. 【P2107】小Z的AK计划(优先队列+贪心)

    水一发优先队列的水题.. 这个题貌似以前有做过类似的.具体的方法是用大根堆辅助贪心算法得出正解.可以看出来,如果小Z走到了某个地方,那么他最远一定是到了这里,不可能有再走回来这种操作,因为很明显那样不 ...

  6. Mysql -- You can't specify target table 'address' for update in FROM clause

    做地址管理时,需要先根据要设为默认的地址的用户将用户的其他地址都设置为非默认 需要select出用户id然后update 原语句 update address set isdeafult = 0 wh ...

  7. 织梦dedecms 自带采集的缩略图地址后面有“/”斜杠的解决方法

    本来想偷懒在网上找一些文档去解决织梦采集缩略图地址带“/”的问题的,可是找了找发现没有人写出解决方法,只好自己动手了. 过程很复杂,找了问题的原因也是找了半天,自己一点一点的测试.最后找到了问题所在. ...

  8. vc 改变控制台字符颜色

    #include <Windows.h> #include <stdio.h> #include <iostream> using namespace std; i ...

  9. Spring中AOP的初窥和入门小案例

    AOP:面向切面编程 AOP的主要作用:是为了程序员更好的关注"业务",专心"做事" 加上双引号的意思:所谓业务,是指他的核心,各行业中需要处理的核心事务,核心 ...

  10. tp5定时器

    # 定时器 * * * * * cd /home/wwwroot/default/dexin/dragon && /usr/bin/php think order --option 1 ...