Apple Tree
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 30636   Accepted: 9162

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
 
 
题意:
给定一颗树,刚开始每个节点上有一个苹果,Q 询问以这个节点为根的子树苹果个数之和。
C 改变这个结点的苹果树,1则为0,0则改为1;
 
单点更新,用树状数组即可。复习一下树状数组。
注意:vector<vector<int> > G(maxn); 邻接表,不然会TLE。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector> using namespace std; const int maxn = ;
vector<vector<int> > G(maxn); int tot;
int in[maxn];
int out[maxn]; void dfs(int u,int father) {
in[u] = ++tot;
for(int i = ; i < (int)G[u].size(); i ++) {
int v = G[u][i];
if(v==father) continue;
dfs(v,u);
}
out[u] = tot;
} int C[maxn];
int n;
int lowbit(int x) {
return x&-x;
} // A[1] + A[2] + ... + A[x]
int sum(int x) {
int ret = ;
while(x>) {
ret +=C[x];
x-=lowbit(x);
}
return ret;
} // A[x] +=d
void add(int x,int d) {
while(x<=n) {
C[x] +=d;
x +=lowbit(x);
}
} int A[maxn]; int main()
{
scanf("%d",&n); for(int i=; i < n; i++) {
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
} dfs(,); for(int i=; i <= n; i++)
{
A[i] = ;
add(i,);
} int m;
scanf("%d",&m);
char cmd[];
while(m--) {
scanf("%s",cmd);
if(cmd[]=='Q') {
int x;
scanf("%d",&x);
printf("%d\n",sum(out[x])-sum(in[x]-));
}
else {
int x;
scanf("%d",&x);
if(A[x]==) {
add(in[x],-);
A[x] = ;
}
else {
add(in[x],);
A[x] = ;
}
}
} return ;
}
 
 

POJ 3321 DFS序的更多相关文章

  1. POJ 3321 DFS序+线段树

    单点修改树中某个节点,查询子树的性质.DFS序 子树序列一定在父节点的DFS序列之内,所以可以用线段树维护. 1: /* 2: DFS序 +线段树 3: */ 4:   5: #include < ...

  2. Apple Tree POJ - 3321 dfs序列构造树状数组(好题)

    There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. ...

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

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

  4. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号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. poj 3321 Apple Tree dfs序+线段树

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K       Description There is an apple tree outsid ...

  7. POJ 3321 Apple Tree(DFS序+线段树单点修改区间查询)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25904   Accepted: 7682 Descr ...

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

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=10486 题意:一颗有n个分支的苹果树,根为1,每个分支只有一个苹果,给出n- ...

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

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

随机推荐

  1. Python+Selenium操作select下拉框

    首先需要倒入Select模块: from selenium.webdriver.support.select import Select 常用方法: 通过索引定位:select_by_index() ...

  2. oracle 单实例DG(闪回技术四)

    一,flashback Oracle Flashback技术是一组数据库特性,它可以让你查看数据库对象的过去状态,或者将数据库对象返回到以前的状态,而无需使用基于时间点的介质恢复.根据数据库的变化,闪 ...

  3. HDFS基本shell操作

    在客户端输入Hadoop fs,可以查看所有的,hadoop shell # -help [cmd] //显示命令的帮助信息,如: hadoop fs -help ls # -ls(r) <pa ...

  4. (转)Shell——基本运算符

    Shell 基本运算符 原文:http://blog.csdn.net/sinat_36053757/article/details/70319481 Shell 和其他编程语言一样,支持多种运算符, ...

  5. [转]JQuery控制div外点击隐藏,div内点击不会隐藏

    一直弄清楚这个效果如何实现,看了这篇博客的几行代码原来如此简单,就是利用了事件冒泡而已. 比如有个div其id为body,实现在div外点击隐藏,div内点击不隐藏,采用jQuery实现如下: $(& ...

  6. 阿里云centos 7 中tomcat 自启动

    这里我的tomcat的安装路径为 /usr/local/tomcat 1 为tomcat添加自启动参数 catalina.sh在执行的时候会调用同级路径下的setenv.sh来设置额外的环境变量,因此 ...

  7. ife task0003学习笔记(三):JavaScript闭包

    一.this易错分析 在学习闭包的时候,有一个概念this很重要,关于this的理解,下面3种情况:this指向谁? fn.call(obj1); obj2.fn() fn() 答案是obj1 obj ...

  8. 解决引入外部文件(图片、js等)出现 403 net::ERR_ABORTED 的问题

    页面中引入外网的链接资源,会产生一个新的http请求.为了安全(URL里可能包含用户信息),浏览器一般都会给这写请求头加上表示来源的referrer 字段. 所以,此时我们需要隐藏外部链接中的refe ...

  9. Oracle新手常遇到的问题

    1.问题描述:尝试加载 Oracle 客户端库时引发 BadImageFormatException.如果在安装 32 位 Oracle 客户端组件的情况下以 64 位模式运行,将出现此问题. 解决方 ...

  10. axios请求报Uncaught (in promise) Error: Request failed with status code 404

    使用axios处理请求时,出现的问题解决 当url是远程接口链接时,会报404的错误: Uncaught (in promise) Error: Request failed with status ...