Apple Tree

Time Limit: 2000MS Memory Limit: 65536K

Total Submissions: 39566 Accepted: 11727

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

"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

Source

POJ Monthly--2007.08.05, Huang, Jinsong

中文题意

n个节点的苹果树,初始每个节点都有一个苹果。m个询问和修改,询问某棵子树的苹果树,修改某个节点,若该节点无苹果则长出苹果,有苹果则摘掉

题解

dfs序,记录进入时间和推出时间in[x],out[x],时间戳虽进入点数而增加,推出时不增加

以时间戳为下标,用树状数组维护单点修改,区间查询

查询in[x]到out[x]之间

修改in[x]

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <iostream>
void swap(int &a, int &b){int tmp = a;a = b, b = tmp;}
int max(int a, int b){return a > b ? a : b;}
int min(int a, int b){return a < b ? a : b;}
int lowbit(int x){return x & (-x);}
void read(int &x)
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9') c = ch, ch = getchar();
while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
if(c == '-') x = -x;
} const int INF = 0x3f3f3f3f;
const int MAXN = 100000 + 10; struct Edge
{
int u, v, nxt;
Edge(int _u, int _v, int _nxt){u = _u, v = _v, nxt = _nxt;}
Edge(){}
}edge[MAXN << 1];
int head[MAXN], cnt; void insert(int a, int b)
{
edge[++ cnt] = Edge(a, b, head[a]), head[a] = cnt;
edge[++ cnt] = Edge(b, a, head[b]), head[b] = cnt;
} int in[MAXN], out[MAXN], rank[MAXN], num[MAXN], t; int n, m; void modify(int x, int y)
{
for(;x <= n;x += lowbit(x))
num[x] += y;
}
int ask(int x)
{
int sum = 0;
for(;x;x -= lowbit(x))
sum += num[x];
return sum;
} void dfs(int x)
{
in[x] = ++ t;
rank[t] = x;
for(int pos = head[x];pos;pos = edge[pos].nxt)
{
int v = edge[pos].v;
if(in[v]) continue;
dfs(v);
}
out[x] = t;
} int main()
{
freopen("data.txt", "r", stdin);
read(n);
for(int i = 1;i < n;++ i)
{
int tmp1, tmp2;
read(tmp1), read(tmp2);
insert(tmp1, tmp2);
}
read(m);
dfs(1);
for(int i = 1;i <= n;++ i) modify(i, 1);
for(int i = 1;i <= m;++ i)
{
char tmp1;
int tmp2;
scanf("%c", &tmp1);
read(tmp2);
if(tmp1 == 'Q')
printf("%d\n", ask(out[tmp2]) - ask(in[tmp2] - 1));
else if(tmp1 == 'C')
{
int tmp3 = ask(in[tmp2]) - ask(in[tmp2] - 1);
if(tmp3 == 1) modify(in[tmp2], -1);
else modify(in[tmp2], 1);
}
}
return 0;
}

POJ3321Apple Tree的更多相关文章

  1. POJ3321Apple Tree[树转序列 BIT]

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26995   Accepted: 8007 Descr ...

  2. POJ3321Apple Tree Dfs序 树状数组

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

  3. poj3321-Apple Tree(DFS序+树状数组)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36442   Accepted: 10894 Desc ...

  4. POJ3321Apple Tree【dfs 树状数组】

    题目大意:一棵树(不一定是二叉树!!),树的节点上本来都有一个苹果,要求完成以下操作: 1.指定某个节点,如果这个节点原本有苹果则拿去,如果没有苹果则填上一个苹果 2.询问某个节点以及其子树一共有多少 ...

  5. poj3321Apple Tree(树状数组)

    http://poj.org/problem?id=3321 刚一看题以为要建一颗树 看了下讨论说dfs 这里dfs遍历时设的标号很好 一个low一个high 包含了以这一节点为根节点的子树结点的所有 ...

  6. C++-POJ3321-Apple Tree[数据结构][树状数组]

    树上的单点修改+子树查询 用dfn[u]和num[u]可以把任意子树表示成一段连续区间,此时结合树状数组就好了 #include <set> #include <map> #i ...

  7. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  8. SAP CRM 树视图(TREE VIEW)

    树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...

  9. 无限分级和tree结构数据增删改【提供Demo下载】

    无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...

随机推荐

  1. NX二次开发-UFUN工程图表格注释获取某一行的tag函数UF_TABNOT_ask_nth_row

    NX9+VS2012 #include <uf.h> #include <uf_tabnot.h> #include <NXOpen/Part.hxx> #incl ...

  2. NX二次开发-通过点击按钮来控制显示工具条

    NX9+VS2012 1.打开D:\Program Files\Siemens\NX 9.0\UGII\menus\ug_main.men 找到装配和PMI,在中间加上一段 TOGGLE_BUTTON ...

  3. (转)在eclipse中将android项目生成apk并且给apk签名

    转:http://www.cnblogs.com/tianguook/archive/2012/09/27/2705724.html 生成apk最懒惰的方法是:只要你运行过android项目,到工作目 ...

  4. 阿里云ecs(phpstudy一件包)

            选择语言       保存并连接    Linux硬盘挂载是比较常见的管理操作之一.默认情况下数据盘没有挂载,需要手动挂载到系统中.     具体操作是分三步:     硬盘挂载1)需 ...

  5. sp_executeSql 用法 执行有参数的sql字符串 出现必须声明标量变量 "@XXX"。

    今天遇到了一个难题 就是把 一个拼接sql语句 的返回值 赋值给一个变量 经研究 要用sp_executeSql这个存储过程 据说是从sql 2005才开始有的 代码如下: declare @str ...

  6. ATC/TC/CF

    10.25 去打 CF,然后被 CF 打了. CF EDU 75 A. Broken Keyboard 精神恍惚,WA 了一发. B. Binary Palindromes 比赛中的憨憨做法,考虑一个 ...

  7. java 对象转Map方法Demo

    /** * 用于对Object进行解析并且转换成Map键值对的形式 * */ public class ObjectUtils { private static final String JAVAP ...

  8. 【转载】C# 开源库大全非常好

    原文地址:http://m.blog.csdn.net/woddle/article/details/37311877 C#开源大全 商业协作和项目管理平台-TeamLab 网络视频会议软件-VMuk ...

  9. openwrt MySQL移植

    1 选择包 选择两个包,拷贝配置文件 cp products/mt7621/config_6080 .config 编译固件 openwrt 百万数据的优化, 执行脚本: mysql -u root ...

  10. Python自学:第五章 对数字列表执行简单的统计计算

    >>>digits = [1,2,3,4,5,6,7,8,9,0] >>>mid(digits) 0 >>>max(digits) 9 >& ...