Apple Tree
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 26762   Accepted: 7947

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

Source

POJ Monthly--2007.08.05, Huang, Jinsong
 
继续继续继续
 
dfs序嘛
原来之前理解的是错的
其实就是tarjan的dfn与low数组
得出dfn与low后
dfn[i]是起点,low[i]是终点,中间的一段就是点i的子树了
这样dfs序就可以“方便的维护子树”
而对于大部分按照dfs序维护序列的题,与树状数组是相当好的组合
这道题差不多是裸题了,多开一个表示当前节点状态的数组就可以了
 #include<stdio.h>
#include<stdlib.h>
#include<string.h>
int ap[],bit[];
int bg[],ed[],cnt=;
int n;
typedef struct{
int to,nxt;
}edge;
edge gra[];
int head[],num=;
int add(int frm,int to){
gra[++num].nxt=head[frm];
gra[num].to=to;
head[frm]=num;
return ;
}
int dfs(int u,int fa){
bg[u]=++cnt;
int j;
for(j=head[u];j;j=gra[j].nxt){
if(gra[j].to!=fa)dfs(gra[j].to,u);
}
ed[u]=cnt;
return ;
}
int lb(int x){
return x&(-x);
}
int c(int x){
int num=ap[x];
x=bg[x];
while(x<=n){
bit[x]+=num;
x+=lb(x);
}
return ;
}
int q(int x){
int a1=,a2=;
int e=ed[x];
while(e){
a1+=bit[e];
e-=lb(e);
}
int b=bg[x]-;
while(b){
a2+=bit[b];
b-=lb(b);
}
return a1-a2;
}
int main(){
scanf("%d",&n);
for(int i=;i<n;i++){
int x,y;
scanf("%d %d",&x,&y);
add(x,y);
add(y,x);
}
dfs(,);
for(int i=;i<=n;i++){
ap[i]=;
c(i);
}
int m;
scanf("%d",&m);
for(int i=;i<=m;i++){
char in[];
int x;
scanf("%s %d",in,&x);
if(in[]=='C'){
ap[x]*=-;
c(x);
}
else printf("%d\n",q(x));
}
return ;
}

睡觉

[poj3321]Apple Tree(dfs序+树状数组)的更多相关文章

  1. POJ 3321 Apple Tree DFS序 + 树状数组

    多次修改一棵树节点的值,或者询问当前这个节点的子树所有节点权值总和. 首先预处理出DFS序L[i]和R[i] 把问题转化为区间查询总和问题.单点修改,区间查询,树状数组即可. 注意修改的时候也要按照d ...

  2. 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 ...

  3. 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 ...

  4. POJ3321Apple Tree Dfs序 树状数组

    出自——博客园-zhouzhendong ~去博客园看该题解~ 题目 POJ3321 Apple Tree 题意概括 有一颗01树,以结点1为树根,一开始所有的结点权值都是1,有两种操作: 1.改变其 ...

  5. [Split The Tree][dfs序+树状数组求区间数的种数]

    Split The Tree 时间限制: 1 Sec  内存限制: 128 MB提交: 46  解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 You are given ...

  6. Codeforces Round #381 (Div. 2) D. Alyona and a tree dfs序+树状数组

    D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  7. 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 ...

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

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

  9. HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca

    Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...

随机推荐

  1. html中select只读显示

    因为Select下拉框只支持disabled属性,不支持readOnly属性,而在提交时,disabled的控件,又是不提交值的.现提供以下几种解决方案: 1.在html中使用以下代码,在select ...

  2. 如何使用 vimdiff 来 git diff /svn diff

    #git 如何实现vimdiffgit config --global diff.tool vimdiff git config --global difftool.prompt false git ...

  3. P1073 最优贸易

    #include <bits/stdc++.h> using namespace std; const int maxn = 100005; int head1[maxn], head2[ ...

  4. rabbitmq之amqp queue

    rabbitmq作为一个消息中间件,暂存信息的能力是必不可少的. 镜像队列

  5. Android百分比布局支持库介绍——com.android.support:percent(转)

    转载自http://www.apkbus.com/forum.php?mod=viewthread&tid=244752&extra=&_dsign=0b699c42 在此之前 ...

  6. 记录工作中用到的linux命令

    日常工作中会对centos进行操作,总是会有一些常用的命令记不住,特开一贴,记录那些命令,学而时习之.   RPM操作类命令:   查看RPM安装路径: 1.rpm -qa|grep Memcache ...

  7. LightOj 1278 - Sum of Consecutive Integers(求奇因子的个数)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1278 题意:给你一个数n(n<=10^14),然后问n能用几个连续的数表示; 例 ...

  8. Android颜色资源文件

    <?xml version="1.0" encoding="utf-8"?><resources> <color name=&qu ...

  9. awk实现按照某个字段排序

    awk 'BEGIN{ FS="|"} { ary[$14,NR]=$0} END{ nrw=asorti(ary, newary) for(i=1;i<=nrw;i++) ...

  10. iOS10 app连接不上网络的问题

    http://jingyan.baidu.com/article/29697b917f2069ab20de3c33.html