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. NOIp2018集训test-9-5(am)

    Problem A. maze 递归处理,题解写得真简单. 我大概这辈子也写不出来这种东西吧. Problem B. count 容易发现合法的数中一定有且仅有两个数加起来等于10,其他数两两配对加起 ...

  2. NX二次开发-UFUN点构造器UF_UI_point_construct

    #include <uf.h> #include <uf_ui.h> UF_initialize(); //点构造器 char sCue[] = "点构造器" ...

  3. fedora23上安装和运行MySQL server (MySQL 已经被MariaDB取代)

    [root@localhost kemin]# dnf install mysql-server Fedora 23 - x86_64 - Updates                        ...

  4. ionic-CSS:ionic 列表

    ylbtech-ionic-CSS:ionic 列表 1.返回顶部 1. ionic 列表 列表是一个应用广泛的界面元素,在所有移动app中几乎都会使用到. 列表可以是基本文字.按钮,开关,图标和缩略 ...

  5. Java 网络编程(1):使用 NetworkInterface 获得本机在局域网内的 IP 地址

    原文地址:https://segmentfault.com/a/1190000007462741 1.问题提出 在使用 Java 开发网络程序时,有时候我们需要知道本机在局域网中的 IP 地址.很常见 ...

  6. gvim 安装YouCompleteMe插件

    可以参考:YouCompleteMe#full-installation-guide 可以直接下载: http://pan.baidu.com/s/1dDIq2Al 密码: si5q 确保vim支持p ...

  7. 2015年7个重要的Web设计趋势

    Web设计趋势每一年都会有所变化.但设计师的创意天赋是推动改变网页设计标准的法则.如果在2015年,网页缺少以下7个设计元素,必定被淘汰~ 1.排版更灵活 这部分的主要焦点在于,字体展现会受到新兴排版 ...

  8. verbosity

    import unittest class myTest(unittest.TestCase): def test_01(self): print("普通方法1") def tes ...

  9. shell 命令 文件查看ls,复制cp,移动mv,查看文件内容cat more less,查看文件信息 file

    1. 查看文件 ls        ls -l  查看文件详细信息 ls -a 查看所有文件(包含隐藏文件) ls -lh  带单位显示文件大小 ls -i  查看文件的节点号(相当身份证唯一)  2 ...

  10. postman报InvalidArgumentException

    解决方案: